Skip to content

Commit fb11468

Browse files
committed
implement offsetbox.PaddedBox
1 parent cb578e4 commit fb11468

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,89 @@ def get_extent_offsets(self, renderer):
406406

407407

408408

409+
410+
class PaddedBox(OffsetBox):
411+
def __init__(self, child, pad=None, draw_frame=False, patch_attrs=None):
412+
"""
413+
*pad* : boundary pad
414+
415+
.. note::
416+
*pad* need to given in points and will be
417+
scale with the renderer dpi, while *width* and *hight*
418+
need to be in pixels.
419+
"""
420+
421+
super(PaddedBox, self).__init__()
422+
423+
self.pad = pad
424+
self._children = [child]
425+
426+
self.patch = FancyBboxPatch(
427+
xy=(0.0, 0.0), width=1., height=1.,
428+
facecolor='w', edgecolor='k',
429+
mutation_scale=1, #self.prop.get_size_in_points(),
430+
snap=True
431+
)
432+
433+
self.patch.set_boxstyle("square",pad=0)
434+
435+
if patch_attrs is not None:
436+
self.patch.update(patch_attrs)
437+
438+
self._drawFrame = draw_frame
439+
440+
441+
def get_extent_offsets(self, renderer):
442+
"""
443+
update offset of childrens and return the extents of the box
444+
"""
445+
446+
dpicor = renderer.points_to_pixels(1.)
447+
pad = self.pad * dpicor
448+
449+
w, h, xd, yd = self._children[0].get_extent(renderer)
450+
451+
return w + 2*pad, h + 2*pad, \
452+
xd+pad, yd+pad, \
453+
[(0, 0)]
454+
455+
456+
def draw(self, renderer):
457+
"""
458+
Update the location of children if necessary and draw them
459+
to the given *renderer*.
460+
"""
461+
462+
width, height, xdescent, ydescent, offsets = self.get_extent_offsets(renderer)
463+
464+
px, py = self.get_offset(width, height, xdescent, ydescent, renderer)
465+
466+
for c, (ox, oy) in zip(self.get_visible_children(), offsets):
467+
c.set_offset((px+ox, py+oy))
468+
469+
self.draw_frame(renderer)
470+
471+
for c in self.get_visible_children():
472+
c.draw(renderer)
473+
474+
#bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
475+
476+
def update_frame(self, bbox, fontsize=None):
477+
self.patch.set_bounds(bbox.x0, bbox.y0,
478+
bbox.width, bbox.height)
479+
480+
if fontsize:
481+
self.patch.set_mutation_scale(fontsize)
482+
483+
def draw_frame(self, renderer):
484+
# update the location and size of the legend
485+
bbox = self.get_window_extent(renderer)
486+
self.update_frame(bbox)
487+
488+
if self._drawFrame:
489+
self.patch.draw(renderer)
490+
491+
409492
class DrawingArea(OffsetBox):
410493
"""
411494
The DrawingArea can contain any Artist as a child. The DrawingArea

0 commit comments

Comments
 (0)