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

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Aug 1, 2021

PR Summary

The dpi transform was not correct for scatter in subfigures. Notice how the marker sizes are too small in the left-most axes below.

Unfortunately the transform stack in collections is weird, in that the "transform" is just a Nx3x3 matrix rather than a full-fledged transform, so fixing directly is difficult. Instead we just figure out what the proper scaling is by transforming one inch.

NOTE I am deprecating the dpi argument to collections.set_sizes. I'm not clear why you would ever want to hard code this...

Before

Before

After

After

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@jklymak jklymak marked this pull request as ready for review August 3, 2021 00:23
@QuLogic
Copy link
Member

QuLogic commented Aug 3, 2021

The before and after images look exactly the same?

@jklymak
Copy link
Member Author

jklymak commented Aug 3, 2021

Ooops, interesting. If I save as png they look fine before and after. If I just do plt.show() I get:

Before:

Before

After:

After

@jklymak jklymak marked this pull request as draft August 5, 2021 19:23
@anntzer
Copy link
Contributor

anntzer commented Aug 5, 2021

@jklymak Is the following (approximately, untested) close to what we need (as discussed in the call?)

diff --git i/lib/matplotlib/figure.py w/lib/matplotlib/figure.py
index 4041793412..ef265b18cc 100644
--- i/lib/matplotlib/figure.py
+++ w/lib/matplotlib/figure.py
@@ -2001,6 +2001,14 @@ class SubFigure(FigureBase):
         if parent._layoutgrid is not None:
             self.init_layoutgrid()
 
+    @property
+    def dpi(self):
+        return self._parent.dpi
+
+    @dpi.setter
+    def dpi(self, value):
+        self._parent.dpi = value
+
     def _redo_transform_rel_fig(self, bbox=None):
         """
         Make the transSubfigure bbox relative to Figure transform.

@QuLogic
Copy link
Member

QuLogic commented Aug 5, 2021

Related to your question on the call, copying the test and saving at different DPI:

import matplotlib.pyplot as plt


# 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')

fig.savefig('foo0.png', dpi=100)
fig.savefig('foo1.png', dpi=200)
fig.savefig('foo2.png', dpi=300)

produces an image with changing scatter size (before this PR).

@jklymak
Copy link
Member Author

jklymak commented Aug 5, 2021

Closed for #20771. #20771 now includes the test here for scatter size...

@jklymak jklymak closed this Aug 5, 2021
@jklymak jklymak deleted the fix-dpi-scatter branch August 5, 2021 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants