|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +from matplotlib.testing.decorators import cleanup |
| 4 | + |
| 5 | +import matplotlib.pyplot as plt |
| 6 | + |
| 7 | +import matplotlib.patches as mpatches |
| 8 | +import matplotlib.transforms as mtrans |
| 9 | +import matplotlib.collections as mcollections |
| 10 | + |
| 11 | + |
| 12 | +@cleanup |
| 13 | +def test_patch_transform_of_none(): |
| 14 | + # tests the behaviour of patches added to an Axes with various transform |
| 15 | + # specifications |
| 16 | + |
| 17 | + ax = plt.axes() |
| 18 | + ax.set_xlim([1, 3]) |
| 19 | + ax.set_ylim([1, 3]) |
| 20 | + |
| 21 | + #draw an ellipse over data coord (2,2) by specifying device coords |
| 22 | + xy_data = (2, 2) |
| 23 | + xy_pix = ax.transData.transform_point(xy_data) |
| 24 | + |
| 25 | + # not providing a transform of None puts the ellipse in data coordinates |
| 26 | + e = mpatches.Ellipse(xy_data, width=1, height=1, fc='yellow', alpha=0.5) |
| 27 | + ax.add_patch(e) |
| 28 | + assert e._transform == ax.transData |
| 29 | + |
| 30 | + # providing a transform of None puts the ellipse in device coordinates |
| 31 | + e = mpatches.Ellipse(xy_pix, width=120, height=120, fc='coral', |
| 32 | + transform=None, alpha=0.5) |
| 33 | + ax.add_patch(e) |
| 34 | + assert isinstance(e._transform, mtrans.IdentityTransform) |
| 35 | + |
| 36 | + # providing an IdentityTransform puts the ellipse in device coordinates |
| 37 | + e = mpatches.Ellipse(xy_pix, width=100, height=100, |
| 38 | + transform=mtrans.IdentityTransform(), alpha=0.5) |
| 39 | + ax.add_patch(e) |
| 40 | + assert isinstance(e._transform, mtrans.IdentityTransform) |
| 41 | + |
| 42 | + |
| 43 | +@cleanup |
| 44 | +def test_collection_transform_of_none(): |
| 45 | + # tests the behaviour of collections added to an Axes with various |
| 46 | + # transform specifications |
| 47 | + |
| 48 | + ax = plt.axes() |
| 49 | + ax.set_xlim([1, 3]) |
| 50 | + ax.set_ylim([1, 3]) |
| 51 | + |
| 52 | + #draw an ellipse over data coord (2,2) by specifying device coords |
| 53 | + xy_data = (2, 2) |
| 54 | + xy_pix = ax.transData.transform_point(xy_data) |
| 55 | + |
| 56 | + # not providing a transform of None puts the ellipse in data coordinates |
| 57 | + e = mpatches.Ellipse(xy_data, width=1, height=1) |
| 58 | + c = mcollections.PatchCollection([e], facecolor='yellow', alpha=0.5) |
| 59 | + ax.add_collection(c) |
| 60 | + # the collection should be in data coordinates |
| 61 | + assert c.get_offset_transform() + c.get_transform() == ax.transData |
| 62 | + |
| 63 | + # providing a transform of None puts the ellipse in device coordinates |
| 64 | + e = mpatches.Ellipse(xy_pix, width=120, height=120) |
| 65 | + c = mcollections.PatchCollection([e], facecolor='coral', |
| 66 | + alpha=0.5) |
| 67 | + c.set_transform(None) |
| 68 | + ax.add_collection(c) |
| 69 | + assert isinstance(c.get_transform(), mtrans.IdentityTransform) |
| 70 | + |
| 71 | + # providing an IdentityTransform puts the ellipse in device coordinates |
| 72 | + e = mpatches.Ellipse(xy_pix, width=100, height=100) |
| 73 | + c = mcollections.PatchCollection([e], transform=mtrans.IdentityTransform(), |
| 74 | + alpha=0.5) |
| 75 | + ax.add_collection(c) |
| 76 | + assert isinstance(c._transOffset, mtrans.IdentityTransform) |
| 77 | + |
| 78 | + |
| 79 | +if __name__=='__main__': |
| 80 | + import nose |
| 81 | + nose.runmodule(argv=['-s','--with-doctest'], exit=False) |
0 commit comments