Skip to content

Commit b21c21e

Browse files
authored
Fix DMS not set on some projections (#293)
* don't set the formatter; handle through init * add test * rm lon0 logic * restore lazy loading * more restoring * more restoring * replace logic with rectilinear * update test * update tests * recover * revert most changes * reshuffle some code and rm dead code * fix and slight shuffle * update unittest * mv logic back * mv logic back * mv logic back * mv logic back * mv logic back * rm print * don't need this anymore * make error more helpful
1 parent 1d71642 commit b21c21e

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

ultraplot/axes/geo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,10 @@ def projection(self, map_projection):
12731273
if not isinstance(map_projection, cls):
12741274
raise ValueError(f"Projection must be a {cls} instance.")
12751275
self._map_projection = map_projection
1276+
if hasattr(self, "_lonaxis") or hasattr(self, "_lataxis"):
1277+
# Update the projection of the lon and lat axes
1278+
self._lonaxis.get_major_formatter()._source_projection = map_projection
1279+
self._lataxis.get_major_formatter()._source_projection = map_projection
12761280

12771281

12781282
class _CartopyAxes(GeoAxes, _GeoAxes):

ultraplot/tests/test_geographic.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def test_lon0_shifts():
236236
# abs is taken due to north-west
237237
str_loc = str(abs(int(loc)))
238238
n = len(str_loc)
239-
assert str_loc == format[:n]
239+
assert str_loc == format[:n], f"Epxected: {str_loc}, got: {format[:n]}"
240240
assert locs[0] != 0 # we should not be a 0 anymore
241241
uplt.close(fig)
242242

@@ -850,3 +850,27 @@ def test_consistent_range():
850850

851851
assert np.allclose(lonview, lonlim)
852852
assert np.allclose(latview, latlim)
853+
854+
855+
@pytest.mark.mpl_image_compare
856+
def test_dms_used_for_mercator():
857+
"""
858+
Test that DMS is used for Mercator projection
859+
"""
860+
limit = (0.6, 113.25)
861+
fig, ax = uplt.subplots(ncols=2, proj=("cyl", "merc"), share=0)
862+
ax.format(land=True, labels=True, lonlocator=limit)
863+
ax.format(land=True, labels=True, lonlocator=limit)
864+
import matplotlib.ticker as mticker
865+
866+
expectations = (
867+
"0°36′E",
868+
"113°15′E",
869+
)
870+
871+
for expectation, tick in zip(expectations, limit):
872+
a = ax[0].gridlines_major.xformatter(tick)
873+
b = ax[1].gridlines_major.xformatter(tick)
874+
assert a == expectation
875+
assert b == expectation
876+
return fig

ultraplot/ticker.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -884,18 +884,6 @@ def __init__(self, lon0=0, *args, **kwargs):
884884
self.lon0 = lon0
885885
super().__init__(*args, **kwargs)
886886

887-
def __call__(self, x, pos=None):
888-
"""
889-
Format the longitude, accounting for lon0 offset.
890-
"""
891-
# Adjust longitude value based on lon0
892-
adjusted_lon = x - self.lon0
893-
# Normalize to -180 to 180 range
894-
adjusted_lon = ((adjusted_lon + 180) % 360) - 180
895-
print(x)
896-
# Use the original formatter with the adjusted longitude
897-
return super().__call__(adjusted_lon, pos)
898-
899887

900888
class LatitudeFormatter(_CartopyFormatter, LatitudeFormatter):
901889
"""

0 commit comments

Comments
 (0)