Skip to content

pyplot.subplots() accepts subplot arguments through **kwargs - #16738

Closed
sabulikia wants to merge 1 commit into
matplotlib:masterfrom
CSCD01-team04:deliverable_2_1460
Closed

sabulikia wants to merge 1 commit into
matplotlib:masterfrom
CSCD01-team04:deliverable_2_1460

Conversation

@sabulikia

Copy link
Copy Markdown

Fixes #1460

PR Summary

pyplot.subplots() now accepts subplot arguments through **kwargs without the need of using dictionaries.

figure1, axis1 = plt.subplots(figsize=(10,20), polar=True)
figure2, axis2 = plt.subplots(figsize=(10,20), subplot_kw=dict(polar=True)) # this still works

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

@sabulikia sabulikia changed the title Allow subplot args to be passed through **kwargs pyplot.subplots() accepts subplot arguments through **kwargs Mar 12, 2020
@sabulikia
sabulikia force-pushed the deliverable_2_1460 branch 2 times, most recently from f31e844 to 969828e Compare March 12, 2020 02:15

@QuLogic QuLogic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure what the original goal of #1460 was, but add_subplot accepts keyword arguments and forwards them to the Axes constructor. This means that subplot_kw can accept many more keyword arguments than the 3 listed here. Maybe that's not needed, but it should be well documented.

Comment thread lib/matplotlib/pyplot.py Resolved
Comment thread lib/matplotlib/pyplot.py Resolved
Comment thread lib/matplotlib/pyplot.py Resolved
@sabulikia
sabulikia force-pushed the deliverable_2_1460 branch from 6710f67 to 402476a Compare March 12, 2020 14:16
@tacaswell

Copy link
Copy Markdown
Member

Wow, that is an old issue!

I share @QuLogic 's concerns about hard-coding a sub-set of kwargs to be hoisted up to subplot_kw. Right now, there is a clear seperation: subplot kwargs go in a dict, fig_kw go as "normal" kwarg so there is no ambiguity over where the input is going to get routed. By special casing just some of the subplot keyword arguments we are setting our selves up bugs in the future of the form "This kwarg X works, but kwarg Y doesn't".

This also has the logical extension that we should route some keys to gridspec_kw if we know they should go there which would make everything more complex.

In short, I am worried that half measures will make things more confusing and inconsistent and the full solution may grow too complex.

@tacaswell tacaswell added this to the v3.3.0 milestone Mar 12, 2020
@sabulikia

sabulikia commented Mar 12, 2020 •

Copy link
Copy Markdown
Author

I checked add_subplot()'s documentation and it mentions "projection", "polar", "sharex", "sharey", and "label". In pyplot.subplot()'s signature the "sharex" and "sharey" arguments are explicitly passed, so I just extracted the three remaining ones from **kwargs.
@tacaswell Just to make sure I understand the issue correctly: The issue with this is that it's not extendable and everytime the arguments need to be changed, the list needs to be updated as well. Also extending this idea to gridspec_kw could make things more complicated. Correct? So your suggestion is to look for a more fundamental solution?

@tacaswell

Copy link
Copy Markdown
Member

@sabulikia That is a reasonable summary.

On one hand add_subplot, in addition to the keywords it consumes, also pass**kwargs through to the subplots class factory. This means we can never fully get rid of subplots_kw (as it is technically possible for the user to provide their on Axes sub-class (which something like windrose [1] does). On the flip side plt.figure also takes **kwargs and passes them onto new_figure_manager which also take **kwargs and pasess them on. Thus we can't do the inverse and identify all of the kwargs that should go to figure by name (and hence bundle the rest of them into the subplots_kw).

The fundamental issue is we have two things that consume all of the "extra" keyword arguments and are user-plug able. I do not think there is a more fundamental solution possible here, but I would be happy to be proved wrong!

It seems simpler to say "input to subplots goes here and input to Figure goes there" than to add "...except for XYZ". Any one exception is easy to remember, but en-masse they are huge sources of friction.

[1] https://github.com/python-windrose/windrose

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.

make subplots smarter with regards to subplot_kw and figure_kw

3 participants