Python:使用 for 循环在一张图中创建多个图
我尝试使用 matplotlib 创建一个 2 行 3 列的图网格(每个图上都绘制了多个数据).但是,无论我怎么尝试,最终保存的图形都只是其中的一个图,其余为空白.我知道另一个是制作的,但它们没有出现在最终图像中.这是我正在尝试的一个基本版本.
I have tried to create a 2 row, 3 column grid of plots (each having multiple data plotted on it) using matplotlib. However, no matter what I try, the final saved figure is just one of the plots, with the rest blank. I know the other were produced, but they are not appearing in the final image. Here is one basic version of what I'm trying.
注释掉的部分显示了我看到的一些替代方案.
The commented out pieces show some alternatives that I have seen.
f,axarr = plt.subplots(2,3, sharex='col', sharey='row')
for i,someargs in enumerate(namelist):
x1,y1,x2,y2 = somefunction(someargs) #output data
ax = axarr.flat[i] #or ax=axarr[row,col]
ax.plot(x1,y1)
ax.plot(x2,y2)
plt.savefig("name")
#or f.savefig("name")
我这样做的方式有问题吗?我得到的图像位于 http://i.imgur.com/QxYRnPT.png任何帮助将不胜感激.
Is there something wrong with the way I am doing this? The image I am getting is located at http://i.imgur.com/QxYRnPT.png Any help would be greatly appreciated.
在花了一些时间仔细查看我正在运行的内容之后,我发现问题可能出在我用来生成数据的函数的某处以及它如何与循环交互.确实,使用基本测试数据没有问题.
After spending some some time closely looking at what I'm running, I've found that the problem might lie somewhere with the function I am using to generate data and how it interacts with the loop. Indeed, using basic test data causes no problem.
该函数做了很多事情(并且需要并行性),所以很难确切地说出它在做什么.
That function does a lot of stuff (and requires parallelism), so it's difficult to tell exactly what it's doing.
我不太确定出了什么问题,但我通过先存储数据,然后以我发布的方式或与其他答案类似的方式访问/绘制数据来解决我的问题.
I'm not really sure what went wrong, but I fixed my problem by storing the data first, and then accessing/plotting it in the manner I posted or similarly to the other answer.