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

Skip to content

Commit 96c6168

Browse files
weiji-liksunden
authored andcommitted
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 cf30ce7 commit 96c6168

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":
245247
if pos_only in kwargs:
@@ -496,10 +498,12 @@ def _plot_args(self, tup, kwargs, *,
496498
else:
497499
x, y = index_of(xy[-1])
498500

499-
if self.axes.xaxis is not None:
500-
self.axes.xaxis.update_units(x)
501-
if self.axes.yaxis is not None:
502-
self.axes.yaxis.update_units(y)
501+
# use the axes from kwargs if provided when updating units
502+
axes = kwargs.get("axes", self.axes)
503+
if axes.xaxis is not None:
504+
axes.xaxis.update_units(x)
505+
if axes.yaxis is not None:
506+
axes.yaxis.update_units(y)
503507

504508
if x.shape[0] != y.shape[0]:
505509
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
@@ -3,6 +3,7 @@
33
import matplotlib.transforms as mtransforms
44
from matplotlib.transforms import Bbox
55
from .mpl_axes import Axes
6+
import functools
67

78

89
class ParasiteAxesBase:
@@ -18,7 +19,8 @@ def __init__(self, parent_axes, aux_transform=None,
1819
def clear(self):
1920
super().clear()
2021
martist.setp(self.get_children(), visible=False)
21-
self._get_lines = self._parent_axes._get_lines
22+
self._get_lines = functools.partial(
23+
self._parent_axes._get_lines, axes=self)
2224
self._parent_axes.callbacks._connect_picklable(
2325
"xlim_changed", self._sync_lims)
2426
self._parent_axes.callbacks._connect_picklable(

lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
zoomed_inset_axes, mark_inset, inset_axes, BboxConnectorPatch,
2626
InsetPosition)
2727
import mpl_toolkits.axes_grid1.mpl_axes
28-
28+
import matplotlib.units as units
29+
import matplotlib.ticker as ticker
2930
import pytest
3031

3132
import numpy as np
@@ -87,6 +88,43 @@ def test_twin_axes_empty_and_removed():
8788
plt.subplots_adjust(wspace=0.5, hspace=1)
8889

8990

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

0 commit comments

Comments
 (0)
0