Saving images in Python at a very high quality
Asked Answered
T

6

186

How can I save Python plots at very high quality?

That is, when I keep zooming in on the object saved in a PDF file, why isn't there any blurring?

Also, what would be the best mode to save it in?

png, eps? Or some other? I can't do pdf, because there is a hidden number that happens that mess with Latexmk compilation.

Tactless answered 24/4, 2013 at 4:38 Comment(0)
H
227

If you are using Matplotlib and are trying to get good figures in a LaTeX document, save as an EPS. Specifically, try something like this after running the commands to plot the image:

plt.savefig('destination_path.eps', format='eps')

I have found that EPS files work best and the dpi parameter is what really makes them look good in a document.

To specify the orientation of the figure before saving, simply call the following before the plt.savefig call, but after creating the plot (assuming you have plotted using an axes with the name ax):

ax.view_init(elev=elevation_angle, azim=azimuthal_angle)

Where elevation_angle is a number (in degrees) specifying the polar angle (down from vertical z axis) and the azimuthal_angle specifies the azimuthal angle (around the z axis).

I find that it is easiest to determine these values by first plotting the image and then rotating it and watching the current values of the angles appear towards the bottom of the window just below the actual plot. Keep in mind that the x, y, z, positions appear by default, but they are replaced with the two angles when you start to click+drag+rotate the image.

Humidity answered 24/4, 2013 at 4:55 Comment(6)
@spencerlon2 what if I want to change the orientation of the figure before saving?Tactless
Why do we need the DPI parameter? Isn't the eps a vector format?Hole
Good point - DPI is not needed for vector formats like svg, pdf, epsHumidity
You can omit format='eps' if you like to. In this case the format is inferred from the filename.Harbot
@Hole I think with higher dpi, the image can be magnified a lot more.Burning
dpi is important if the exported plot contains raster elements (images) in addition to vector art. To elaborate, a lower dpi export will contain pixelated raster elements such as embedded images.Carvey
A
122

Just to add my results, also using Matplotlib.

.eps made all my text bold and removed transparency. .svg gave me high-resolution pictures that actually looked like my graph.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# Do the plot code
fig.savefig('myimage.svg', format='svg', dpi=1200)

I used 1200 dpi because a lot of scientific journals require images in 1200 / 600 / 300 dpi, depending on what the image is of. Convert to desired dpi and format in GIMP or Inkscape.

Obviously the dpi doesn't matter since .svg are vector graphics and have "infinite resolution".

Audition answered 16/10, 2014 at 10:33 Comment(3)
The DPI matters a great deal when we use imshow(..) somewhere on the figure. These bitmaps get rasterised down as they get embedded into the SVG. With the default DPI, the results are even worse than what we see on the screen, definitely unsuitable for publications.Telium
I also highly recommend Scour (github.com/scour-project/scour) to reduce the file size of your svg. On the most aggressive setting I can get reduce my file size to 50% of the original - 5% if I use the .svgz compressed format.Audition
Actually dpi = 1200 works with png as well! Amazing, thanks a million :)Oleic
O
12

Okay, I found spencerlyon2's answer working. However, in case anybody would find himself/herself not knowing what to do with that one line, I had to do it this way:

beingsaved = plt.figure()

# Some scatter plots
plt.scatter(X_1_x, X_1_y)
plt.scatter(X_2_x, X_2_y)

beingsaved.savefig('destination_path.eps', format='eps', dpi=1000)
Osteology answered 20/11, 2017 at 11:2 Comment(1)
dpi=1000 is the answer.Shine
S
11

You can save to a figure that is 1920x1080 (or 1080p) using:

fig = plt.figure(figsize=(19.20,10.80))

You can also go much higher or lower. The above solutions work well for printing, but these days you want the created image to go into a PNG/JPG or appear in a wide screen format.

Schwenk answered 6/11, 2019 at 13:9 Comment(2)
You should probably add that this is because (usually) the default value for dpi is 100 and figsize is given in inches. So 19.2 inches with 100 dots per inch gives you 1920 dots in that dimension =DAudition
note that the text isn't scaled up when you make the figsize large, which means that when showing the image at a scale less than 100% the text might be unreadable. It is better to set the figsize (close) to the actual size in your document and then set the dpi higher to increase the resolution of the text and images.Occidentalize
P
9

In case you are working with seaborn plots, instead of Matplotlib, you can save a .png image like this:

Let's suppose you have a matrix object (either Pandas or NumPy), and you want to take a heatmap:

import seaborn as sb

image = sb.heatmap(matrix)   # This gets you the heatmap
image.figure.savefig("C:/Your/Path/ ... /your_image.png")   # This saves it

This code is compatible with the latest version of Seaborn. Other code around Stack Overflow worked only for previous versions.

Another way I like is this. I set the size of the next image as follows:

plt.subplots(figsize=(15,15))

And then later I plot the output in the console, from which I can copy-paste it where I want. (Since Seaborn is built on top of Matplotlib, there will not be any problem.)

Pantheism answered 19/11, 2018 at 11:3 Comment(0)
B
0

If a title gets cut off during saving, add bbox_inches='tight' to your savefig call. That's what happened to me after I found the solution to the first part of the problem in this thread. Thanks for your help :)

Bechtel answered 5/3 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.