Suppose I have one data set which is very big, it takes a long time to plot. I want to save two figures of it in different range. In the GUI, I just plot once and zoom in to the desired range, and repeat to get the other range. How to do it in automatically the code?
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
by default saves previously plotted data, so you do not need to replot or re-create the figure
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()