19

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

tacaswell
  • 84,579
  • 22
  • 210
  • 199
user1696811
  • 941
  • 4
  • 10
  • 20
  • 3
    Why don't you use a vector graphics format like eps or pdf? – Benjamin Bannier Mar 22 '13 at 16:39
  • 1
    [This question](http://stackoverflow.com/q/332289/875127) deals with changing the size/dpi/resolution of an image, but it sounds like you've tried that. Check it out in-case there's something you missed. – Cianan Sims Mar 22 '13 at 16:41
  • 2
    You should definitely use a vector based format for plots. They are nicely supported by LaTeX. – David Zwicker Mar 22 '13 at 16:54
  • 2
    Can you post a simple bit of code that generates a non-satisfactory image, and the file it generates? Are you generating jpegs? – tacaswell Mar 22 '13 at 18:55

1 Answers1

24

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.

rubenvb
  • 74,642
  • 33
  • 187
  • 332