11

I'm using Windows XP v3/Python 2.7 with Canopy and Anaconda package managers/editors.

I am using Python/Matplotlib to produce some Bland-Altman plots (statistical scatter plots) for publication.

After processing the data, the plt.show() command opens a new "Figure" window containing the plot, which looks fine.

I want to be able to use the dynamic pan and zoom commands in this window to interactively optimise the appearance of my plot, then save it as it appears in the window as a high resolution press-quality png image (400-600 dpi, 7 x 5 inches).

The default setting for saving images from the "Figure" window appears to be set to screen resolution (800 x 600 pixels), and I cannot find any options in this window which allow me to change these settings.

I've read other posts on this forum which explain how to directly save a plot from Python in higher resolution by using the following commands to manipulate dpi and image size, e.g.:

plt.figure(figsize=(18, 12), dpi=400)
plt.savefig("myplot.png", dpi = 400)

However, this is not the solution that I'm looking for; as I want to be able to modify the plot using the dynamic pan and zoom features of the "Figure" window before saving in a higher resolution than the default screen resolution.

I'd be grateful for your help.

Many thanks in anticipation & Happy New Year.

Dave (UK)

Dave
  • 515
  • 1
  • 8
  • 17
  • I use Spyder and can manipulate the window, pan, zoom, and save without a problem. What is your window viewing application? – Myles Baker Jan 01 '14 at 18:55
  • Hi Myles - I'm running Spyder via Anaconda. I can also pan, zoom and save; but can only save in default resolution (800 x 600 pixels), and can't find a way to change this to higher resolution. – Dave Jan 01 '14 at 22:14
  • This may be silly, but have you tried grabbing the corner of the figure window? – Myles Baker Jan 01 '14 at 23:14

3 Answers3

1

Try this:

Determine how to set width and height using a pixels-to-inches converter, like in the following matplotlib documentation. Then try:

import matplotlib.pyplot as plt
fig = plt.figure(frameon=False)
fig.set_size_inches(width,height)
Myles Baker
  • 3,600
  • 2
  • 19
  • 25
  • Not working for me I'm afraid: still end up with a ~40 Kb png file of 800 x 600 resolution. Have tried putting your above commands in different places in my routine including at the very start and at the end: doesn't seem to make any difference. Would be grateful for any other suggestions. – Dave Jan 02 '14 at 11:50
1

I had this issue in spyder and found changing the value in Preferences > iPython Console > Inline Backend > Resolution changes the resolution when I save figures from the built in window viewing application.

skoeb
  • 805
  • 1
  • 6
  • 13
0

One may register an event upon a key press that would save the figure with some previously given size and dpi. The following uses a class that stores some figsize and dpi and upon pressing t wll change the figure size and dpi of the figure. It will then save this figure and restore the old size and dpi such that the figure on screen remains unchanged.

import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt

fig,ax=plt.subplots()
ax.plot([1,3,1])

class AnySizeSaver():
    def __init__(self, fig=None, figsize=None, dpi=None, filename=None):
        if not fig: fig=plt.gcf()
        self.fig = fig
        if not figsize: figsize=self.fig.get_size_inches()
        self.figsize=figsize
        if not dpi: dpi=self.fig.dpi
        self.dpi=dpi
        if not filename: filename="myplot.png"
        self.filename=filename
        self.cid = self.fig.canvas.mpl_connect("key_press_event", self.key_press)

    def key_press(self, event):
        if event.key == "t":
            self.save()

    def save(self):
        oldfigsize = self.fig.get_size_inches()
        olddpi=self.fig.dpi
        self.fig.set_size_inches(self.figsize)
        self.fig.set_dpi(self.dpi)
        self.fig.savefig(self.filename, dpi=self.dpi)
        self.fig.set_size_inches(oldfigsize, forward=True)
        self.fig.set_dpi(olddpi)
        self.fig.canvas.draw_idle()
        print(fig.get_size_inches())

ass = AnySizeSaver(fig=fig, figsize=(3,3), dpi=600)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712