Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在单个循环中使用子图在多个图形上绘图_Python_Matplotlib_Plot_Subplot - Fatal编程技术网

Python 在单个循环中使用子图在多个图形上绘图

Python 在单个循环中使用子图在多个图形上绘图,python,matplotlib,plot,subplot,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

我在两个图上绘图,每个图都有多个子图。我需要在一个循环内完成这项工作。当我只有一个数字时,我会这样做:

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

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

每个pyplot函数在面向对象的API中都有其相应的方法。如果确实要同时在两个图形的轴上循环,则如下所示:

fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
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()

在这里,您可以在两个展平轴阵列上循环,以便打印到
ax1
ax2
fig1
fig2
是matplotlib图()

为了获得索引,还使用了枚举。那么这条线呢

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

这两个都是更长的写

此时,您可能会对以下事实感兴趣:尽管上述使用面向对象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()

这是一个显示如何在两个不同图形上运行散点图的版本。基本上,您可以参考使用
plt.subplot
创建的轴

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')
当我从post
plt.colorbar()
获取原始代码时,引发了一个错误,因此我在答案中忽略了它。如果您有一个示例,说明了
colorbar
的工作原理,我们可以看看如何实现这一点,但代码的其余部分应该按预期工作


请注意,如果
day
every未出现在
dayRolCol
numpy中,则会引发错误,由您决定如何处理该情况。此外,使用numpy绝对不是唯一的方法,只是一种我很熟悉的方法-你真正需要做的就是找到一种方法,将某一天/绘图与你想要绘图的轴的(x,y)索引相联系。

如果你回答一个已经有答案的问题,最好弄清楚你的答案有多大不同。仅仅有两次相同的解决方案是没有用的。另外,另一个答案显示了如何使用colorbar,因此我认为说“我们”可以看看它是如何工作的是没有意义的——如果你愿意,你自己也可以看看。谢谢你的回答。一个问题是axis1和axis2来自哪里。似乎与您之前介绍的AxeArray1没有任何联系(或者这只是一个错误)?@ImportanceOfBeingErnest您是对的,另一个答案出现在我写我的答案时,直到我看到它after@Drproctor
axis1
是从
axesArray1
创建的,如下所示:
axis1=axesArray1[rowIdx,colIdx]
<代码>axis2也是以类似的方式制作的,谢谢您的回答。我知道枚举基本上是在for循环中创建索引。那么ax1和ax2只是索引吗?您能解释一下本例中对象ax1、axes1和图1的类型吗?
ax1
ax2
是matplotlib轴<代码>枚举是在循环中获取索引的一种简单方法。也许有助于更好地理解枚举。我也更新了答案。