Zéz*_*lle 0 matplotlib python-3.x
我想在仅 1 个循环中的两个不同图形中绘制不同的内容(我有一个巨大的矩阵,我不想放置 2 个 for 循环),如下所示:
plt.figure(0)
plt.figure(1)
for i in range(10):
#plot it only on the figure(0)
plt.plot(np.arange(10), np.power(np.arange(10), i), label = 'square')
#plot it only on the figure(1)
plt.plot(np.arange(10), np.power(np.arange(10), 1/i), label = '1/square')
plt.legend() #if it does for both figures seperately
plt.show()
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这个目标?多谢。
在绘制之前,您需要“激活”相应的图形。
plt.figure(0)
plt.figure(1)
for i in range(10):
#plot it only on the figure(0)
plt.figure(0)
plt.plot(np.arange(10), np.power(np.arange(10), i), label = 'square')
#plot it only on the figure(1)
plt.figure(1)
plt.plot(np.arange(10), np.power(np.arange(10), 1/i), label = '1/square')
#legend for figure(0)
plt.figure(0)
plt.legend()
#legend for figure(1)
plt.figure(1)
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)
直接使用对象及其方法。
fig0, ax0 = plt.subplots()
fig1, ax1 = plt.subplots()
for i in range(10):
#plot it only on the fig0
ax0.plot(np.arange(10), np.power(np.arange(10), i), label = 'square')
#plot it only on the fig1
ax1.plot(np.arange(10), np.power(np.arange(10), 1/i), label = '1/square')
ax0.legend()
ax1.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
726 次 |
| 最近记录: |