GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — DOC: Tutorial on API shortcuts by timhoffm · Pull Request #30952 · matplotlib/matplotlib

Conversation

@timhoffm

@timhoffm timhoffm commented Jan 12, 2026

Copy link
Copy Markdown
Member

Closes #30941.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rcomer reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@github-actions github-actions Bot added the Documentation: tutorials files in galleries/tutorials label Jan 12, 2026
@timhoffm
timhoffm force-pushed the api-shortcuts branch from 86f6b00 to a02dbfb Compare January 12, 2026 00:22
anntzer
anntzer reviewed Jan 12, 2026
View reviewed changes
Comment thread galleries/tutorials/compact_api.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm
timhoffm force-pushed the api-shortcuts branch 2 times, most recently from 6e76aa7 to ed817a0 Compare January 12, 2026 19:19
QuLogic
QuLogic reviewed Jan 14, 2026
View reviewed changes
Comment thread galleries/tutorials/compact_api.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread galleries/tutorials/compact_api.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread galleries/tutorials/compact_api.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread galleries/tutorials/compact_api.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread galleries/tutorials/compact_api.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread galleries/tutorials/compact_api.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread galleries/tutorials/compact_api.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

rcomer
rcomer reviewed Jan 14, 2026
View reviewed changes

@rcomer rcomer left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Most of my thoughts were addressed as I kept reading 🙂

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

timhoffm and story645 reacted with heart emoji
All reactions
  • ❤️ 2 reactions
Comment thread galleries/tutorials/compact_api.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread galleries/tutorials/coding_shortcuts.py
# You can go even further by tapping into the pyplot logic and use `.pyplot.axes` to
# create the axes:

ax = plt.axes(xlabel="day", ylabel="daylight hours", title="London")

@rcomer rcomer Jan 14, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is there a reason to use plt.axes here rather than plt.subplot? plt.subplot would seem more consistent with subplots/add_subplot in the rest of the tutorial.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@timhoffm timhoffm Jan 14, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This is indeed a debatable aspect plt.axes() and plt.subplot() are equivalent.

I think the options are more or less:

flowchart LR
    A["A: fig, ax = plt.subplots()"]
    A --> B["B: ax = plt.figure().subplots()"]
    A --> C["C: ax = plt.figure().add_subplot()"]
    B --> D["D: ax = plt.axes()"]
    C --> D
    B --> E["E: ax = plt.subplot()"]
    C --> E
Loading

I have chosen A -> C because that's what @anntzer uses and add_subplot() accept Axes properties as kwags, which subplots() does not. Ideally there would be plt.figure().add_axes() or plt.figure().axes() but we are not in that place right now and this PR is scoped to use existing API.

Favoring D over E is based on my dislike for subplot(). IMHO creating a single subplot at a time is awkward, because you have implicit state - a set subplot() calls is only meaningful if they are aligned. That doesn't matter for creating a single 111 subplot, but I don't want to advertise that function in general. Also, plt.subplots() / fig.subplots(), plt.subplot() look very much alike but they have subtly different semantics. YMMV, but I find somewhat more distinct names better in this case. I also like the similarity ax = plt.axes(). Eventually, plt.axes() is shorter than plt.subplot(); and brevity is the point of this tutorial 😄.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rcomer reacted with thumbs up emoji rcomer reacted with eyes emoji
All reactions
  • 👍 1 reaction
  • 👀 1 reaction

@rcomer rcomer Jan 14, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We do have
https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.add_axes.html#matplotlib.figure.Figure.add_axes

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jklymak jklymak Jan 14, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Plt.axes is pretty ambiguous as to whether it is treated as a subplot, and hence placed using subplot_params or not. I looked at the source and it is created with add_subplot (unless you use a four-tuple) so thats probably fine, but the ambiguity is a bit confusing.

On the other hand the number of times I've mistyped subplot when I meant subplots is an argument against subplot!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@rcomer rcomer Jan 14, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Oh I see. Figure.add_axes requires at least one parameter so is not equivalent to plt.axes 👀😬

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@timhoffm timhoffm Jan 14, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Exactly, one could discuss whether supporting argument-less fig.add_axes() is reasonable, but that'd be a much heavier lift than describing existing API.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rcomer reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@timhoffm timhoffm Jan 14, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Plt.axes is pretty ambiguous as to whether it is treated as a subplot, and hence placed using subplot_params or not. I looked at the source and it is created with add_subplot (unless you use a four-tuple) so thats probably fine, but the ambiguity is a bit confusing.

