8000 Fix scaling of regular polygons · matplotlib/matplotlib@1a774c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a774c9

Browse files
committed
Fix scaling of regular polygons
1 parent 711bacb commit 1a774c9

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

lib/matplotlib/collections.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,8 @@ class _CollectionWithSizes(Collection):
725725
"""
726726
Base class for collections that have an array of sizes.
727727
"""
728+
_factor = 1.0
729+
728730
def get_sizes(self):
729731
"""
730732
Returns the sizes of the elements in the collection. The
@@ -756,7 +758,7 @@ def set_sizes(self, sizes, dpi=72.0):
756758
else:
757759
self._sizes = np.asarray(sizes)
758760
self._transforms = np.zeros((len(self._sizes), 3, 3))
759-
scale = np.sqrt(self._sizes) * dpi / 72.0
761+
scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor
760762
self._transforms[:, 0, 0] = scale
761763
self._transforms[:, 1, 1] = scale
762764
self._transforms[:, 2, 2] = 1.0
@@ -894,6 +896,8 @@ class RegularPolyCollection(_CollectionWithSizes):
894896
"""Draw a collection of regular polygons with *numsides*."""
895897
_path_generator = mpath.Path.unit_regular_polygon
896898

899+
_factor = 1.0 / np.sqrt(np.pi)
900+
897901
@docstring.dedent_interpd
898902
def __init__(self,
899903
numsides,
@@ -1401,6 +1405,8 @@ class CircleCollection(_CollectionWithSizes):
14011405
"""
14021406
A collection of circles, drawn using splines.
14031407
"""
1408+
_factor = 1.0 / np.sqrt(np.pi)
1409+
14041410
@docstring.dedent_interpd
14051411
def __init__(self, sizes, **kwargs):
14061412
"""
Loading
Loading

lib/matplotlib/tests/test_collections.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,36 @@ def test_regularpolycollection_rotate():
529529
ax.autoscale_view()
530530

531531

532+
@image_comparison(baseline_images=['regularpolycollection_scale'],
533+
extensions=['png'], remove_text=True)
534+
def test_regularpolycollection_scale():
535+
# See issue #3860
536+
537+
class SquareCollection(mcollections.RegularPolyCollection):
538+
def __init__(self, **kwargs):
539+
super(SquareCollection, self).__init__(4, rotation=np.pi/4., **kwargs)
540+
541+
def get_transform(self):
542+
"""Return transform scaling circle areas to data space."""
543+
ax = self.axes
544+
545+
pts2pixels = 72.0 / ax.figure.dpi
546+
547+
scale_x = pts2pixels * ax.bbox.width / ax.viewLim.width
548+
scale_y = pts2pixels * ax.bbox.height / ax.viewLim.height
549+
return mtransforms.Affine2D().scale(scale_x, scale_y)
550+
551+
fig, ax = plt.subplots()
552+
553+
xy = [(0, 0)]
554+
# Unit square has a half-diagonal of `1 / sqrt(2)`, so `pi * r**2` equals...
555+
circle_areas = [np.pi / 2]
556+
squares = SquareCollection(sizes=circle_areas, offsets=xy,
557+
transOffset=ax.transData)
558+
ax.add_collection(squares, autolim=True)
559+
ax.axis([-1, 1, -1, 1])
560+
561+
532562
if __name__ == '__main__':
533563
import nose
534564
nose.runmodule 328F (argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0