Python:循环中的子图:第一个面板出现在错误的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17210646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: subplot within a loop: first panel appears in wrong position
提问by russell johnston
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:
我对 Python 相当陌生,并且从更多的 Matlab 角度来看。我正在尝试制作一系列 2 x 5 面板轮廓子图。到目前为止,我的方法是将(在某种程度上)我的 Matlab 代码转换为 Python 并在循环中绘制我的子图。代码的相关部分如下所示:
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:
作为这个论坛的新手,我似乎不允许附上结果图像。但是,通过我在代码中的索引作为“临时”,2 x 5 面板的结果布局是:
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.
也就是说,第一个面板(250)出现在我认为应该是 259 的最后一个位置。251 似乎是我想要放置 250 的地方。它们似乎都按正确的顺序排列,只是循环移动了一个。
I know this will be something very silly, but appreciate any help you can give.
我知道这将是一件非常愚蠢的事情,但感谢您提供的任何帮助。
Thank you in advance.
先感谢您。
采纳答案by Rutger Kassies
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).
布局当然有点乱,但这是因为您当前的设置(figsize、wspace 等)。


回答by David Zwicker
The problem is the indexing subplotis using. Subplots are counted starting with 1!
Your code thus needs to read
问题是subplot正在使用索引。子图从 1 开始计数!您的代码因此需要阅读
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 = 251+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))
Note the change in the line where you calculate temp
请注意计算行的变化 temp
回答by Tim Hoffmann
Basically the same solution as provided by Rutger Kassies, but using a more pythonic syntax:
与Rutger Kassies提供的解决方案基本相同,但使用了更 Pythonic 的语法:
fig, axs = plt.subplots(2,5, figsize=(15, 6), facecolor='w', edgecolor='k')
fig.subplots_adjust(hspace = .5, wspace=.001)
data = np.arange(250, 260)
for ax, d in zip(axs.ravel(), data):
ax.contourf(np.random.rand(10,10), 5, cmap=plt.cm.Oranges)
ax.set_title(str(d))

