|
1 | 1 | import numpy as np |
2 | 2 |
|
3 | | -from matplotlib import ticker as mticker |
| 3 | +from matplotlib import ticker as mticker, _api |
4 | 4 | from matplotlib.transforms import Bbox, Transform |
5 | 5 |
|
6 | 6 |
|
@@ -150,6 +150,19 @@ def __init__(self, |
150 | 150 | self.tick_formatter2 = tick_formatter2 |
151 | 151 | self.set_transform(transform) |
152 | 152 |
|
| 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 | + |
153 | 166 | def get_grid_info(self, x1, y1, x2, y2): |
154 | 167 | """ |
155 | 168 | 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): |
192 | 205 | tck_labels = grid_info["lon"]["tick_labels"] = {} |
193 | 206 | for direction in ["left", "bottom", "right", "top"]: |
194 | 207 | 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) |
197 | 210 |
|
198 | 211 | tck_labels = grid_info["lat"]["tick_labels"] = {} |
199 | 212 | for direction in ["left", "bottom", "right", "top"]: |
200 | 213 | 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) |
203 | 216 |
|
204 | 217 | return grid_info |
205 | 218 |
|
|
0 commit comments