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

Skip to content

FIX: dpi and scatter for subfigures now correct #20777

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/deprecations/20777-JMK.rst
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 7 additions & 4 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -956,15 +957,17 @@ 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([])
self._transforms = np.empty((0, 3, 3))
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
Expand All @@ -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)


Expand Down Expand Up @@ -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
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions lib/mpl_toolkits/mplot3d/art3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down