Michelle
Michelle

Reputation: 11

Increase resolution of figure for saving

Is there a way to increase the resolution of a figure when saving it using the matplotlib toolbar save button?

I tried increasing the dpi but it doesn't seem to make much of a difference when using the save button on the toolbar.

This is how I was increasing the dpi to what the user specified.

if self.txtDPI.toPlainText() == "":
    DPI = 120 
else: 
    DPI = int(self.txtDPI.toPlainText()) 
self.tempfig.set_dpi(DPI)

I have a GUI that the figure is on and underneath it is the matplotlib toolbar so they can edit the chart. I am trying to get it to save the figure with the set dpi when the user hits the "save" button on the matplotlib toolbar. I thought drawing the figure with the user input dpi would make it save the figure with that dpi but it doesn't. It also makes the chart go off the "canvas" if the user increases the dpi above 120.

EDIT: I got it to work by doing the following:

import matplotlib as mpl

mpl.rcParams['savefig.dpi'] = DPI

Thank you for all your suggestions!

Upvotes: 0

Views: 2068

Answers (1)

Greg
Greg

Reputation: 12234

If you're happy to set the dpi before generating the figure then I would suggest setting the rcParams. This can be done either in a matplotlibrc file, or if you just have one script that you want to increase the dpi then add something like this:

import matplotlib.pyplot as plt
plt.rcParams['savefig.dpi'] = 500

If on the other hand you want to be able to set the dpi when you save the figure, then you will need to extend the interactive matplotlib window. Here is an example of how that can be done in matplotlib alone

EDIT:

An easy way to add the interactivity would be to make use of the IPython interactive widgets. Here is a screenshot of how this could work:

enter image description here

Every time you move the slider, it calls plot with the updated value of dpi, so the figure is resaved. If the figure is particularly large and slow to generate you may want to use interact_manual instead. In order to do this just install the IPython notebook with version greater than 3.0.

Upvotes: 1

Related Questions