Title in matlab graph subplot - python

I've created a matlab graph in my Tkinter GUI. Which is part of a bigger GUI class. I'm having issues with adding a title.
Question: Does anyone know how I give my subplots a title ?
self.f = plt.Figure(figsize=(4,5), dpi=90)
self.a = self.f.add_subplot(211)
self.a.plot([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 1, 7, 4, 2, 5, 0])
self.a.plt.ylabel('some numbers')
self.b = self.f.add_subplot(212)
self.b.plot([1, 2, 3, 4, 5, 6, 7, 8], [1, 3, 6, 1, 0, 2, 1, 0])
self.canvas = FigureCanvasTkAgg(self.f, master=self.frame1)
self.canvas.get_tk_widget().grid(row=8, column=0, columnspan=2)
Simply adding the following code doesn't work.
self.a.plt.title('some numbers')

This is an example from matplotlib
Matplotlib has standards I haven't quite fully comprehended yet, but it seems that which method you use to do something like set a title depends on if you're using a fig, plot, or axis...in this case, the answer is:
self.a.set_title('title goes here for your subplot')

Related

Pygmt : how to fill polygons with cmap color

Good morning,
I am using pygmt on python 3.6 with spyder.
I am trying to fill several polygons in a range of colors defined by a colorpalet.
I used makecpt to define the colorpalet.
The variable I want to represent is Mog.
My code is :
pygmt.makecpt(cmap="bilbao", series=[-5, 50, 5])
for i , long in enumerate(longitude_polyT):
fig.plot(x=longitude_polyT[i], y=latitude_polyT[i], frame="a", pen="black", color=Mog[i], cmap=True)
But it doesn't fill my polygons.
Does anybody have an idea about it?
Thanks a lot!
Here is my best guess at what you want:
import pygmt
fig = pygmt.Figure()
a = fig.basemap(region=[0, 6, 0, 2], projection="X12c/4c", frame=True, )
pol = ["n0.9c", "c0.9c", "d0.9c"]
Mog = [
pygmt.makecpt(cmap="bilbao", series=[-5, 50, 5]),
pygmt.makecpt(cmap="bilbao", series=[-5, 15, 5]),
pygmt.makecpt(cmap="bilbao", series=[-8000, 4000])
]
longitude_polyT = [1, 3, 5]
latitude_polyT = [1, 1, 1]
for i, long in enumerate(longitude_polyT):
fig.plot(x=long, y=latitude_polyT[i], style=pol[i], frame=a,
pen="black",
color=Mog[i], cmap=True)
fig.show()
Couldn't get it to show different colours :(

Add list to Matplotlib

I am doing a randomly generated world and I'm starting of with basic graphing trying to be similar to Perlin Noise. I did everything and the last thing that I've written (important one) did work.
import math
import random
import matplotlib.pyplot as plt
print('ur seed')
a = input()
seed = int(a)
b = (math.cos(seed) * 100)
c = round(b)
# print(c)
for i in range(10):
z = (random.randint(-1, 2))
change = (z + c)
gener = []
gener.append(change)
time = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#print(gener)
#print(change)
plt.ylabel('generated')
plt.xlabel('time')
#Here I wanna add them to the graph and it is Erroring a lot
plt.scatter(time, gener)
plt.title('graph')
plt.show()
the problem is that you're setting gener to [] in the loop not out of the loop. also, you don't need the time variable inside the loop either.
change
for i in range(10):
z = (random.randint(-1, 2))
change = (z + c)
gener = []
gener.append(change)
time = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
to
gener = []
for i in range(10):
z = (random.randint(-1, 2))
change = (z + c)
gener.append(change)
time = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Sort x-axis for sns.countplot

I am working my way through: https://medium.com/analytics-vidhya/exploratory-data-analysis-of-the-hotel-booking-demand-with-python-200925230106
In a bunch of the visualization outputs the sort order is off. As I am working my way through each question, I have successfully fixed the sort order of every output -- until now.
For question #6, part two (Let’s see the stay duration trend for each hotel type.) I am getting
the exact same output as is shown in the article. However, the x-axis is incorrectly sorted, and I am trying to fix it as I have all previous outputs.
Here is my code for question #6, including the first part where I fixed the sort order:
# 6. How long do people stay in the hotel?
df_not_canceled2 = df_not_canceled.copy()
total_nights = df_not_canceled2['stays_in_weekend_nights'] + df_not_canceled2['stays_in_week_nights']
x5, y5, z5 = get_count(total_nights, limit=10)
x5 = x5[[0, 2, 1, 3, 5, 6, 4, 8, 7, 9]]
y5 = y5[[0, 2, 1, 3, 5, 6, 4, 8, 7, 9]]
z5 = z5[[0, 2, 1, 3, 5, 6, 4, 8, 7, 9]]
plot(x5, y5, x_label='Number of Nights', y_label='Booking Percentage (%)', title='Night Stay Duration (Top 10)', figsize =(10, 5))
plt.show()
# The stay duration trend for each hotel type.
df_not_canceled2.loc[:, 'total_nights'] = df_not_canceled2['stays_in_weekend_nights'] + df_not_canceled2['stays_in_week_nights']
df_not_canceled2 = df_not_canceled2.sort_values(by=['total_nights']).reset_index(drop=True)
fig1, ax = plt.subplots(figsize=(12, 6))
ax.set_xlabel('No of Nights')
ax.set_ylabel('No of Nights')
ax.set_title('Hotel wise night stay duration (Top 10)')
sns.countplot(x='total_nights', hue='hotel', data=df_not_canceled2,
order=df_not_canceled2['total_nights'].value_counts().iloc[:10].index, ax=ax)
plt.show()
First I tried sorting the df by 'total_nights'. The output did not change. Then I sorted and reset the index (this is the current state of my code). Still no change.
This is what I get (exactly the same as the article):
Notice the sort order of the x-axis (total_nights). I want 1, 2, 3, 4, 5, etc., not 1, 3, 2, 4, 7, etc.
Just figured it out. Had to remove .value_counts from the order parameter.

