import matplotlib.pyplot as pltfig, ax = plt.subplots( nrows=1, ncols=1 ) # create figure & 1 axisax.plot([0,1,2], [10,20,3])fig.savefig('path/to/save/image/to.png') # save the figure to fileplt.close(fig) # close the figure window
import datetimeimport numpy as npfrom matplotlib.backends.backend_pdf import PdfPagesimport matplotlib.pyplot as plt
# Create the PdfPages object to which we will save the pages:# The with statement makes sure that the PdfPages object is closed properly at# the end of the block, even if an Exception occurs.with PdfPages('multipage_pdf.pdf') as pdf:plt.figure(figsize=(3, 3))plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')plt.title('Page One')pdf.savefig() # saves the current figure into a pdf pageplt.close()
plt.rc('text', usetex=True)plt.figure(figsize=(8, 6))x = np.arange(0, 5, 0.1)plt.plot(x, np.sin(x), 'b-')plt.title('Page Two')pdf.savefig()plt.close()
plt.rc('text', usetex=False)fig = plt.figure(figsize=(4, 5))plt.plot(x, x*x, 'ko')plt.title('Page Three')pdf.savefig(fig) # or you can pass a Figure object to pdf.savefigplt.close()
# We can also set the file's metadata via the PdfPages object:d = pdf.infodict()d['Title'] = 'Multipage PDF Example'd['Author'] = u'Jouni K. Sepp\xe4nen'd['Subject'] = 'How to create a multipage pdf file and set its metadata'd['Keywords'] = 'PdfPages multipage keywords author title subject'd['CreationDate'] = datetime.datetime(2009, 11, 13)d['ModDate'] = datetime.datetime.today()
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4, 5)) # size in inches# use plot(), etc. to create your plot.
# Pick one of the following lines to uncomment# save_file = None# save_file = os.path.join(your_directory, your_file_name)
if save_file:plt.savefig(save_file)plt.close(fig)else:plt.show()
# Saves a PNG file of the current graph to the folder and updates it every time# (nameOfimage, dpi=(sizeOfimage),Keeps_Labels_From_Disappearing)plt.savefig(__file__+".png",dpi=(250), bbox_inches='tight')# Hard coded name: './test.png'
import mpltex
@mpltex.acs_decoratordef myplot():plt.figure()plt.plot(x,y,'b-',lable='xxx')plt.tight_layout(pad=0.5)plt.savefig('xxxx') # the figure format was controlled by the decorator, it can be either eps, or pdf or png....plt.close()