10000 Support standard formatters in axisartist. · matplotlib/matplotlib@f699d50 · GitHub
[go: up one dir, main page]

Skip to content

Commit f699d50

Browse files
committed
Support standard formatters in axisartist.
Ideally, axisartist-specific formatters (and locators) should be deprecated and removed in the future.
1 parent a4dca24 commit f699d50

File tree

6 files changed

+38
-24
lines changed

6 files changed

+38
-24
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``axisartist`` can now be used together with standard ``Formatters``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... instead of being limited to axisartist-specific ones.

lib/mpl_toolkits/axisartist/floating_axes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ def _update_grid(self, x1, y1, x2, y2):
222222
grid_info["lon_info"] = lon_levs, lon_n, lon_factor
223223
grid_info["lat_info"] = lat_levs, lat_n, lat_factor
224224

225-
grid_info["lon_labels"] = grid_finder.tick_formatter1(
226-
"bottom", lon_factor, lon_levs)
227-
grid_info["lat_labels"] = grid_finder.tick_formatter2(
228-
"bottom", lat_factor, lat_levs)
225+
grid_info["lon_labels"] = grid_finder._format_ticks(
226+
1, "bottom", lon_factor, lon_levs)
227+
grid_info["lat_labels"] = grid_finder._format_ticks(
228+
2, "bottom", lat_factor, lat_levs)
229229

230230
lon_values = lon_levs[:lon_n] / lon_factor
231231
lat_values = lat_levs[:lat_n] / lat_factor

lib/mpl_toolkits/axisartist/grid_finder.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
from matplotlib import ticker as mticker
3+
from matplotlib import ticker as mticker, _api
44
from matplotlib.transforms import Bbox, Transform
55

66

@@ -150,6 +150,19 @@ def __init__(self,
150150
self.tick_formatter2 = tick_formatter2
151151
self.set_transform(transform)
152152

153+
def _format_ticks(self, idx, direction, factor, levels):
154+
"""
155+
Helper to support both standard formatters (inheriting from
156+
`.mticker.Formatter`) and axisartist-specific ones; should be called instead of
157+
directly calling ``self.tick_formatter1`` and ``self.tick_formatter2``. This
158+
method should be considered as a temporary workaround which will be removed in
159+
the future at the same time as axisartist-specific formatters.
160+
"""
161+
fmt = _api.check_getitem(
162+
{1: self.tick_formatter1, 2: self.tick_formatter2}, idx=idx)
163+
return (fmt.format_ticks(levels) if isinstance(fmt, mticker.Formatter)
164+
else fmt(direction, factor, levels))
165+
153166
def get_grid_info(self, x1, y1, x2, y2):
154167
"""
155168
lon_values, lat_values : list of grid values. if integer is given,
@@ -192,14 +205,14 @@ def get_grid_info(self, x1, y1, x2, y2):
192205
tck_labels = grid_info["lon"]["tick_labels"] = {}
193206
for direction in ["left", "bottom", "right", "top"]:
194207
levs = grid_info["lon"]["tick_levels"][direction]
195-
tck_labels[direction] = self.tick_formatter1(
196-
direction, lon_factor, levs)
208+
tck_labels[direction] = self._format_ticks(
209+
1, direction, lon_factor, levs)
197210

198211
tck_labels = grid_info["lat"]["tick_labels"] = {}
199212
for direction in ["left", "bottom", "right", "top"]:
200213
levs = grid_info["lat"]["tick_levels"][direction]
201-
tck_labels[direction] = self.tick_formatter2(
202-
direction, lat_factor, levs)
214+
tck_labels[direction] = self._format_ticks(
215+
2, direction, lat_factor, levs)
203216

204217
return grid_info
205218

lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ def update_lim(self, axes):
137137
"extremes": (lon_min, lon_max, lat_min, lat_max),
138138
"lon_info": (lon_levs, lon_n, np.asarray(lon_factor)),
139139
"lat_info": (lat_levs, lat_n, np.asarray(lat_factor)),
140-
"lon_labels": grid_finder.tick_formatter1(
141-
"bottom", lon_factor, lon_levs),
142-
"lat_labels": grid_finder.tick_formatter2(
143-
"bottom", lat_factor, lat_levs),
140+
"lon_labels": grid_finder._format_ticks(
141+
1, "bottom", lon_factor, lon_levs),
142+
"lat_labels": grid_finder._format_ticks(
143+
2, "bottom", lat_factor, lat_levs),
144144
"line_xy": (xx, yy),
145145
}
146146

Loading

lib/mpl_toolkits/axisartist/tests/test_grid_helper_curvelinear.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import matplotlib.pyplot as plt
44
from matplotlib.path import Path
55
from matplotlib.projections import PolarAxes
6+
from matplotlib.ticker import FuncFormatter
67
from matplotlib.transforms import Affine2D, Transform
78
from matplotlib.testing.decorators import image_comparison
89

@@ -75,11 +76,8 @@ def inverted(self):
7576
ax1.grid(True)
7677

7778

78-
# Remove tol & kerning_factor when this test image is regenerated.
79-
@image_comparison(['polar_box.png'], style='default', tol=0.27)
79+
@image_comparison(['polar_box.png'], style='default', tol=0.02)
8080
def test_polar_box():
81-
plt.rcParams['text.kerning_factor'] = 6
82-
8381
fig = plt.figure(figsize=(5, 5))
8482

8583
# PolarAxes.PolarTransform takes radian. However, we want our coordinate
@@ -95,13 +93,13 @@ def test_polar_box():
9593
lon_minmax=None,
9694
lat_minmax=(0, np.inf))
9795

98-
grid_locator1 = angle_helper.LocatorDMS(12)
99-
tick_formatter1 = angle_helper.FormatterDMS()
100-
101-
grid_helper = GridHelperCurveLinear(tr,
102-
extreme_finder=extreme_finder,
103-
grid_locator1=grid_locator1,
104-
tick_formatter1=tick_formatter1)
96+
grid_helper = GridHelperCurveLinear(
97+
tr,
98+
extreme_finder=extreme_finder,
99+
grid_locator1=angle_helper.LocatorDMS(12),
100+
tick_formatter1=angle_helper.FormatterDMS(),
101+
tick_formatter2=FuncFormatter(lambda x, p: "eight" if x == 8 else f"{int(x)}"),
102+
)
105103

106104
ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)
107105

0 commit comments

Comments
 (0)
0