Is there any pytorch function can combine the specific continuous dimensions of tensor into one?

Let's call the function I'm looking for "magic_combine", which can combine the continuous dimensions of tensor I give to it. For more specific, I want it to do the following thing:
a = torch.zeros(1, 2, 3, 4, 5, 6)
b = a.magic_combine(2, 5) # combine dimension 2, 3, 4
print(b.size()) # should be (1, 2, 60, 6)
I know that torch.view() can do the similar thing. But I'm just wondering if there is any more elegant way to achieve the goal?
a = torch.zeros(1, 2, 3, 4, 5, 6)
b = a.view(*a.shape[:2], -1, *a.shape[5:])
Seems to me a bit simpler than the current accepted answer and doesn't go through a list constructor (3 times).
There is a variant of flatten that takes start_dim and end_dim parameters. You can call it in the same way as your magic_combine (except that end_dim is inclusive).
a = torch.zeros(1, 2, 3, 4, 5, 6)
b = a.flatten(2, 4) # combine dimension 2, 3, 4
print(b.size()) # should be (1, 2, 60, 6)
https://pytorch.org/docs/stable/generated/torch.flatten.html
There is also a corresponding unflatten, in which you can specify a dimension to unflatten and a shape to unflatten it to.
I am not sure what you have in mind with "a more elegant way", but Tensor.view() has the advantage not to re-allocate data for the view (original tensor and view share the same data), making this operation quite light-weight.
As mentioned by #UmangGupta, it is however rather straight-forward to wrap this function to achieve what you want, e.g.:
import torch
def magic_combine(x, dim_begin, dim_end):
combined_shape = list(x.shape[:dim_begin]) + [-1] + list(x.shape[dim_end:])
return x.view(combined_shape)
a = torch.zeros(1, 2, 3, 4, 5, 6)
b = magic_combine(a, 2, 5) # combine dimension 2, 3, 4
print(b.size())
# torch.Size([1, 2, 60, 6])
Also possible with torch einops.
Github.
> pip install einops
from einops import rearrange
a = torch.zeros(1, 2, 3, 4, 5, 6)
b = rearrange(a, 'd0 d1 d2 d3 d4 d5 -> d0 d1 (d2 d3 d4) d5')

Computing average for numpy array

I have a 2d numpy array (6 x 6) elements. I want to create another 2D array out of it, where each block is the average of all elements within a blocksize window. Currently, I have the foll. code:
import os, numpy
def avg_func(data, blocksize = 2):
# Takes data, and averages all positive (only numerical) numbers in blocks
dimensions = data.shape
height = int(numpy.floor(dimensions[0]/blocksize))
width = int(numpy.floor(dimensions[1]/blocksize))
averaged = numpy.zeros((height, width))
for i in range(0, height):
print i*1.0/height
for j in range(0, width):
block = data[i*blocksize:(i+1)*blocksize,j*blocksize:(j+1)*blocksize]
if block.any():
averaged[i][j] = numpy.average(block[block>0])
return averaged
arr = numpy.random.random((6,6))
avgd = avg_func(arr, 3)
Is there any way I can make it more pythonic? Perhaps numpy has something which does it already?
UPDATE
Based on M. Massias's soln below, here is an update with fixed values replaced by variables. Not sure if it is coded right. it does seem to work though:
dimensions = data.shape
height = int(numpy.floor(dimensions[0]/block_size))
width = int(numpy.floor(dimensions[1]/block_size))
t = data.reshape([height, block_size, width, block_size])
avrgd = numpy.mean(t, axis=(1, 3))
To compute some operation slice by slice in numpy, it is very often useful to reshape your array and use extra axes.
To explain the process we'll use here: you can reshape your array, take the mean, reshape it again and take the mean again.
Here I assume blocksize is 2
t = np.array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 5],])
t = t.reshape([6, 3, 2])
t = np.mean(t, axis=2)
t = t.reshape([3, 2, 3])
np.mean(t, axis=1)
outputs
array([[ 0.5, 2.5, 4.5],
[ 0.5, 2.5, 4.5],
[ 0.5, 2.5, 4.5]])
Now that it's clear how this works, you can do it in one pass only:
t = t.reshape([3, 2, 3, 2])
np.mean(t, axis=(1, 3))
works too (and should be quicker since means are computed only once - I guess). I'll let you substitute height/blocksize, width/blocksize and blocksize accordingly.
See #askewcan nice remark on how to generalize this to any dimension.

Categories

Resources