8000 Fix bug when collection is empty. · mkcor/matplotlib@b3dd8c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3dd8c4

Browse files
committed
Fix bug when collection is empty.
svn path=/branches/transforms/; revision=4577
1 parent 79be499 commit b3dd8c4

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

lib/matplotlib/axes.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from matplotlib import lines as mlines
2222
from matplotlib import mlab
2323
from matplotlib import patches as mpatches
24+
from matplotlib import path as mpath
2425
from matplotlib import quiver as mquiver
2526
from matplotlib import scale as mscale
2627
from matplotlib import table as mtable
@@ -770,13 +771,14 @@ def cla(self):
770771

771772
self.grid(self._gridOn)
772773
props = font_manager.FontProperties(size=rcParams['axes.titlesize'])
773-
self.title = mtext.Text(
774-
x=0.5, y=1.02, text='',
774+
self.titleOffsetTrans = mtransforms.Affine2D()
775+
self.title = mtext.Text(
776+
x=0.5, y=1.00, text='',
775777
fontproperties=props,
776778
verticalalignment='bottom',
777779
horizontalalignment='center',
778780
)
779-
self.title.set_transform(self.transAxes)
781+
self.title.set_transform(self.transAxes + self.titleOffsetTrans)
780782
self.title.set_clip_box(None)
781783

782784
self._set_artist_props(self.title)
@@ -800,6 +802,8 @@ def cla(self):
800802
self.xaxis.set_clip_path(self.axesPatch)
801803
self.yaxis.set_clip_path(self.axesPatch)
802804

805+
self.titleOffsetTrans.clear()
806+
803807
def clear(self):
804808
'clear the axes'
805809
self.cla()
@@ -905,14 +909,14 @@ def get_data_ratio(self):
905909
ysize = max(math.fabs(ymax-ymin), 1e-30)
906910
return ysize/xsize
907911

908-
def apply_aspect(self):
912+
def apply_aspect(self, currentPosition):
909913
'''
910914
Use self._aspect and self._adjustable to modify the
911915
axes box or the view limits.
912916
'''
913917
aspect = self.get_aspect()
914918
if aspect == 'auto':
915-
self.set_position( self._originalPosition , 'active')
919+
self.set_position(currentPosition, 'active')
916920
return
917921

918922
if aspect == 'equal':
@@ -929,7 +933,7 @@ def apply_aspect(self):
929933
fig_aspect = figH/figW
930934
if self._adjustable == 'box':
931935
box_aspect = A * self.get_data_ratio()
932-
pb = self._originalPosition.frozen()
936+
pb = currentPosition.frozen()
933937
pb1 = pb.shrunk_to_aspect(box_aspect, pb, fig_aspect)
934938
self.set_position(pb1.anchored(self.get_anchor(), pb), 'active')
935939
return
@@ -1289,25 +1293,48 @@ def autoscale_view(self, tight=False, scalex=True, scaley=True):
12891293
YL = ylocator.autoscale()
12901294
self.set_ybound(YL)
12911295

1296+
def adjust_for_axis_text(self, renderer):
1297+
pad_pixels = rcParams['xtick.major.pad'] * self.figure.dpi / 72.0
1298+
inverse_transFigure = self.figure.transFigure.inverted()
1299+
t_text, b_text = self.xaxis.get_text_heights(renderer)
1300+
l_text, r_text = self.yaxis.get_text_widths(renderer)
1301+
title_height = self.title.get_window_extent(renderer).height
1302+
title_height += pad_pixels * 2.0
1303+
original_t_text = t_text
1304+
1305+
((l_text, t_text),
1306+
(r_text, b_text),
1307+
(dummy, title_height)) = inverse_transFigure.transform(
1308+
((l_text, t_text),
1309+
(r_text, b_text),
1310+
(0.0, title_height)))
1311+
x0, y0, x1, y1 = self.get_position(True).extents
1312+
# Adjust the title
1313+
self.titleOffsetTrans.clear().translate(
1314+
0, original_t_text + pad_pixels * 2.0)
1315+
return mtransforms.Bbox.from_extents(
1316+
x0 + l_text, y0 + b_text, x1 - r_text,
1317+
y1 - t_text - title_height)
1318+
12921319
#### Drawing
12931320
def draw(self, renderer=None, inframe=False):
12941321
"Draw everything (plot lines, axes, labels)"
1295-
if renderer is None:
1322+
if renderer is None:
12961323
renderer = self._cachedRenderer
12971324

12981325
if renderer is None:
12991326
raise RuntimeError('No renderer defined')
13001327
if not self.get_visible(): return
13011328
renderer.open_group('axes')
1302-
self.apply_aspect()
1329+
1330+
currentPosition = self.adjust_for_axis_text(renderer)
1331+
self.apply_aspect(currentPosition)
13031332

13041333
if self.axison and self._frameon:
13051334
self.axesPatch.draw(renderer)
13061335

13071336
artists = []
13081337

1309-
1310-
13111338
if len(self.images)<=1 or renderer.option_image_nocomposite():
13121339
for im in self.images:
13131340
im.draw(renderer)
@@ -1319,7 +1346,6 @@ def draw(self, renderer=None, inframe=False):
13191346
ims = [(im.make_image(mag),0,0)
13201347
for im in self.images if im.get_visible()]
13211348

1322-
13231349
im = mimage.from_images(self.bbox.height*mag,
13241350
self.bbox.width*mag,
13251351
ims)
@@ -1360,6 +1386,14 @@ def draw(self, renderer=None, inframe=False):
13601386
a.draw(renderer)
13611387

13621388
renderer.close_group('axes')
1389+
1390+
# ### DEBUGGING
1391+
# gc = renderer.new_gc()
1392+
# gc.set_linewidth(2.0)
1393+
# x0, y0, x1, y1 = self.get_position(True).extents
1394+
# renderer.draw_path(gc, mpath.Path(
1395+
# [[x0, y0], [x0, y1], [x1, y1], [x1, y0], [x0, y0]]),
1396+
# self.figure.transFigure)
13631397
self._cachedRenderer = renderer
13641398

13651399
def draw_artist(self, a):

0 commit comments

Comments
 (0)
0