我有这个简单的代码,它在两个不同的数字(图 1 和图 2)中绘制完全相同的东西。但是,我必须写两次 ax?.plot(x, y) 行,一次用于 ax1,一次用于 ax2。我怎么能只有一个情节表达式(有多个冗余的可能是我更复杂的代码的麻烦来源)。像 ax1,ax2.plot(x, y) ... 之类的东西?
import numpy as np
import matplotlib.pyplot as plt
#Prepares the data
x = np.arange(5)
y = np.exp(x)
#plot fig1
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
#plot fig2
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
#adds the same fig2 plot on fig1
ax1.plot(x, y)
ax2.plot(x, y)
plt.show()