While I don't know much about how callbacks work in matplotlib,
looking at the source code of figure.draw() method, it seems that
call_back functions connected with the "draw_event" are called after a
figure is drawn. Therefore, you need a second draw. My guess is that
this second draw is somehow automagically done in gui backends.
So, a quick and easy solution would be to save the same figure twice.
First one will have a figure before the adjustment, but the second one
will have a figure after the adjustment.
Or, you may create a custom figure class which overrides the draw()
method to adjust the margins before you draw the actual figure.
Attached is an example of such custom figure class. I slightly
modified the original example.
-JJ
On Thu, Aug 21, 2008 at 12:53 PM, Mathieu Leplatre <[EMAIL PROTECTED]> wrote:
> Hi all,
> I am trying to automatically adjust margins with the SVG backend.
>
> The FAQ example :
> http://matplotlib.sourceforge.net/doc/html/faq/howto_faq.html#how-do-i-automatically-make-room-for-my-tick-labels
> only works with GUI backends.
> May it come from get_window_extent() call ?
>
> How could I modify this snipplet so that it works with SVG (or PDF) backends ?
>
> Thank you for your support.
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import matplotlib.figure as mfigure
class MyFigure(mfigure.Figure):
def _adjust_subplotleft(self, renderer):
bboxes = []
for ax in self.get_axes():
if not ax.get_visible(): continue
for label in ax.get_yticklabels():
if not label.get_visible(): continue
bbox = label.get_window_extent(renderer=renderer)
# the figure transform goes from relative coords->pixels and we
# want the inverse of that
bboxi = bbox.inverse_transformed(self.transFigure)
bboxes.append(bboxi)
# this is the bbox that bounds all the bboxes, again in relative
# figure coords
if bboxes:
bbox = mtransforms.Bbox.union(bboxes)
if self.subplotpars.left < bbox.width:
# we need to move it over
self.subplots_adjust(left=1.1*bbox.width) # pad a little
def draw(self, renderer):
if renderer:
subplot_left_orig = self.subplotpars.left
self._adjust_subplotleft(renderer)
mfigure.Figure.draw(self, renderer)
if renderer:
self.subplots_adjust(left=subplot_left_orig)
fig = plt.figure(1, FigureClass=MyFigure)
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.set_yticks((2,5,7))
labels = ax.set_yticklabels(('really, really, really, really long', 'long', 'labels'))
plt.savefig("test")
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users