Save Figure in High Resolution in Matplotlib We can plot figures in high resolutions by setting high values of dpi in matplotlib. pyplot. savefig() function. We can control the resolution of the saved figure through dpi parameter in savefig() function.
To get a high-quality image, we can use . eps image format. You can increase the dot per inch value, i.e., dpi. Using savefig() method, we can save the image locally.
The dpi method of figure module of matplotlib library is the resolution in dots per inch. Syntax: fig.dpi. Parameters: This method does not accept any parameters. Returns: This method returns resolution in dots per inch.
Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.
You can use savefig()
to export to an image file:
plt.savefig('filename.png')
In addition, you can specify the dpi
argument to some scalar value, for example:
plt.savefig('filename.png', dpi=300)
Use plt.figure(dpi=1200)
before all your plt.plot...
and at the end use plt.savefig(...)
see: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figure
and
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.savefig
You can save your graph as svg for a lossless quality:
import matplotlib.pylab as plt
x = range(10)
plt.figure()
plt.plot(x,x)
plt.savefig("graph.svg")
For future readers who found this question while trying to save high resolution images from matplotlib as I am, I have tried some of the answers above and elsewhere, and summed them up here.
Best result: plt.savefig('filename.pdf')
and then converting this pdf to a png on the command line so you can use it in powerpoint:
pdftoppm -png -r 300 filename.pdf filename
OR simply opening the pdf and cropping to the image you need in adobe, saving as a png and importing the picture to powerpoint
Less successful test #1: plt.savefig('filename.png', dpi=300)
This does save the image at a bit higher than the normal resolution, but it isn't high enough for publication or some presentations. Using a dpi value of up to 2000 still produced blurry images when viewed close up.
Less successful test #2: plt.savefig('filename.pdf')
This cannot be opened in Microsoft Office Professional Plus 2016 (so no powerpoint), same with Google Slides.
Less successful test #3: plt.savefig('filename.svg')
This also cannot be opened in powerpoint or Google Slides, with the same issue as above.
Less successful test #4: plt.savefig('filename.pdf')
and then converting to png on the command line:
convert -density 300 filename.pdf filename.png
but this is still too blurry when viewed close up.
Less successful test #5: plt.savefig('filename.pdf')
and opening in GIMP, and exporting as a high quality png (increased the file size from ~100 KB to ~75 MB)
Less successful test #6: plt.savefig('filename.pdf')
and then converting to jpeg on the command line:
pdfimages -j filename.pdf filename
This did not produce any errors but did not produce an output on Ubuntu even after changing around several parameters.
For saving the graph:
matplotlib.rcParams['savefig.dpi'] = 300
For displaying the graph when you use plt.show()
:
matplotlib.rcParams["figure.dpi"] = 100
Just add them at the top
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With