假设我有一个非常大的数据集,需要很长时间来绘制。我想将它的两个数字保存在不同的范围内。在 GUI 中,我只需绘制一次并放大到所需的范围,然后重复以获得其他范围。如何在代码中自动执行此操作?
import numpy as np
import matplotlib.pyplot as plt
# Generate data
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
# Create the first figure
plt.figure("fig1")
plt.scatter(x, y) # suppose plot this will take a long time
plt.ylim(-1, 0)
plt.title("Figure 1: Sin(x) with y-range (-1, 0)")
# Save the first figure
plt.savefig("figure1.png")
# Create the second figure
plt.figure("fig2")
plt.scatter(x, y) # is it possible to avoid this second plot?
plt.ylim(0, 1)
plt.title("Figure 2: Sin(x) with y-range (0, 1)")
# Save the second figure
plt.savefig("figure2.png")
# Show the plots
plt.show()
matplotlib.pyplot
默认情况下保存以前绘制的数据,因此您无需重新绘制或重新创建图形
import numpy as np
import matplotlib.pyplot as plt
# Generate data
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
# Create the first figure
plt.figure("fig1")
plt.scatter(x, y) # suppose plot this will take a long time
plt.ylim(-1, 0)
plt.title("Figure 1: Sin(x) with y-range (-1, 0)")
# Save the first figure
plt.savefig("figure1.png")
plt.ylim(0, 1)
plt.title("Figure 2: Sin(x) with y-range (0, 1)")
# Save the second figure
plt.savefig("figure2.png")
# Show the plots
plt.show()