draw_event does not fire for savefig() when installing from source

Hi,

I'm using the method described on http://matplotlib.sourceforge.net/faq/howto_faq.html#howto-auto-adjust to automatically adjust my subplot figures to make room for my axis labels. I'm not drawing my graphs to the screen, but I'm instead outputting them to PNG files using a savefig("filename.png", dpi=100) call.

I'm on a Ubuntu 9.04 Jaunty Jackalope system, so I originally used the python-matplotlib package for convenience, which contains matplotlib 0.98.5.2. However, I later also needed the option to move my legend on top of my figure, which means that I had to upgrade to at least matplotlib 0.99.x since I would need the bbox_to_anchor kwarg for that. Naturally nothing higher than 0.98 is available for Ubuntu 9.04, so I went and installed MPL from source. That worked, until I discovered that now the draw_event no longer fires when I call savefig(). It still fires for show(), but for some reason, after installing from source, it no longer does. I made sure that I installed all of MPL's dependencies; the build report lists version numbers for all them where it didn't before, so I'm pretty sure those are all satisfied.

Here's the test program I used, adapted directly from the sample code. I also tried adding in manual fig.canvas.draw() calls to try and trigger the event manually, which seemingly are all happily ignored.

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.set_yticks((2,5,7))
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))

def on_draw(event):
    print "on_draw"
    bboxes = []
    for label in labels:
        bbox = label.get_window_extent()
        # the figure transform goes from relative coords->pixels and we
        # want the inverse of that
        bboxi = bbox.inverse_transformed(fig.transFigure)
        bboxes.append(bboxi)

    # this is the bbox that bounds all the bboxes, again in relative
    # figure coords
    bbox = mtransforms.Bbox.union(bboxes)
    if fig.subplotpars.left < bbox.width:
        # we need to move it over
        fig.subplots_adjust(left=1.1*bbox.width) # pad a little
        fig.canvas.draw()
    return False

fig.canvas.mpl_connect('draw_event', on_draw)

#plt.show() # this fires draw_event
plt.savefig('delete_me.png', dpi=100) # this doesn't

Convinced this was a bug, I then tried compiling a whole bunch of different versions from source to see what version it stopped working at. It didn't work for any of them, including the 0.98.5.x builds.
Finally, I tried grabbing python-matplotlib's matplotlibrc file and using it to override the compiled version's RC file with, and lo: draw_event fires again.

I'm not very familiar with MPL's internals so I'm sure there's a reason for this (one thing I immediately noticed was that the DEB RC file seems to use the TkAgg backend, whereas the compiled version uses the GTKAgg backend). However, it feels to me like whether or not a "user-mode" event fires or not should not depend on some configuration settings.

I've yet to figure out which setting exactly causes draw_event to fail, but I just wanted to let you guys know; might be a good idea to tweak the default settings so that draw_event behaves as expected.

Cheers,
Jeroen DR

I wonder if this is backend-dependent. Which backend are you using for the installs from source?

Ben Root

···

On Thu, Apr 7, 2011 at 10:41 AM, Jeroen DR <voetsjoeba+mplusers@…287…> wrote:

Hi,

I’m using the method described on

http://matplotlib.sourceforge.net/faq/howto_faq.html#howto-auto-adjust

to automatically adjust my subplot figures to make room for my axis

labels. I’m not drawing my graphs to the screen, but I’m instead

outputting them to PNG files using a savefig(“filename.png”, dpi=100) call.

I’m on a Ubuntu 9.04 Jaunty Jackalope system, so I originally used the

python-matplotlib package for convenience, which contains matplotlib

0.98.5.2. However, I later also needed the option to move my legend on

top of my figure, which means that I had to upgrade to at least

matplotlib 0.99.x since I would need the bbox_to_anchor kwarg for that.

Naturally nothing higher than 0.98 is available for Ubuntu 9.04, so I

went and installed MPL from source. That worked, until I discovered that

now the draw_event no longer fires when I call savefig(). It still fires

for show(), but for some reason, after installing from source, it no

longer does. I made sure that I installed all of MPL’s dependencies; the

build report lists version numbers for all them where it didn’t before,

so I’m pretty sure those are all satisfied.

Here’s the test program I used, adapted directly from the sample code. I

also tried adding in manual fig.canvas.draw() calls to try and trigger

the event manually, which seemingly are all happily ignored.

import matplotlib.pyplot as plt

import matplotlib.transforms as mtransforms

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot(range(10))

ax.set_yticks((2,5,7))

labels = ax.set_yticklabels((‘really, really, really’, ‘long’, ‘labels’))

def on_draw(event):

print "on_draw"

bboxes = []

for label in labels:

    bbox = label.get_window_extent()

    # the figure transform goes from relative coords->pixels and we

    # want the inverse of that

    bboxi = bbox.inverse_transformed(fig.transFigure)

    bboxes.append(bboxi)



# this is the bbox that bounds all the bboxes, again in relative

# figure coords

bbox = mtransforms.Bbox.union(bboxes)

if fig.subplotpars.left < bbox.width:

    # we need to move it over

    fig.subplots_adjust(left=1.1*bbox.width) # pad a little

    fig.canvas.draw()

return False

fig.canvas.mpl_connect(‘draw_event’, on_draw)

#plt.show() # this fires draw_event

plt.savefig(‘delete_me.png’, dpi=100) # this doesn’t

Convinced this was a bug, I then tried compiling a whole bunch of

different versions from source to see what version it stopped working

at. It didn’t work for any of them, including the 0.98.5.x builds.

Finally, I tried grabbing python-matplotlib’s matplotlibrc file and

using it to override the compiled version’s RC file with, and lo:

draw_event fires again.

I’m not very familiar with MPL’s internals so I’m sure there’s a reason

for this (one thing I immediately noticed was that the DEB RC file seems

to use the TkAgg backend, whereas the compiled version uses the GTKAgg

backend). However, it feels to me like whether or not a “user-mode”

event fires or not should not depend on some configuration settings.

I’ve yet to figure out which setting exactly causes draw_event to fail,

but I just wanted to let you guys know; might be a good idea to tweak

the default settings so that draw_event behaves as expected.

Cheers,

Jeroen DR