Saving plots to disk
It is often necessary to save the results of plots in permanent storage. The reasons for that include exporting plots to other software, sharing the graphs, and creating posters or presentations. Another reason to save graphs is to preserve different versions of the same plot.
Getting ready
Start Jupyter and run the following three commands in an execution cell:
%matplotlib inline import numpy as np import matplotlib.pyplot as plt
How to do it…
To save a plot to disk, we use the savefig()
function, as illustrated in the following code segment:
plt.figure(figsize=(4,4)) tvalues = np.linspace(-np.pi, np.pi, 500) xvalues = 2*np.cos(tvalues)-np.cos(2*tvalues) yvalues = 2*np.sin(tvalues)-np.sin(2*tvalues) plt.plot(xvalues, yvalues, color='brown') plt.axhline(0, color='black', lw=1) plt.axvline(0, color='black', lw=1) plt.title('Cardioid') plt.savefig('cardioid.png') None
How it works…
In this code, we first create a new figure and set its size. Then, the tvalues
, xvalues
, and yvalues...