我对matplotlib不太熟悉,想在for循环的每次迭代中向这两个不同的绘图中的每一个添加一行。我尝试了下面简化示例中显示的两件事,但是都抛出了错误。
import matplotlib.pyplot as plt
ax1 = plt.figure()
ax2 = plt.figure()
for i in range(5):
## these throw "Can not put single artist in more than one figure" error
plt.plot([0,i], [0,i], figure=ax1)
plt.plot([i,0], [i,0], figure=ax2)
## these throw "'Figure' object has no attribute 'plot'" error
ax1.plot([0,i], [0,i])
ax2.plot([i,0], [i,0])
我无法正确理解您的目标,但是由于使用图形时好像它们是轴一样,您会遇到错误。您需要声明一个属于图形的轴对象,如下所示:
import matplotlib.pyplot as plt
fig1 = plt.figure() #figure object
fig2 = plt.figure()
ax1 = fig1.gca() #axis object
ax2 = fig2.gca()
for i in range(5):
ax1.plot([0,i], [0,i])
ax2.plot([i,0], [i,0])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句