8000 Support standard formatters in axisartist. by anntzer · Pull Request #26586 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Support standard formatters in axisartist. #26586

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

Merged
merged 1 commit into from
Sep 27, 2023
Merged
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
3 changes: 3 additions & 0 deletions doc/users/next_whats_new/stdfmt-axisartist.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``axisartist`` can now be used together with standard ``Formatters``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... instead of being limited to axisartist-specific ones.
8 changes: 4 additions & 4 deletions lib/mpl_toolkits/axisartist/floating_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ def _update_grid(self, x1, y1, x2, y2):
grid_info["lon_info"] = lon_levs, lon_n, lon_factor
grid_info["lat_info"] = lat_levs, lat_n, lat_factor

grid_info["lon_labels"] = grid_finder.tick_formatter1(
"bottom", lon_factor, lon_levs)
grid_info["lat_labels"] = grid_finder.tick_formatter2(
"bottom", lat_factor, lat_levs)
grid_info["lon_labels"] = grid_finder._format_ticks(
1, "bottom", lon_factor, lon_levs)
grid_info["lat_labels"] = grid_finder._format_ticks(
2, "bottom", lat_factor, lat_levs)

lon_values = lon_levs[:lon_n] / lon_factor
lat_values = lat_levs[:lat_n] / lat_factor
Expand Down
23 changes: 18 additions & 5 deletions lib/mpl_toolkits/axisartist/grid_finder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from matplotlib import ticker as mticker
from matplotlib import ticker as mticker, _api
from matplotlib.transforms import Bbox, Transform


Expand Down Expand Up @@ -150,6 +150,19 @@ def __init__(self,
self.tick_formatter2 = tick_formatter2
self.set_transform(transform)

def _format_ticks(self, idx, direction, factor, levels):
"""
Helper to support both standard formatters (inheriting from
`.mticker.Formatter`) and axisartist-specific ones; should be called instead of
directly calling ``self.tick_formatter1`` and ``self.tick_formatter2``. This
method should be considered as a temporary workaround which will be removed in
the future at the same time as axisartist-specific formatters.
"""
fmt = _api.check_getitem(
{1: self.tick_formatter1, 2: self.tick_formatter2}, idx=idx)
return (fmt.format_ticks(levels) if isinstance(fmt, mticker.Formatter)
else fmt(direction, factor, levels))

def get_grid_info(self, x1, y1, x2, y2):
"""
lon_values, lat_values : list of grid values. if integer is given,
Expand Down Expand Up @@ -192,14 +205,14 @@ def get_grid_info(self, x1, y1, x2, y2):
tck_labels = grid_info["lon"]["tick_labels"] = {}
for direction in ["left", "bottom", "right", "top"]:
levs = grid_info["lon"]["tick_levels"][direction]
tck_labels[direction] = self.tick_formatter1(
direction, lon_factor, levs)
tck_labels[direction] = self._format_ticks(
1, direction, lon_factor, levs)
< 8000 span class='blob-code-inner blob-code-marker ' data-code-marker=" ">
tck_labels = grid_info["lat"]["tick_labels"] = {}
for direction in ["left", "bottom", "right", "top"]:
levs = grid_info["lat"]["tick_levels"][direction]
tck_labels[direction] = self.tick_formatter2(
direction, lat_factor, levs)
tck_labels[direction] = self._format_ticks(
2, direction, lat_factor, levs)

return grid_info

Expand Down
8 changes: 4 additions & 4 deletions lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ def update_lim(self, axes):
"extremes": (lon_min, lon_max, lat_min, lat_max),
"lon_info": (lon_levs, lon_n, np.asarray(lon_factor)),
"lat_info": (lat_levs, lat_n, np.asarray(lat_factor)),
"lon_labels": grid_finder.tick_formatter1(
"bottom", lon_factor, lon_levs),
"lat_labels": grid_finder.tick_formatter2(
"bottom", lat_factor, lat_levs),
"lon_labels": grid_finder._format_ticks(
1, "bottom", lon_factor, lon_levs),
"lat_labels": grid_finder._format_ticks(
2, "bottom", lat_factor, lat_levs),
"line_xy": (xx, yy),
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 9 additions & 11 deletions lib/mpl_toolkits/axisartist/tests/test_grid_helper_curvelinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.projections import PolarAxes
from matplotlib.ticker import FuncFormatter
from matplotlib.transforms import Affine2D, Transform
from matplotlib.testing.decorators import image_comparison

Expand Down Expand Up @@ -75,11 +76,8 @@ def inverted(self):
ax1.grid(True)


# Remove tol & kerning_factor when this test image is regenerated.
@image_comparison(['polar_box.png'], style='default', tol=0.27)
@image_comparison(['polar_box.png'], style='default', tol=0.02)
def test_polar_box():
plt.rcParams['text.kerning_factor'] = 6

fig = plt.figure(figsize=(5, 5))

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

grid_locator1 = angle_helper.LocatorDMS(12)
tick_formatter1 = angle_helper.FormatterDMS()

grid_helper = GridHelperCurveLinear(tr,
extreme_finder=extreme_finder,
grid_locator1=grid_locator1,
tick_formatter1=tick_formatter1)
grid_helper = GridHelperCurveLinear(
tr,
extreme_finder=extreme_finder,
grid_locator1=angle_helper.LocatorDMS(12),
tick_formatter1=angle_helper.FormatterDMS(),
tick_formatter2=FuncFormatter(lambda x, p: "eight" if x == 8 else f"{int(x)}"),
)

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

Expand Down
0