在单个循环中绘制带有子图的多个图形

时间:2017-10-04 20:24:55

标签: python matplotlib plot subplot

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

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()

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

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

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

2 个答案:

答案 0 :(得分:1)

每个pyplot函数在面向对象的API中都有相应的方法。如果你真的想同时循环两个数字的轴,这将是这样的:

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()

这里循环遍历两个展平的轴阵列,这样ax1ax2就是要绘制的matplotlib axesfig1fig2是matplotlib数字(matplotlib.figure.Figure)。

为了获得索引,使用enumerate。这一行

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解决方案仍然是可能的。这看起来像

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()

答案 1 :(得分:1)

这是一个版本,显示如何在两个不同的数字上运行散点图。基本上,您引用了使用plt.subplots创建的轴。

import matplotlib.pyplot as plt
import numpy as np

x1 = y1 = range(10)
x2 = y2 = range(5)

nRows = nCols = 6
fig1, axesArray1 = plt.subplots(nrows=nRows,ncols=nCols,figsize=(20, 20))
fig1.subplots_adjust(hspace=.5,wspace=0.4)
fig1.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

fig2, axesArray2 = plt.subplots(nrows=nRows,ncols=nCols,figsize=(20, 20))
fig2.subplots_adjust(hspace=.5,wspace=0.4)
fig2.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

days = range(1, 32)
dayRowCol = np.array([i + 1 for i in range(nRows * nCols)]).reshape(nRows, nCols)
for day in days:
    rowIdx, colIdx = np.argwhere(dayRowCol == day)[0]

    axis1 = axesArray1[rowIdx, colIdx]
    axis1.set_title('day=' + str(day))
    axis1.scatter(x1, y1)

    axis2 = axesArray2[rowIdx, colIdx]
    axis2.set_title('day=' + str(day))
    axis2.scatter(x2, y2)

    # This didn't run in the original script, so I left it as is
    # plt.colorbar().set_label('Distance from ocean',rotation=270)

fig1.savefig('plots/everyday_D1_color.png')
fig2.savefig('plots/everyday_D2_color.png')
plt.close('all')

当我从帖子plt.colorbar()中取出原始代码时出现错误,所以我把它留在了答案中。如果你有一个关于colorbar如何工作的例子,我们可以看看如何使这两个数字发生,但其余的代码应该按预期工作!

请注意,如果day每次dayRolCol numpy都没有显示错误,则由您自行决定如何处理该情况。另外,使用numpy绝对不是唯一的方法,只是我喜欢的方式 - 你真正需要做的就是找到一种方法将某一天/情节与(x,y)指数联系起来你要绘制的轴。

相关问题