Skip to content

plt.stairs: fix unit handling for orientation="horizontal"#31666

Merged
story645 merged 1 commit into
matplotlib:mainfrom
stiglers-eponym:fix_stairs_units
May 17, 2026
Merged

plt.stairs: fix unit handling for orientation="horizontal"#31666
story645 merged 1 commit into
matplotlib:mainfrom
stiglers-eponym:fix_stairs_units

Conversation

@stiglers-eponym

Copy link
Copy Markdown
Contributor

PR summary

plt.stairs or Axes.stairs calls self._process_unit_info as required for e.g. time dimensions on one axis. This PR introduces a distinction between vertical and horizontal orientation to fix the unit processing for orientation="horizontal".

Example

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> times = np.arange(10).astype("M8[Y]")
>>> plt.stairs(np.random.random(9), times, orientation="horizontal")
<matplotlib.patches.StepPatch object at 0x7f5a834a3410>
>>> plt.show()
>>> # Old behavior: sets the x axis to time coordinates, but the y axis should contain time values.
>>> # New behavior: sets the y axis to time coordinates.

AI Disclosure

No AI was used for this PR.

PR checklist

Comment on lines +7833 to +7835
else:
edges, values, baseline = self._process_unit_info(
[("y", edges), ("x", values), ("x", baseline)], kwargs)

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.

Since vertical is the default, should it be the fallback position? Or will this error out if an orientation besides horizontal or vertical is provided?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just followed the convention that is used in mpatches.StepPatch (in patches.py) to which orientation is passed. The current behavior is that any value that is not "vertical" will be interpreted like "horizontal".

One can of course change this behavior, but that was not my intention in this PR.

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 hear that that's a separate discussion. I'm also wondering why stairs has to do unit processing if it's being done inside the patch creation code. Is it cause of:

https://github.com/stiglers-eponym/matplotlib/blob/6bcc38ad70066c391e327884af8bf917975cfd3f/lib/matplotlib/axes/_axes.py#L7855-L7863

And if so can that information be populated from patch.get_data()?

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.

The issue is that Artists are currently not responsible for data limit handling. For the patch itself, this is done in add_patch() through _update_patch_limits(). But I suspect this cannot handle the sticky edges of the baseline, and it would not be reasonable to special case this there. Therefore, the baseline handling is left directly to stairs(), which implies the need for unit conversion.

The long-term solution will be #30342. I don't think a partial rewrite to handle the current limitations in another way is reasonable. Therefore, the proposed minimal fix is the way to go.

@story645 story645 added this to the v3.11.0 milestone May 17, 2026
@story645 story645 merged commit c6377d0 into matplotlib:main May 17, 2026
41 checks passed
@story645

Copy link
Copy Markdown
Member

Thanks for the fix!

timhoffm pushed a commit that referenced this pull request May 17, 2026
timhoffm added a commit that referenced this pull request May 17, 2026
…666-on-v3.11.x

Backport PR #31666 on branch v3.11.x (plt.stairs: fix unit handling for orientation="horizontal")
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.

3 participants