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

Skip to content

ENH: property if DrawingArea clips children #4473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/matplotlib/offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ class DrawingArea(OffsetBox):
"""
The DrawingArea can contain any Artist as a child. The DrawingArea
has a fixed width and height. The position of children relative to
the parent is fixed. The children can be clipped at the
the parent is fixed. The children can be clipped at the
boundaries of the parent.
"""

Expand All @@ -596,6 +596,19 @@ def __init__(self, width, height, xdescent=0.,

self.dpi_transform = mtransforms.Affine2D()

@property
def clip_children(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs docstring

"""
If the children of this DrawingArea should be clipped
by DrawingArea bounding box.
"""
return self._clip_children

@clip_children.setter
def clip_children(self, val):
self._clip_children = bool(val)
self.stale = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is only stale if the state changes, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but there are only a few checks anywhere else. This problem will either be solved if we go with a managed attributes meta-class (i.e. traitlets) or slowly as it annoys people.


def get_transform(self):
"""
Return the :class:`~matplotlib.transforms.Transform` applied
Expand Down
41 changes: 39 additions & 2 deletions lib/matplotlib/tests/test_offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
unicode_literals)

import nose

from matplotlib.testing.decorators import image_comparison
from nose.tools import assert_true, assert_false
from matplotlib.testing.decorators import image_comparison, cleanup
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
Expand Down Expand Up @@ -45,5 +45,42 @@ def test_offsetbox_clipping():
ax.set_ylim((0, 1))


@cleanup
def test_offsetbox_clip_children():
# - create a plot
# - put an AnchoredOffsetbox with a child DrawingArea
# at the center of the axes
# - give the DrawingArea a gray background
# - put a black line across the bounds of the DrawingArea
# - see that the black line is clipped to the edges of
# the DrawingArea.
fig, ax = plt.subplots()
size = 100
da = DrawingArea(size, size, clip=True)
bg = mpatches.Rectangle((0, 0), size, size,
facecolor='#CCCCCC',
edgecolor='None',
linewidth=0)
line = mlines.Line2D([-size*.5, size*1.5], [size/2, size/2],
color='black',
linewidth=10)
anchored_box = AnchoredOffsetbox(
loc=10,
child=da,
pad=0.,
frameon=False,
bbox_to_anchor=(.5, .5),
bbox_transform=ax.transAxes,
borderpad=0.)

da.add_artist(bg)
da.add_artist(line)
ax.add_artist(anchored_box)

fig.canvas.draw()
assert_false(fig.stale)
da.clip_children = True
assert_true(fig.stale)

if __name__ == '__main__':
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)