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

Skip to content

Commit 7c3c98b

Browse files
committed
ENH: property if DrawingArea clips children
Add proprety `clip_children` to provide user interface if DrawingArea tries to clip it's children.
1 parent fdf1bb5 commit 7c3c98b

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ class DrawingArea(OffsetBox):
570570
"""
571571
The DrawingArea can contain any Artist as a child. The DrawingArea
572572
has a fixed width and height. The position of children relative to
573-
the parent is fixed. The children can be clipped at the
573+
the parent is fixed. The children can be clipped at the
574574
boundaries of the parent.
575575
"""
576576

@@ -596,6 +596,15 @@ def __init__(self, width, height, xdescent=0.,
596596

597597
self.dpi_transform = mtransforms.Affine2D()
598598

599+
@property
600+
def clip_children(self):
601+
return self._clip_children
602+
603+
@clip_children.setter
604+
def clip_children(self, val):
605+
self._clip_children = bool(val)
606+
self.stale = True
607+
599608
def get_transform(self):
600609
"""
601610
Return the :class:`~matplotlib.transforms.Transform` applied

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
unicode_literals)
33

44
import nose
5-
6-
from matplotlib.testing.decorators import image_comparison
5+
from nose.tools import assert_true, assert_false
6+
from matplotlib.testing.decorators import image_comparison, cleanup
77
import matplotlib.pyplot as plt
88
import matplotlib.patches as mpatches
99
import matplotlib.lines as mlines
@@ -45,5 +45,42 @@ def test_offsetbox_clipping():
4545
ax.set_ylim((0, 1))
4646

4747

48+
@cleanup
49+
def test_offsetbox_clip_children():
50+
# - create a plot
51+
# - put an AnchoredOffsetbox with a child DrawingArea
52+
# at the center of the axes
53+
# - give the DrawingArea a gray background
54+
# - put a black line across the bounds of the DrawingArea
55+
# - see that the black line is clipped to the edges of
56+
# the DrawingArea.
57+
fig, ax = plt.subplots()
58+
size = 100
59+
da = DrawingArea(size, size, clip=True)
60+
bg = mpatches.Rectangle((0, 0), size, size,
61+
facecolor='#CCCCCC',
62+
edgecolor='None',
63+
linewidth=0)
64+
line = mlines.Line2D([-size*.5, size*1.5], [size/2, size/2],
65+
color='black',
66+
linewidth=10)
67+
anchored_box = AnchoredOffsetbox(
68+
loc=10,
69+
child=da,
70+
pad=0.,
71+
frameon=False,
72+
bbox_to_anchor=(.5, .5),
73+
bbox_transform=ax.transAxes,
74+
borderpad=0.)
75+
76+
da.add_artist(bg)
77+
da.add_artist(line)
78+
ax.add_artist(anchored_box)
79+
80+
fig.canvas.draw()
81+
assert_false(fig.stale)
82+
da.clip_children = True
83+
assert_true(fig.stale)
84+
4885
if __name__ == '__main__':
4986
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)