Skip to content

Commit 0c143b1

Browse files
Copilothenryiii
andauthored
fix: replace deprecated axes grouper .join() with .sharex() for matplotlib>=3.6 (#635)
* Initial plan * fix: Replace deprecated .join() with .sharex() for matplotlib>=3.6 compatibility Co-authored-by: henryiii <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: henryiii <[email protected]>
1 parent 65f7935 commit 0c143b1

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

src/mplhep/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,14 @@ def extend_ratio(ax, yhax):
472472
fig.get_size_inches()[0],
473473
fig.get_size_inches()[1] * extend_ratio(ax, yhax)[1],
474474
)
475-
ax.get_shared_x_axes().join(ax, yhax)
475+
yhax.sharex(ax)
476476
elif position in ["bottom"]:
477477
divider.set_vertical([*xsizes, axes_size.Fixed(height)])
478478
fig.set_size_inches(
479479
fig.get_size_inches()[0],
480480
fig.get_size_inches()[1] * extend_ratio(ax, yhax)[1],
481481
)
482-
ax.get_shared_x_axes().join(ax, yhax)
482+
yhax.sharex(ax)
483483

484484
return yhax
485485

5.46 KB
Loading
5.46 KB
Loading
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
Tests for hist2dplot cbarpos parameter with matplotlib >= 3.6.
3+
4+
This test addresses the bug where using cbarpos="top" or cbarpos="bottom"
5+
causes an AttributeError due to matplotlib 3.6+ making groupers immutable.
6+
The fix uses ax.sharex() instead of the deprecated .join() method.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import os
12+
13+
os.environ["RUNNING_PYTEST"] = "true"
14+
15+
import matplotlib.pyplot as plt
16+
import numpy as np
17+
import pytest
18+
19+
plt.switch_backend("Agg")
20+
21+
import mplhep as mh
22+
23+
24+
@pytest.fixture
25+
def sample_2d_data():
26+
"""Create sample 2D histogram data."""
27+
np.random.seed(42)
28+
xedges = np.linspace(0, 10, 51)
29+
yedges = np.linspace(0, 10, 51)
30+
H = np.random.normal(size=(50, 50))
31+
return H, xedges, yedges
32+
33+
34+
def test_hist2dplot_cbarpos_top(sample_2d_data):
35+
"""Test hist2dplot with cbarpos='top'."""
36+
H, xedges, yedges = sample_2d_data
37+
fig, ax = plt.subplots()
38+
mh.hist2dplot(H, xedges, yedges, cbar=True, cbarpos="top", ax=ax)
39+
plt.close(fig)
40+
41+
42+
def test_hist2dplot_cbarpos_bottom(sample_2d_data):
43+
"""Test hist2dplot with cbarpos='bottom'."""
44+
H, xedges, yedges = sample_2d_data
45+
fig, ax = plt.subplots()
46+
mh.hist2dplot(H, xedges, yedges, cbar=True, cbarpos="bottom", ax=ax)
47+
plt.close(fig)
48+
49+
50+
def test_hist2dplot_cbarpos_left(sample_2d_data):
51+
"""Test hist2dplot with cbarpos='left'."""
52+
H, xedges, yedges = sample_2d_data
53+
fig, ax = plt.subplots()
54+
mh.hist2dplot(H, xedges, yedges, cbar=True, cbarpos="left", ax=ax)
55+
plt.close(fig)
56+
57+
58+
def test_hist2dplot_cbarpos_right(sample_2d_data):
59+
"""Test hist2dplot with cbarpos='right'."""
60+
H, xedges, yedges = sample_2d_data
61+
fig, ax = plt.subplots()
62+
mh.hist2dplot(H, xedges, yedges, cbar=True, cbarpos="right", ax=ax)
63+
plt.close(fig)
64+
65+
66+
def test_hist2dplot_cbarpos_all_positions(sample_2d_data):
67+
"""Test hist2dplot with all cbarpos positions in one test."""
68+
H, xedges, yedges = sample_2d_data
69+
positions = ["top", "bottom", "left", "right"]
70+
71+
for pos in positions:
72+
fig, ax = plt.subplots()
73+
mh.hist2dplot(H, xedges, yedges, cbar=True, cbarpos=pos, ax=ax)
74+
plt.close(fig)
75+
76+
77+
@pytest.mark.mpl_image_compare(style="default", remove_text=True, tolerance=10)
78+
def test_hist2dplot_cbarpos_top_visual():
79+
"""Visual comparison test for cbarpos='top'."""
80+
np.random.seed(42)
81+
xedges = np.linspace(0, 10, 21)
82+
yedges = np.linspace(0, 10, 21)
83+
x = np.random.normal(5, 2, 1000)
84+
y = np.random.normal(5, 2, 1000)
85+
H, _, _ = np.histogram2d(x, y, bins=(xedges, yedges))
86+
87+
fig, ax = plt.subplots(figsize=(8, 7))
88+
mh.hist2dplot(H, xedges, yedges, cbar=True, cbarpos="top", ax=ax)
89+
90+
return fig
91+
92+
93+
@pytest.mark.mpl_image_compare(style="default", remove_text=True, tolerance=10)
94+
def test_hist2dplot_cbarpos_bottom_visual():
95+
"""Visual comparison test for cbarpos='bottom'."""
96+
np.random.seed(42)
97+
xedges = np.linspace(0, 10, 21)
98+
yedges = np.linspace(0, 10, 21)
99+
x = np.random.normal(5, 2, 1000)
100+
y = np.random.normal(5, 2, 1000)
101+
H, _, _ = np.histogram2d(x, y, bins=(xedges, yedges))
102+
103+
fig, ax = plt.subplots(figsize=(8, 7))
104+
mh.hist2dplot(H, xedges, yedges, cbar=True, cbarpos="bottom", ax=ax)
105+
106+
return fig

0 commit comments

Comments
 (0)