[英]matplotlib.pyplot: create a subplot of stored plots
python 3.6 om mac matplotlib 2.1.0使用matplotlib.pyplot(作為plt)
假設我有一些plt.figures()
,我將其附加到名為figure的列表作為對象。 當在命令行中我做: figures[0]
它產生列表figures
的索引0的figures
。 但是,我如何安排將數字中的所有圖都放在子圖中。
# Pseudo code:
plt.figure()
for i, fig in enumerate(figures): # figures contains the plots
plt.subplot(2, 2, i+1)
fig # location i+1 of the subplot is filled with the fig plot element
因此,我會得到一個2乘2的網格,其中包含圖中的每個圖。
希望這是有道理的。
圖是一個數字。 你不能在圖中有一個數字。 通常的方法是創建一個圖形,創建一個或多個子圖,在子圖中繪制一些東西。
如果你想要在不同的軸或圖中繪制某些東西,可能有必要將繪圖包裝在一個以軸為參數的函數中。
然后,您可以使用此函數繪制到新圖形的軸,或繪制到具有許多子圖的圖形軸。
import numpy as np
import matplotlib.pyplot as plt
def myplot(ax, data_x, data_y, color="C0"):
ax.plot(data_x, data_y, color=color)
ax.legend()
x = np.linspace(0,10)
y = np.cumsum(np.random.randn(len(x),4), axis=0)
#create 4 figures
for i in range(4):
fig, ax = plt.subplots()
myplot(ax, x, y[:,i], color="C{}".format(i))
# create another figure with each plot as subplot
fig, ax = plt.subplots(2,2)
for i in range(4):
myplot(ax.flatten()[i], x, y[:,i], color="C{}".format(i))
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.