Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e8b1be

Browse files
committed
updated the autotick label faq to use bboxes and transforms rather than iteration
svn path=/trunk/matplotlib/; revision=5673
1 parent c371027 commit 5e8b1be

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

doc/faq/howto_faq.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ connecting
112112
get the window extent there, and then do something with it, eg move
113113
the left of the canvas over; see :ref:`event-handling-tutorial`.
114114

115-
Here is a recursive, iterative solution that will gradually move the
116-
left of the subplot over until the label fits w/o going outside the
117-
figure border (requires matplotlib 0.98)
115+
Here is that gets a bounding box in relative figure coordinates (0..1)
116+
of each of the labels and uses it to move the left of the subplots
117+
over so that the tick labels fit in the figure
118118

119119
.. plot:: auto_subplots_adjust.py
120120
:include-source:
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import matplotlib.pyplot as plt
2-
2+
import matplotlib.transforms as mtransforms
33
fig = plt.figure()
44
ax = fig.add_subplot(111)
55
ax.plot(range(10))
66
ax.set_yticks((2,5,7))
77
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))
88

99
def on_draw(event):
10+
bboxes = []
1011
for label in labels:
1112
bbox = label.get_window_extent()
12-
if bbox.xmin<0:
13-
fig.subplots_adjust(left=1.1*fig.subplotpars.left)
14-
fig.canvas.draw()
15-
break
13+
# the figure transform goes from relative coords->pixels and we
14+
# want the inverse of that
15+
bboxi = bbox.inverse_transformed(fig.transFigure)
16+
bboxes.append(bboxi)
17+
18+
# this is the bbox that bounds all the bboxes, again in relative
19+
# figure coords
20+
bbox = mtransforms.Bbox.union(bboxes)
21+
if fig.subplotpars.left < bbox.width:
22+
# we need to move it over
23+
fig.subplots_adjust(left=1.1*bbox.width) # pad a little
24+
fig.canvas.draw()
25+
return False
1626

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

1929
plt.show()
30+

0 commit comments

Comments
 (0)