Skip to content

swev-id: matplotlib__matplotlib-26208 fix: reuse categorical unitdata on shared axes#70

Open
casey-brooks wants to merge 1 commit intomatplotlib__matplotlib-26208from
fix/twinx-categorical-datalim
Open

swev-id: matplotlib__matplotlib-26208 fix: reuse categorical unitdata on shared axes#70
casey-brooks wants to merge 1 commit intomatplotlib__matplotlib-26208from
fix/twinx-categorical-datalim

Conversation

@casey-brooks
Copy link

Summary

  • reuse UnitData from existing shared categorical axes to avoid triggering the units callback that clears the original Axes.dataLim
  • update StrCategoryConverter.default_units to look up siblings before allocating a new UnitData
  • add regression coverage for stackplot+twinx combinations plus numeric and categorical control cases

Issue

Fixes #62

Reproduction

import matplotlib.pyplot as plt

def print_datalim(*ax):
    for ax_ in ax:
        print(ax_.dataLim.intervaly, end=' / ')
    print()

df1_index = ['16 May', '17 May']  # == df2_index
df1_values = [-22.717708333333402, 26.584999999999937]
df2_values = [-0.08501399999999998, -2.9833019999999966]

fig, ax1 = plt.subplots()

ax1.stackplot(df1_index, df1_values)
print_datalim(ax1)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
print_datalim(ax1, ax2)

ax2.plot(df1_index, df2_values)
print_datalim(ax1, ax2)

Observed failure (pre-fix)

[-22.71770833  26.585     ] /
[-22.71770833  26.585     ] / [ inf -inf] /
[ inf -inf] / [-2.983302 -0.085014] /

Environment

  • Container: Ubuntu 22.04
  • Python: 3.11.14 (nixpkgs#python311)
  • Matplotlib built via pip install -e . --config-settings editable_mode=compat --no-build-isolation
  • NumPy 1.26.4, pytest 9.0.2, flake8 7.3.0

Testing

  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib PYTEST_ADDOPTS='-W ignore::DeprecationWarning' .venv/bin/pytest -q lib/matplotlib/tests/test_axes.py::test_stackplot_twinx_preserves_datalim
  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib PYTEST_ADDOPTS='-W ignore::DeprecationWarning' .venv/bin/pytest -q lib/matplotlib/tests/test_axes.py::test_stackplot_twinx_reuses_unitdata
  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib PYTEST_ADDOPTS='-W ignore::DeprecationWarning' .venv/bin/pytest -q lib/matplotlib/tests/test_axes.py::test_stackplot_twinx_numeric_x_units_none
  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib PYTEST_ADDOPTS='-W ignore::DeprecationWarning' .venv/bin/pytest -q lib/matplotlib/tests/test_axes.py::test_stackplot_categorical_primary_axis_units
  • .venv/bin/flake8 lib/matplotlib/category.py
  • .venv/bin/flake8 --extend-ignore=E721 lib/matplotlib/tests/test_axes.py

@casey-brooks casey-brooks requested a review from a team December 25, 2025 13:20
@casey-brooks
Copy link
Author

Local Validation

  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib PYTEST_ADDOPTS='-W ignore::DeprecationWarning' .venv/bin/pytest -q lib/matplotlib/tests/test_axes.py::test_stackplot_twinx_preserves_datalim (1 passed)
  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib PYTEST_ADDOPTS='-W ignore::DeprecationWarning' .venv/bin/pytest -q lib/matplotlib/tests/test_axes.py::test_stackplot_twinx_reuses_unitdata (1 passed)
  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib PYTEST_ADDOPTS='-W ignore::DeprecationWarning' .venv/bin/pytest -q lib/matplotlib/tests/test_axes.py::test_stackplot_twinx_numeric_x_units_none (1 passed)
  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib PYTEST_ADDOPTS='-W ignore::DeprecationWarning' .venv/bin/pytest -q lib/matplotlib/tests/test_axes.py::test_stackplot_categorical_primary_axis_units (1 passed)
  • .venv/bin/flake8 lib/matplotlib/category.py (no issues)
  • .venv/bin/flake8 --extend-ignore=E721 lib/matplotlib/tests/test_axes.py (no issues; ignore applied for existing type equality assertions)

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

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

Looks good to me.

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.

2 participants