8000 Use axes=self as an extra args in parasite_axes · matplotlib/matplotlib@a622508 · GitHub
[go: up one dir, main page]

Skip to content

Commit a622508

Browse files
committed
Use axes=self as an extra args in parasite_axes
Right now parasite_axes just use self._parent_axes._get_lines as self._get_lines, but it can't update the axes unit when there are twin axes. Therefore, we need to provide axes=self as an extra args to handle this. We also need to change the callees to use axes in kwargs when provided. Add a unit test for missing unit in twin axes This test creates a plot with twin axes where both axes have units. It then checks whether units are appended correctly on the respective axes. The code base without the modification fails the unit test whereas the modification makes it pass the unit test.t:q`
1 parent c603cc6 commit a622508

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ def set_prop_cycle(self, cycler):
239239
self._prop_keys = cycler.keys # This should make a copy
240240

241241
def __call__(self, *args, data=None, **kwargs):
242-
self.axes._process_unit_info(kwargs=kwargs)
242+
# use the axes from kwargs if provided
243+
axes = kwargs.get("axes", self.axes)
244+
axes._process_unit_info(kwargs=kwargs)
243245

244246
for pos_only in "xy":
8000 245247
if pos_only in kwargs:
@@ -488,10 +490,12 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
488490
else:
489491
x, y = index_of(xy[-1])
490492

491-
if self.axes.xaxis is not None:
492-
self.axes.xaxis.update_units(x)
493-
if self.axes.yaxis is not None:
494-
self.axes.yaxis.update_units(y)
493+
# use the axes from kwargs if provided when updating units
494+
axes = kwargs.get("axes", self.axes)
495+
if axes.xaxis is not None:
496+
axes.xaxis.update_units(x)
497+
if axes.yaxis is not None:
498+
axes.yaxis.update_units(y)
495499

496500
if x.shape[0] != y.shape[0]:
497501
raise ValueError(f"x and y must have same first dimension, but "

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from matplotlib.axes import subplot_class_factory
66
from matplotlib.transforms import Bbox
77
from .mpl_axes import Axes
8+
import functools
89

910

1011
class ParasiteAxesBase:
@@ -20,7 +21,8 @@ def __init__(self, parent_axes, aux_transform=None,
2021
def cla(self):
2122
super().cla()
2223
martist.setp(self.get_children(), visible=False)
23-
self._get_lines = self._parent_axes._get_lines
24+
self._get_lines = functools.partial(
25+
self._parent_axes._get_lines, axes=self)
2426

2527
@_api.deprecated("3.5")
2628
def get_images_artists(self):
Loading

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from mpl_toolkits.axes_grid1.inset_locator import (
2222
zoomed_inset_axes, mark_inset, inset_axes, BboxConnectorPatch)
2323
import mpl_toolkits.axes_grid1.mpl_axes
24-
24+
import matplotlib.units as units
25+
import matplotlib.ticker as ticker
2526
import pytest
2627

2728
import numpy as np
@@ -84,6 +85,43 @@ def test_twin_axes_empty_and_removed():
8485
plt.subplots_adjust(wspace=0.5, hspace=1)
8586

8687

88+
@image_comparison(['twin_axes_both_with_units.png'])
89+
def test_twin_axes_both_with_units():
90+
mpl.rcParams.update(
91+
{"font.size": 8, "xtick.labelsize": 8, "ytick.labelsize": 8})
92+
class TestUnit:
93+
def __init__(self, val):
94+
self._val = val
95+
96+
class UnitA(TestUnit):
97+
fmt = "%0.1f Unit A"
98+
class UnitB(TestUnit):
99+
fmt = "%0.1f Unit B"
100+
101+
class UnitConverter(units.ConversionInterface):
102+
@staticmethod
103+
def convert(value, unit, axis):
104+
return [x._val for x in value]
105+
106+
@staticmethod
107+
def axisinfo(unit, axis):
108+
return units.AxisInfo(majfmt=ticker.FormatStrFormatter(unit.fmt))
109+
110+
@staticmethod
111+
def default_units(x, axis):
112+
return x[0].__class__
113+
114+
units.registry[UnitA] = UnitConverter()
115+
units.registry[UnitB] = UnitConverter()
116+
117+
host = host_subplot(111)
118+
119+
p1, = host.plot([0, 1, 2], [UnitA(x) for x in (0, 1, 2)])
120+
par1 = host.twinx()
121+
par1.axis["right"].major_ticklabels.set_visible(True)
122+
p2, = par1.plot([0, 1, 2], [UnitB(x) for x in (0, 2, 2)])
123+
124+
87125
def test_axesgrid_colorbar_log_smoketest():
88126
fig = plt.figure()
89127
grid = AxesGrid(fig, 111, # modified to be only subplot

0 commit comments

Comments
 (0)
0