<p>我建议创造3个数字。一个用于显示,2个用于保存和打印相同的数据。在</p>
<pre><code>import matplotlib.pyplot as plt
import numpy as np
data = np.sort(np.cumsum(np.random.rand(24,16), axis=0), axis=0)
def plot(ax, x, y, **kwargs):
ax.plot(x,y, **kwargs)
colors = ["crimson", "indigo", "limegreen", "gold"]
markers = ["o", "", "s", ""]
lines = ["", "-", "", ":"]
# figure 0 for showing
fig0, axes = plt.subplots(nrows=2,ncols=4)
for i, ax in enumerate(axes.flatten()):
plot(ax, data[:,2*i], data[:,2*i+1], marker=markers[i%4], ls=lines[i%4],color=colors[i%4])
# figure 1 for saving
fig1, axes = plt.subplots(nrows=1,ncols=4)
for i, ax in enumerate(axes.flatten()):
plot(ax, data[:,2*i], data[:,2*i+1], marker=markers[i], ls=lines[i],color=colors[i])
#figure 2 for saving
fig2, axes = plt.subplots(nrows=1,ncols=4)
for i, ax in enumerate(axes.flatten()):
plot(ax, data[:,2*i+4], data[:,2*i+1+4], marker=markers[i], ls=lines[i],color=colors[i])
#save figures 1 and 2
fig1.savefig(__file__+"1.pdf")
fig2.savefig(__file__+"2.pdf")
#close figures 1 and 2
plt.close(fig1)
plt.close(fig2)
#only show figure 0
plt.show()
</code></pre>