tl;dr: I consider this rather a feature because it creates less mental overhead for the average user

I consider this rather a feature. The distinction between Axes and Subplot is an unnecessary burden for the average Matplotlib user (#18222). To clarify for those who are not into the topic (not sure if this is 100% exact, but the definition is good enough for here): A Subplot is an Axes that is created based on a GridSpec, determing the rough part of the figure this is plotted into, and SubplotParams, determining borders and margins of the Axes grid.

In former times this difference was more visible through the Subplot class, which we luckily got rid of #23573. Unfortunately, the name subplot must be kept around because plt.subplots() is our most-prominent function. To alleviate this, I consider and teach subplot as in informal concept - i.e. multiple figures arranged in a grid - as in "the figure consists of two (sub)plots side-by-side". One subplot being the limiting case is a just plausible enough explanation for fig, ax = plt.subplots(). It's called "subplots" because it supports creating one or more Axes.

I'd argue that for creating a single full-figure Axes, the distinction between a 111 subplot and a manually places Axes is irrelevant for most users. The only difference is whether you can control its position via subplot_params. But most users won't do manual adjustment, and the need is thankfully significantly reduced through constrained_layout.

In that spirit:

  • plt.subplots() as the general solution is here to stay. We cannot do anything about it and justify it's naming because it can create multiple (sub)plots.
  • fig.add_subplot() is the mildly odd one for creating just one full-sized Axes. fig.subplots() works, but I don't want to recommend it here because it cannot directly accept Axes properties (which is justified in that it creates both figure and Axes). I would use and argument-less fig.add_axes() if that was there, but is not right now.
  • plt.axes() is better than plt.subplot(), because the informal subplot notion is awkward for one full-size (sub)plot.

Some of the arguments above are a bit of a stretch, but this is my best solution to tell a comprehensible story on a historically complex and at times suboptimal API.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rcomer reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@timhoffm

timhoffm commented Jan 14, 2026

Copy link
Copy Markdown
Member Author

@rcomer @story645 I'm not 100% happy with the title Shorter call patterns (and the filename compact_api.py is also not in sync). Do you have suggestions for improvement?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@story645

story645 commented Jan 14, 2026

Copy link
Copy Markdown
Member

Do you have suggestions for improvement?

I'd just name it shortcuts since that's what you named the PR and put the note about these being shorter calls in the preamble if it's not already there.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm

timhoffm commented Jan 14, 2026

Copy link
Copy Markdown
Member Author

I'd just name it shortcuts since that's what you named the PR and put the note about these being shorter calls in the preamble if it's not already there.

What shortcuts exactly?

  • "Shortcuts" - is too generic (could e.g. also be keyboard shortcuts)
  • "API shortcuts" - Technically correct but maybe possibly appealing to the less experienced user. They don't care about API, the just want to plot something with little effort.
  • "Plotting shortcuts" - Maybe but a bit vague
  • "Plotting command shortcuts" - Not sure, but "plotting" commands could be misunderstood to focus on the methods that "plot data", where as the big shortening here is on the other supplementary activities
  • ...

For now I stay with "Shorter call patterns", which seems the most understandable to me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm
timhoffm force-pushed the api-shortcuts branch from eedfe13 to 2a9c9d4 Compare January 14, 2026 21:52
@rcomer

rcomer commented Jan 14, 2026

Copy link
Copy Markdown
Member

"Coding shortcuts"?

timhoffm and story645 reacted with thumbs up emoji story645 reacted with heart emoji
All reactions
  • 👍 2 reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm
DOC: Tutorial on coding shortcuts
bec52d0
@timhoffm
timhoffm force-pushed the api-shortcuts branch from 2a9c9d4 to bec52d0 Compare January 14, 2026 22:21
@timhoffm

timhoffm commented Jan 14, 2026

Copy link
Copy Markdown
Member Author

I like "Coding shortcuts". "Coding" has the right informal pragmatism that applies to this tutorial.

story645 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rcomer
rcomer reviewed Jan 15, 2026
View reviewed changes
Comment thread galleries/tutorials/index.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread galleries/tutorials/index.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread galleries/tutorials/index.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm @rcomer
Apply suggestions from code review
4bf79a2
Co-authored-by: Ruth Comer <[email protected]>
rcomer
rcomer approved these changes Jan 15, 2026
View reviewed changes
@rcomer rcomer added this to the v3.10-doc milestone Jan 15, 2026
@rcomer
rcomer merged commit ea0fb5b into matplotlib:main Jan 15, 2026
20 checks passed

Uh oh!

There was an error while loading. Please reload this page.

meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Jan 15, 2026
@timhoffm @meeseeksmachine
Backport PR matplotlib#30952: DOC: Tutorial on API shortcuts
bc44318
@meeseeksmachine meeseeksmachine mentioned this pull request Jan 15, 2026
Backport PR #30952 on branch v3.10.x (DOC: Tutorial on API shortcuts) #30965
Merged
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Jan 15, 2026
@timhoffm @meeseeksmachine
Backport PR matplotlib#30952: DOC: Tutorial on API shortcuts
efb581c
@meeseeksmachine meeseeksmachine mentioned this pull request Jan 15, 2026
Backport PR #30952 on branch v3.10.8-doc (DOC: Tutorial on API shortcuts) #30966
Merged
@timhoffm
timhoffm deleted the api-shortcuts branch January 15, 2026 10:43
timhoffm added a commit that referenced this pull request Jan 15, 2026
@timhoffm
Merge pull request #30966 from meeseeksmachine/auto-backport-of-pr-30…
3c78c27
…952-on-v3.10.8-doc

Backport PR #30952 on branch v3.10.8-doc (DOC: Tutorial on API shortcuts)
timhoffm added a commit that referenced this pull request Jan 15, 2026
@timhoffm
Merge pull request #30965 from meeseeksmachine/auto-backport-of-pr-30…
521df0b
…952-on-v3.10.x

Backport PR #30952 on branch v3.10.x (DOC: Tutorial on API shortcuts)
@timhoffm timhoffm mentioned this pull request Jan 15, 2026
ENH: Support Figure.add_axes() without parameters #30968
Open
wavebyrd pushed a commit to wavebyrd/matplotlib that referenced this pull request Mar 13, 2026
@timhoffm @meeseeksmachine
Backport PR matplotlib#30952: DOC: Tutorial on API shortcuts
a42dc7b
wavebyrd pushed a commit to wavebyrd/matplotlib that referenced this pull request Mar 13, 2026
@timhoffm
Merge pull request matplotlib#30965 from meeseeksmachine/auto-backpor…
b05e08c
…t-of-pr-30952-on-v3.10.x

Backport PR matplotlib#30952 on branch v3.10.x (DOC: Tutorial on API shortcuts)
wavebyrd pushed a commit to wavebyrd/matplotlib that referenced this pull request Mar 13, 2026
@timhoffm @meeseeksmachine
Backport PR matplotlib#30952: DOC: Tutorial on API shortcuts
606ac3e
wavebyrd pushed a commit to wavebyrd/matplotlib that referenced this pull request Mar 13, 2026
@timhoffm
Merge pull request matplotlib#30966 from meeseeksmachine/auto-backpor…
f2782c3
…t-of-pr-30952-on-v3.10.8-doc

Backport PR matplotlib#30952 on branch v3.10.8-doc (DOC: Tutorial on API shortcuts)
This was referenced Apr 21, 2026
Backport PR #31282 on branch v3.10.x (SEC: Block shell escapes in latex and ps commands) #31542
Closed
Backport PR #31248 on branch v3.10.x (SEC: Remove eval() from validate_cycler) #31543
Closed
merge up v3.10.9 #31560
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@QuLogic QuLogic QuLogic left review comments
@anntzer anntzer anntzer left review comments
@jklymak jklymak jklymak left review comments
@rcomer rcomer rcomer approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Documentation: tutorials files in galleries/tutorials

Projects

None yet

Milestone

v3.10-doc

Development

Successfully merging this pull request may close these issues.

Idea: Reducing verbosity through more compact API patterns

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@timhoffm @story645 @rcomer @QuLogic @anntzer @jklymak
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.