|
From: <jd...@us...> - 2008-06-25 21:43:10
|
Revision: 5673
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5673&view=rev
Author: jdh2358
Date: 2008-06-25 14:43:09 -0700 (Wed, 25 Jun 2008)
Log Message:
-----------
updated the autotick label faq to use bboxes and transforms rather than iteration
Modified Paths:
--------------
trunk/matplotlib/doc/faq/howto_faq.rst
trunk/matplotlib/doc/pyplots/auto_subplots_adjust.py
Modified: trunk/matplotlib/doc/faq/howto_faq.rst
===================================================================
--- trunk/matplotlib/doc/faq/howto_faq.rst 2008-06-25 14:37:23 UTC (rev 5672)
+++ trunk/matplotlib/doc/faq/howto_faq.rst 2008-06-25 21:43:09 UTC (rev 5673)
@@ -112,9 +112,9 @@
get the window extent there, and then do something with it, eg move
the left of the canvas over; see :ref:`event-handling-tutorial`.
-Here is a recursive, iterative solution that will gradually move the
-left of the subplot over until the label fits w/o going outside the
-figure border (requires matplotlib 0.98)
+Here is that gets a bounding box in relative figure coordinates (0..1)
+of each of the labels and uses it to move the left of the subplots
+over so that the tick labels fit in the figure
.. plot:: auto_subplots_adjust.py
:include-source:
Modified: trunk/matplotlib/doc/pyplots/auto_subplots_adjust.py
===================================================================
--- trunk/matplotlib/doc/pyplots/auto_subplots_adjust.py 2008-06-25 14:37:23 UTC (rev 5672)
+++ trunk/matplotlib/doc/pyplots/auto_subplots_adjust.py 2008-06-25 21:43:09 UTC (rev 5673)
@@ -1,5 +1,5 @@
import matplotlib.pyplot as plt
-
+import matplotlib.transforms as mtransforms
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
@@ -7,13 +7,24 @@
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))
def on_draw(event):
+ bboxes = []
for label in labels:
bbox = label.get_window_extent()
- if bbox.xmin<0:
- fig.subplots_adjust(left=1.1*fig.subplotpars.left)
- fig.canvas.draw()
- break
+ # 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 was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|