How to Save a Plot to a File Using Matplotlib?
Matplotlib is a widely used python library to plot graphs, plots, charts, etc. show() method is used to display graphs as output, but don’t save it in any file. To save generated graphs in a file on storage disk, savefig() method is used.
savefig() : Save the current figure.
Syntax: pyplot.savefig(fname, dpi=None, facecolor=’w’, edgecolor=’w’, orientation=’portrait’, papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None, metadata=None)
- fname : path or name of output file with extension. If extension is not provided plot is saved as png file. Supported file formats: eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff.
- dpi : dots per inch resolution of figure
- facecolor : facecolor of figure
- edgecolor : edgecolor of figure
- orientation : landscape or portrait
- format : The file format, e.g. ‘png’, ‘pdf’, ‘svg’, etc.
- transparent : If it is True, the patches of axes will all be transparent
- Plot a graph
- Use pyplot.savefig() method to save generated plot in a file
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course