Skip to content Skip to sidebar Skip to footer

Matplotlib Shows Different Figure Than Saves From The Show() Window

I plot rather complex data with matplotlib's imshow(), so I prefer to first visually inspect if it is all right, before saving. So I usually call plt.show(), see if it is fine, and

Solution 1:

When you save a figure in png/jpg you are forced to rasterize it, convert it to a finite number of pixels. If you want to keep the full resolution, you have a few options:

  • Use a very high dpi parameter, like 900. Saving the plot will be slow, and many image viewers will take some time to open it, but the information is there and you can always crop it.
  • Save the image data, the exact numbers you used to make the plot. Whenever you need to inspect it, load it in Matplotlib in interactive mode, navigate to your desired corner, and save it.
  • Use SVG: it is a vector graphics format, so you are not limited to pixels.

Here is how to use SVG:

import matplotlib
matplotlib.use('SVG')
import matplotlib.pyplot as plt

# Generate the image
plt.imshow(image, interpolation='none')
plt.savefig('output_image')

Edit:

To save a true SVG you need to use the SVG backend from the beginning, which is unfortunately, incompatible with interactive mode. Some backends, like GTKCairo seem to allow both, but the result is still rasterized, not a true SVG.

This may be a bug in matplotlib, at least, to the best of my knowledge, it is not documented.

Post a Comment for "Matplotlib Shows Different Figure Than Saves From The Show() Window"