Skip to content

Fix DMS not set on some projections #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ultraplot/axes/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,10 @@ def projection(self, map_projection):
if not isinstance(map_projection, cls):
raise ValueError(f"Projection must be a {cls} instance.")
self._map_projection = map_projection
if hasattr(self, "_lonaxis") or hasattr(self, "_lataxis"):
# Update the projection of the lon and lat axes
self._lonaxis.get_major_formatter()._source_projection = map_projection
self._lataxis.get_major_formatter()._source_projection = map_projection


class _CartopyAxes(GeoAxes, _GeoAxes):
Expand Down
26 changes: 25 additions & 1 deletion ultraplot/tests/test_geographic.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def test_lon0_shifts():
# abs is taken due to north-west
str_loc = str(abs(int(loc)))
n = len(str_loc)
assert str_loc == format[:n]
assert str_loc == format[:n], f"Epxected: {str_loc}, got: {format[:n]}"
assert locs[0] != 0 # we should not be a 0 anymore
uplt.close(fig)

Expand Down Expand Up @@ -850,3 +850,27 @@ def test_consistent_range():

assert np.allclose(lonview, lonlim)
assert np.allclose(latview, latlim)


@pytest.mark.mpl_image_compare
def test_dms_used_for_mercator():
"""
Test that DMS is used for Mercator projection
"""
limit = (0.6, 113.25)
fig, ax = uplt.subplots(ncols=2, proj=("cyl", "merc"), share=0)
ax.format(land=True, labels=True, lonlocator=limit)
ax.format(land=True, labels=True, lonlocator=limit)
import matplotlib.ticker as mticker

expectations = (
"0°36′E",
"113°15′E",
)

for expectation, tick in zip(expectations, limit):
a = ax[0].gridlines_major.xformatter(tick)
b = ax[1].gridlines_major.xformatter(tick)
assert a == expectation
assert b == expectation
return fig
12 changes: 0 additions & 12 deletions ultraplot/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,18 +884,6 @@ def __init__(self, lon0=0, *args, **kwargs):
self.lon0 = lon0
super().__init__(*args, **kwargs)

def __call__(self, x, pos=None):
"""
Format the longitude, accounting for lon0 offset.
"""
# Adjust longitude value based on lon0
adjusted_lon = x - self.lon0
# Normalize to -180 to 180 range
adjusted_lon = ((adjusted_lon + 180) % 360) - 180
print(x)
# Use the original formatter with the adjusted longitude
return super().__call__(adjusted_lon, pos)


class LatitudeFormatter(_CartopyFormatter, LatitudeFormatter):
"""
Expand Down