在一个循环中绘制多个图

问题描述 投票:0回答:2

我试图绘制循环中有一个图,我希望得到两个不同的数字,但是Python只显示一个数字来代替。事实上,它似乎Python中绘制了第一个第二个数字。这是我使用的代码:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,10)
y = np.arange(0,10)

for _ in range(2):
   plt.plot(x,y)
   plt.show()

它值得指出的是,我与Python 2.7 PyCharm的环境中工作。任何样的建议表示赞赏。

python python-2.7 matplotlib pycharm
2个回答
2
投票

尝试以下方法:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,10)
y = np.arange(0,10)

for _ in range(2):
   plt.figure() # add this statement before your plot
   plt.plot(x,y)
   plt.show()

1
投票

这可以这样做:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,10)
y = np.arange(0,10)

f, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot(x, y)
ax2.plot(x, y)
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.