Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: subplot within a loop: first panel appears in wrong position

I am fairly new to Python and come from a more Matlab point of view. I am trying to make a series of 2 x 5 panel contourf subplots. My approach so far has been to convert (to a certain degree) my Matlab code to Python and plot my subplots within a loop. The relavent part of the code looks like this:

fig=plt.figure(figsize=(15, 6),facecolor='w', edgecolor='k') for i in range(10):      #this part is just arranging the data for contourf      ind2 = py.find(zz==i+1)     sfr_mass_mat = np.reshape(sfr_mass[ind2],(pixmax_x,pixmax_y))     sfr_mass_sub = sfr_mass[ind2]     zi = griddata(massloclist, sfrloclist, sfr_mass_sub,xi,yi,interp='nn')       temp = 250+i  # this is to index the position of the subplot     ax=plt.subplot(temp)     ax.contourf(xi,yi,zi,5,cmap=plt.cm.Oranges)     plt.subplots_adjust(hspace = .5,wspace=.001)      #just annotating where each contour plot is being placed     ax.set_title(str(temp)) 

As a newbie to this forum, i dont seem to be allowed to attach the resulting image. However, going by my indexing in the code as 'temp' the resulting layout of the the 2 x 5 panels is:

251 - 252 - 253 - 254 - 255 256 - 257 - 258 - 259 - 250 

However, what i want is

250 - 251 - 252 - 253 - 254 255 - 256 - 257 - 258 - 259  

That is, the first panel (250) appears in the last position where i would think 259 should be. And 251 seems to be where i want 250 to be placed. They all seem to be in the correct sequence, just circularly shifted by one.

I know this will be something very silly, but appreciate any help you can give.

Thank you in advance.

like image 218
russell johnston Avatar asked Jun 20 '13 09:06

russell johnston


People also ask

How do you plot a subplot in a loop?

Create a figure and a set of subplots with number of rows = 3 and number of columns = 2. Make a function to iterate the columns of each row and plot the x data points using plot() method at each column index. Iterate rows (Step 2) and create random x data points and call iterate_columns() function (Step 3).

How do you change the subplot in Python?

Using subplots_adjust() function subplots_adjust() method changes the space between subplots. By using parameter wspace we can adjust the width of white space between the subplots (called Padding). Sometimes we refer wspace also as the width of the subplots.

How do subplots work in Python?

subplots to create figure and multiple axes (most useful) Rather than creating a single axes, this function creates a full grid of equal-sized axes in a single line, returning them in a NumPy array. You need to specify the no of rows and columns as an argument to the subplots() function.


1 Answers

Using your code with some random data, this would work:

fig, axs = plt.subplots(2,5, figsize=(15, 6), facecolor='w', edgecolor='k') fig.subplots_adjust(hspace = .5, wspace=.001)  axs = axs.ravel()  for i in range(10):      axs[i].contourf(np.random.rand(10,10),5,cmap=plt.cm.Oranges)     axs[i].set_title(str(250+i)) 

The layout is off course a bit messy, but that's because of your current settings (the figsize, wspace etc).

enter image description here

like image 104
Rutger Kassies Avatar answered Sep 27 '22 18:09

Rutger Kassies