在单个循环中用子图绘制多个图形

问题描述:

我正在绘制两个图形,每个图形都有多个子图.我需要在一个循环中执行此操作.当我只有一个数字时,我会这样做:

I'm plotting on two figures and each of these figures have multiple subplots. I need to do this inside a single loop. Here is what I do when I have only one figure:

fig, ax = plt.subplots(nrows=6,ncols=6,figsize=(20, 20))
fig.subplots_adjust(hspace=.5,wspace=0.4)
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

for x in range(1,32):
    plt.subplot(6,6,x)
    plt.title('day='+str(x))
    plt.scatter(x1,y1)
    plt.scatter(x2,y2)
    plt.colorbar().set_label('Distance from ocean',rotation=270)
plt.savefig('Plots/everyday_D color.png')    
plt.close()

现在我知道当你有多个数字时你需要做这样的事情:

Now I know when you have multiple figures you need to do something like this:

fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()

但我不知道如何在循环中绘制,每个子图都在它的位置(因为如果有两个数字,你不能继续做 plt.scatter ).请具体说明我需要做什么(关于是否是fig1.scatter, ax1.scatter, fig.subplots_adjust, ...以及最后如何保存和关闭)

But I don't know how to plot in the loop, that each subplot is in it's place (Because you can't keep doing plt.scatter if there are two figures). Please be specific with what do I need to do (regarding whether it is fig1.scatter, ax1.scatter, fig.subplots_adjust, ... and how to save and close at the end)

每个pyplot函数在面向对象的API中都有其对应的方法.如果您真的想同时遍历两个图形的轴,则如下所示:

Each of the pyplot function has its corresponding method in the object oriented API. If you really want to loop over both figures' axes at the same time, this would look like this:

import numpy as np
import matplotlib.pyplot as plt

x1 = x2 = np.arange(10)
y1 = y2 = c = np.random.rand(10,6)

fig1, axes1 = plt.subplots(nrows=2,ncols=3)
fig1.subplots_adjust(hspace=.5,wspace=0.4)

fig2, axes2 = plt.subplots(nrows=2,ncols=3)
fig2.subplots_adjust(hspace=.5,wspace=0.4)

for i, (ax1,ax2) in enumerate(zip(axes1.flatten(), axes2.flatten())):
    ax1.set_title('day='+str(i))
    ax2.set_title('day='+str(i))
    sc1 = ax1.scatter(x1,y1[:,i], c=c[:,i])
    sc2 = ax2.scatter(x2,y2[:,i], c=c[:,i])
    fig1.colorbar(sc1, ax=ax1)
    fig2.colorbar(sc2, ax=ax2)

plt.savefig("plot.png") 
plt.show()   
plt.close()

在这里循环遍历两个扁平轴数组,例如 ax1ax2matplotlib 轴 绘制到.fig1fig2 是 matplotlib 图形(matplotlib.figure.Figure ).

Here you loop over the two flattened axes arrays, such that ax1 and ax2 are the matplotlib axes to plot to. fig1 and fig2 are matplotlib figures (matplotlib.figure.Figure).

为了同样获得索引,使用了 enumerate .所以这行

In order to obtain an index as well, enumerate is used. So the line

for i, (ax1,ax2) in enumerate(zip(axes1.flatten(), axes2.flatten())):
    # loop code

在这里等同于

for i in range(6):
    ax1 = axes1.flatten()[i]
    ax2 = axes2.flatten()[i]
    # loop code

i = 0
for ax1,ax2 in zip(axes1.flatten(), axes2.flatten()):
    # loop code
    i += 1

两者都写得更长.

在这一点上,您可能会对以下事实感兴趣:尽管使用面向对象API的上述解决方案肯定更通用并且更可取,但是仍然可以使用纯pyplot解决方案.这看起来像

At this point you may be interested in the fact that althought the above solution using the object oriented API is surely more versatile and preferable, a pure pyplot solution still is possible. This would look like

import numpy as np
import matplotlib.pyplot as plt

x1 = x2 = np.arange(10)
y1 = y2 = c = np.random.rand(10,6)

plt.figure(1)
plt.subplots_adjust(hspace=.5,wspace=0.4)

plt.figure(2)
plt.subplots_adjust(hspace=.5,wspace=0.4)

for i in range(6):
    plt.figure(1)
    plt.subplot(2,3,i+1)
    sc1 = plt.scatter(x1,y1[:,i], c=c[:,i])
    plt.colorbar(sc1)

    plt.figure(2)
    plt.subplot(2,3,i+1)
    sc2 = plt.scatter(x1,y1[:,i], c=c[:,i])
    plt.colorbar(sc2)

plt.savefig("plot.png") 
plt.show()   
plt.close()