Skip to content

Commit 0b2ef5b

Browse files
cvanelterenCopilot
andauthored
Hotfix panel (#238)
* simplified logic and basing reference on original grid * rm print statements * spelling * Update ultraplot/figure.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * do more comprehensive checking * rm debug * add some reasoning * fixed * add a stale check * compound tests and check for the failure * fixed missing functions in grid * need to fix test * update calls * update tests * formatting restored to defaults * don't adjust labels when not sharing * tests pass -- some expected failures * rm debug statements * fix test * added tests * updated test to check for sharing of panels * update test * update test * Update ultraplot/tests/test_geographic.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * rm unnecessary funcs * Deprecate basemap (#243) * Deprecate basemap. Basemap will be deprecated in the next major release (v2.0). See #243 for context. * Hotfix get_border_axes (#236) * simplified logic and basing reference on original grid * rm print statements * spelling * Update ultraplot/figure.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * do more comprehensive checking * rm debug * add some reasoning * fixed * add a stale check * compound tests and check for the failure * update calls * update tests * formatting restored to defaults * don't adjust labels when not sharing * tests pass -- some expected failures * rm debug statements * fix test * Update ultraplot/tests/test_geographic.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update ultraplot/figure.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update ultraplot/axes/geo.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Revert changes * rm duplicate * fixes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * refactor panel_group_member * mv logic to base * mv to the correct spot --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 84a57e6 commit 0b2ef5b

File tree

4 files changed

+112
-13
lines changed

4 files changed

+112
-13
lines changed

ultraplot/axes/base.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,43 @@ def set_prop_cycle(self, *args, **kwargs):
31503150
cycle = self._active_cycle = constructor.Cycle(*args, **kwargs)
31513151
return super().set_prop_cycle(cycle) # set the property cycler after validation
31523152

3153+
def _is_panel_group_member(self, other: "Axes") -> bool:
3154+
"""
3155+
Determine if the current axes and another axes belong to the same panel group.
3156+
3157+
Two axes belong to the same panel group if any of the following is true:
3158+
1. One axis is the parent of the other
3159+
2. Both axes are panels sharing the same parent
3160+
3161+
Parameters
3162+
----------
3163+
other : Axes
3164+
The other axes to compare with
3165+
3166+
Returns
3167+
-------
3168+
bool
3169+
True if both axes belong to the same panel group, False otherwise
3170+
"""
3171+
# Case 1: self is a panel of other (other is the parent)
3172+
if self._panel_parent is other:
3173+
return True
3174+
3175+
# Case 2: other is a panel of self (self is the parent)
3176+
if other._panel_parent is self:
3177+
return True
3178+
3179+
# Case 3: both are panels of the same parent
3180+
if (
3181+
self._panel_parent
3182+
and other._panel_parent
3183+
and self._panel_parent is other._panel_parent
3184+
):
3185+
return True
3186+
3187+
# Not in the same panel group
3188+
return False
3189+
31533190
@docstring._snippet_manager
31543191
def inset(self, *args, **kwargs):
31553192
"""

ultraplot/axes/cartesian.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -551,18 +551,6 @@ def _get_spine_side(self, s, loc):
551551
)
552552
return side
553553

554-
def _is_panel_group_member(self, other):
555-
"""
556-
Return whether the axes belong in a panel sharing stack..
557-
"""
558-
return (
559-
self._panel_parent is other # other is child panel
560-
or other._panel_parent is self # other is main subplot
561-
or other._panel_parent
562-
and self._panel_parent # ...
563-
and other._panel_parent is self._panel_parent # other is sibling panel
564-
)
565-
566554
def _sharex_limits(self, sharex):
567555
"""
568556
Safely share limits and tickers without resetting things.

ultraplot/axes/geo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ def __share_axis_setup(
566566
limits: bool,
567567
):
568568
level = getattr(self.figure, f"_share{which}")
569-
if getattr(self, f"_panel_share{which}_group") and self.is_panel_group_member(
569+
if getattr(self, f"_panel_share{which}_group") and self._is_panel_group_member(
570570
other
571571
):
572572
level = 3

ultraplot/tests/test_geographic.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,77 @@ def test_check_tricontourf():
665665
assert "transform" in mocked.call_args.kwargs
666666
assert isinstance(mocked.call_args.kwargs["transform"], ccrs.PlateCarree)
667667
uplt.close(fig)
668+
669+
670+
def test_panels_geo():
671+
fig, ax = uplt.subplots(proj="cyl")
672+
ax.format(labels=True)
673+
for dir in "top bottom right left".split():
674+
pax = ax.panel_axes(dir)
675+
match dir:
676+
case "top":
677+
assert len(pax.get_xticklabels()) > 0
678+
assert len(pax.get_yticklabels()) > 0
679+
case "bottom":
680+
assert len(pax.get_xticklabels()) > 0
681+
assert len(pax.get_yticklabels()) > 0
682+
case "left":
683+
assert len(pax.get_xticklabels()) > 0
684+
assert len(pax.get_yticklabels()) > 0
685+
case "right":
686+
assert len(pax.get_xticklabels()) > 0
687+
assert len(pax.get_yticklabels()) > 0
688+
689+
690+
@pytest.mark.mpl_image_compare
691+
def test_geo_with_panels():
692+
"""
693+
We are allowed to add panels in GeoPlots
694+
"""
695+
# Define coordinates
696+
lat = np.linspace(-90, 90, 180)
697+
lon = np.linspace(-180, 180, 360)
698+
time = np.arange(2000, 2005)
699+
lon_grid, lat_grid = np.meshgrid(lon, lat)
700+
701+
# Zoomed region elevation (Asia region)
702+
lat_zoom = np.linspace(0, 60, 60)
703+
lon_zoom = np.linspace(60, 180, 120)
704+
lz, lz_grid = np.meshgrid(lon_zoom, lat_zoom)
705+
706+
elevation = (
707+
2000 * np.exp(-((lz - 90) ** 2 + (lz_grid - 30) ** 2) / 400)
708+
+ 1000 * np.exp(-((lz - 120) ** 2 + (lz_grid - 45) ** 2) / 225)
709+
+ np.random.normal(0, 100, lz.shape)
710+
)
711+
elevation = np.clip(elevation, 0, 4000)
712+
713+
fig, ax = uplt.subplots(nrows=2, proj="cyl")
714+
pax = ax[0].panel("r")
715+
pax.barh(lat_zoom, elevation.sum(axis=1))
716+
pax = ax[1].panel("r")
717+
pax.barh(lat_zoom - 30, elevation.sum(axis=1))
718+
ax[0].pcolormesh(
719+
lon_zoom,
720+
lat_zoom,
721+
elevation,
722+
cmap="bilbao",
723+
colorbar="t",
724+
colorbar_kw=dict(
725+
align="l",
726+
length=0.5,
727+
),
728+
)
729+
ax[1].pcolormesh(
730+
lon_zoom,
731+
lat_zoom - 30,
732+
elevation,
733+
cmap="glacial",
734+
colorbar="t",
735+
colorbar_kw=dict(
736+
align="r",
737+
length=0.5,
738+
),
739+
)
740+
ax.format(oceancolor="blue", coast=True)
741+
return fig

0 commit comments

Comments
 (0)