diff --git a/doc/api/next_api_changes/deprecations/20777-JMK.rst b/doc/api/next_api_changes/deprecations/20777-JMK.rst new file mode 100644 index 000000000000..6214205f966f --- /dev/null +++ b/doc/api/next_api_changes/deprecations/20777-JMK.rst @@ -0,0 +1,7 @@ +dpi keyword argument of collections.set_sizes is removed +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The *dpi* argument of `.PathCollection.set_sizes`, `.PolyCollection.set_sizes`, +`.RegularPolyCollection.set_sizes`, `.CircleCollection.set_sizes` no longer has +an effect. The dpi is determined by *dpi_scale_trans* property of `.FigureBase`, +which works with subfigures. \ No newline at end of file diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 736b1f016331..e490b7c907f3 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -947,6 +947,7 @@ def get_sizes(self): """ return self._sizes + @_api.delete_parameter("3.5", "dpi") def set_sizes(self, sizes, dpi=72.0): """ Set the sizes of each member of the collection. @@ -956,8 +957,6 @@ def set_sizes(self, sizes, dpi=72.0): sizes : ndarray or None The size to set for each element of the collection. The value is the 'area' of the element. - dpi : float, default: 72 - The dpi of the canvas. """ if sizes is None: self._sizes = np.array([]) @@ -965,6 +964,10 @@ def set_sizes(self, sizes, dpi=72.0): else: self._sizes = np.asarray(sizes) self._transforms = np.zeros((len(self._sizes), 3, 3)) + dpi = 72.0 + if self.figure is not None: + trans = self.figure.dpi_scale_trans + dpi = trans.transform(np.array([1, 1]))[0] scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor self._transforms[:, 0, 0] = scale self._transforms[:, 1, 1] = scale @@ -973,7 +976,7 @@ def set_sizes(self, sizes, dpi=72.0): @artist.allow_rasterization def draw(self, renderer): - self.set_sizes(self._sizes, self.figure.dpi) + self.set_sizes(self._sizes) super().draw(renderer) @@ -1335,7 +1338,7 @@ def get_rotation(self): @artist.allow_rasterization def draw(self, renderer): - self.set_sizes(self._sizes, self.figure.dpi) + self.set_sizes(self._sizes) self._transforms = [ transforms.Affine2D(x).rotate(-self._rotation).get_matrix() for x in self._transforms diff --git a/lib/matplotlib/tests/baseline_images/test_figure/test_subfigure_scatter_size.png b/lib/matplotlib/tests/baseline_images/test_figure/test_subfigure_scatter_size.png new file mode 100644 index 000000000000..6e46999e7e2a Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_figure/test_subfigure_scatter_size.png differ diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index ccb1381fa5b5..97f975356854 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1031,6 +1031,23 @@ def test_subfigure_spanning(): np.testing.assert_allclose(sub_figs[2].bbox.max, [w, h / 3]) +@image_comparison(['test_subfigure_scatter_size.png'], style='mpl20', + remove_text=True) +def test_subfigure_scatter_size(): + # markers in the left- and right-most subplots should be the same + fig = plt.figure() + gs = fig.add_gridspec(1, 2) + ax0 = fig.add_subplot(gs[1]) + ax0.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s') + ax0.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s') + + sfig = fig.add_subfigure(gs[0]) + axs = sfig.subplots(1, 2) + for ax in [ax0, axs[0]]: + ax.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s', color='r') + ax.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s', color='g') + + def test_add_subplot_kwargs(): # fig.add_subplot() always creates new axes, even if axes kwargs differ. fig = plt.figure() diff --git a/lib/mpl_toolkits/mplot3d/art3d.py b/lib/mpl_toolkits/mplot3d/art3d.py index 5c1825b255b2..f9ad0e2ce7cf 100644 --- a/lib/mpl_toolkits/mplot3d/art3d.py +++ b/lib/mpl_toolkits/mplot3d/art3d.py @@ -568,8 +568,9 @@ def set_3d_properties(self, zs, zdir): self._vzs = None self.stale = True + @_api.delete_parameter("3.5", "dpi") def set_sizes(self, sizes, dpi=72.0): - super().set_sizes(sizes, dpi) + super().set_sizes(sizes) if not self._in_draw: self._sizes3d = sizes @@ -607,7 +608,7 @@ def do_3d_projection(self, renderer=None): # we have to special case the sizes because of code in collections.py # as the draw method does - # self.set_sizes(self._sizes, self.figure.dpi) + # self.set_sizes(self._sizes) # so we can not rely on doing the sorting on the way out via get_* if len(self._sizes3d) > 1: