Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you improve matplotlib image quality?

I am using a python program to produce some data, plotting the data using matplotlib.pyplot and then displaying the figure in a latex file.

I am currently saving the figure as a .png file but the image quality isn't great. I've tried changing the DPI in matplotlib.pyplot.figure(dpi=200) etc but this seems to make little difference. I've also tried using differnet image formats but they all look a little faded and not very sharp.

Has anyone else had this problem?

Any help would be much appreciated

like image 757
user1696811 Avatar asked Mar 22 '13 16:03

user1696811


People also ask

How do I create a high resolution matplotlib plot?

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.

What is matplotlib dpi?

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.

Is there anything better than matplotlib?

Plotly has several advantages over matplotlib. One of the main advantages is that only a few lines of codes are necessary to create aesthetically pleasing, interactive plots. The interactivity also offers a number of advantages over static matplotlib plots: Saves time when initially exploring your dataset.

How do I save a figure in high resolution Python?

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.


1 Answers

You can save the images in a vector format so that they will be scalable without quality loss. Such formats are PDF and EPS. Just change the extension to .pdf or .eps and matplotlib will write the correct image format. Remember LaTeX likes EPS and PDFLaTeX likes PDF images. Although most modern LaTeX executables are PDFLaTeX in disguise and convert EPS files on the fly (same effect as if you included the epstopdf package in your preamble, which may not perform as well as you'd like).

Alternatively, increase the DPI, a lot. These are the numbers you should keep in mind:

  • 300dpi: plain paper prints
  • 600dpi: professional paper prints. Most commercial office printers reach this in their output.
  • 1200dpi: professional poster/brochure grade quality.

I use these to adapt the quality of PNG figures in conjunction with figure's figsize option, which allows for correctly scaled text and graphics as you improve the quality through dpi.

like image 148
rubenvb Avatar answered Sep 23 '22 05:09

rubenvb