Skip to content

FIX: check subplot kwargs #15966

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 3 commits into from
Dec 18, 2019
Merged

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Dec 17, 2019

PR Summary

Closes #15964

A couple more guard-rails for using plt.subplot when you really meant plt.subplots.

from matplotlib import pyplot as plt
fig, (ax1, ax2) = plt.subplot(nrows=1, ncols=2, sharey=True)

Old:

...
   ~/.virtualenvs/tcpsp/lib/python3.7/site-packages/matplotlib/cbook/__init__.py in join(self, a, *args)
        923 
        924         for arg in args:
    --> 925             set_b = mapping.get(weakref.ref(arg), [weakref.ref(arg)])
        926             if set_b is not set_a:
        927                 if len(set_b) > len(set_a):


    TypeError: cannot create weak reference to 'bool' object

Now:

./testIt.py:2: UserWarning: You have passed nrows and/or ncols to subplot(). Did you mean to use subplots()?
  fig, (ax1, ax2) = plt.subplot(nrows=1, ncols=2, sharey=True)
Traceback (most recent call last):
  File "./testIt.py", line 2, in <module>
    fig, (ax1, ax2) = plt.subplot(nrows=1, ncols=2, sharey=True)
  File "/Users/jklymak/matplotlib/lib/matplotlib/pyplot.py", line 963, in subplot
    a = fig.add_subplot(*args, **kwargs)
  File "/Users/jklymak/matplotlib/lib/matplotlib/figure.py", line 1390, in add_subplot
    a = subplot_class_factory(projection_class)(self, *args, **kwargs)
  File "/Users/jklymak/matplotlib/lib/matplotlib/axes/_subplots.py", line 39, in __init__
    self._axes_class.__init__(self, fig, self.figbox, **kwargs)
  File "/Users/jklymak/matplotlib/lib/matplotlib/axes/_base.py", line 446, in __init__
    raise TypeError('sharey must be an axes, not a bool')
TypeError: sharey must be an axes, not a bool

Wasn't sure if passing nrows or ncols should just error out.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 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

@jklymak jklymak added this to the v3.3.0 milestone Dec 17, 2019
Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

I usually am against these kinds of checks, but here I'll approve because it's such a basic entry point and the error is so mystifying.

@jklymak
Copy link
Member Author

jklymak commented Dec 17, 2019

Should we error on ncols=1? or is a warning OK?

@jklymak jklymak force-pushed the fix-check-subplot-syntax branch from 75d5200 to 02490b7 Compare December 17, 2019 21:05
@anntzer
Copy link
Contributor

anntzer commented Dec 17, 2019

Is there a reason why one would want to write subplot(ncols=1)? I don't think so? In which case erroring seems fine?

@jklymak
Copy link
Member Author

jklymak commented Dec 17, 2019

It doesn't work right now because we check args for ncols etc rather than passing them by name, so I think an error is OK.

@jklymak jklymak force-pushed the fix-check-subplot-syntax branch from a5ea45f to 0ef53cf Compare December 17, 2019 21:25
@jklymak jklymak force-pushed the fix-check-subplot-syntax branch from 0ef53cf to 67117cc Compare December 17, 2019 21:57
@timhoffm timhoffm merged commit e5283e0 into matplotlib:master Dec 18, 2019
@jklymak jklymak deleted the fix-check-subplot-syntax branch December 16, 2020 21:32
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.

Guard plt.subplot kwargs a bit better?
3 participants