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

Skip to content

Avoid incorrect warning in savefig #11777

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

Merged
merged 1 commit into from
Aug 27, 2018

Conversation

fredrik-1
Copy link
Contributor

@fredrik-1 fredrik-1 commented Jul 25, 2018

The following code

    import matplotlib.pyplot as plt
    import matplotlib.transforms as tf
    fig, ax=plt.subplots(1,1, num=1, clear=True)
    fig.set_tight_layout(True)
    fig.savefig('test1.jpg', bbox_inches='tight')
    #or 
    fig.savefig('test2.jpg', bbox_inches=tf.Bbox([[0,0],[3,3]]))

Throws the following "incorrect" or at least irrelevant warning to the user:

matplotlib\lib\matplotlib\figure.py:2352: UserWarning: This figure includes 
Axes that are not compatible with tight_layout, so results might be incorrect.
  warnings.warn("This figure includes Axes that are not compatible " 

The reason is that tight_bbox.adjust_bbox sets the axes_locator to a function that makes the subplot axes not behave as a subplot axes in the tight layout calculation. The axes is thus ignored in that calculation and the warning appears. I believe that this is the intended behavior, except for the warning.

This pr change the code such that tight_layout is not called after the call to tight_bbox.adjust_bbox to avoid the warning. The actual behavior should be the same and it is in my tests.

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@tacaswell tacaswell added this to the v3.1 milestone Jul 26, 2018
@tacaswell
Copy link
Member

attn @jklymak

Re-setting tight_layout re-sets what axes are eligible to participate?

All of the code around this is very complex, is this the right place to fix this problem? I am concerned that this is adding more epicycles to the code rather than addressing it at the root.

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting as request changes to hold this for 3.1

Anyone may dismiss this review once 3.0 is branched.

@jklymak
Copy link
Member

jklymak commented Jul 26, 2018

Thanks @fredrik-1. I think a fix like this is fine.

<aside> Not quite sure why adjust_bbox ties itself in knots like it does so that this error happens, but lets not break that now. I suspect it has to do with not wanting the real axes locators to be called when this gets done, presumably because some of those would respond to changing figure size. </aside>

Organizationally, though, I think this fix should be inside tight_bbox.adjust_bbox if possible. adjust_bbox has a helper function restore_bbox that puts all the old locators back where it found them, so tight_layout state can be saved in there as well I think (w/o testing). That would be a cleaner change than introducing mysteriousness to backend_bases.

@fredrik-1
Copy link
Contributor Author

I did the proposed change, it works and I believe that it is the correct place for the fix.

@jklymak jklymak dismissed tacaswell’s stale review August 19, 2018 17:31

3.0 is now branched...

@dstansby dstansby merged commit 5698d3f into matplotlib:master Aug 27, 2018
@fredrik-1 fredrik-1 deleted the warning_savefig branch August 28, 2018 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants