Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita622508

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 axesThis 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 parentc603cc6 commita622508

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
forpos_onlyin"xy":
245247
ifpos_onlyinkwargs:
@@ -488,10 +490,12 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
488490
else:
489491
x,y=index_of(xy[-1])
490492

491-
ifself.axes.xaxisisnotNone:
492-
self.axes.xaxis.update_units(x)
493-
ifself.axes.yaxisisnotNone:
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+
ifaxes.xaxisisnotNone:
496+
axes.xaxis.update_units(x)
497+
ifaxes.yaxisisnotNone:
498+
axes.yaxis.update_units(y)
495499

496500
ifx.shape[0]!=y.shape[0]:
497501
raiseValueError(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
frommatplotlib.axesimportsubplot_class_factory
66
frommatplotlib.transformsimportBbox
77
from .mpl_axesimportAxes
8+
importfunctools
89

910

1011
classParasiteAxesBase:
@@ -20,7 +21,8 @@ def __init__(self, parent_axes, aux_transform=None,
2021
defcla(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
defget_images_artists(self):

‎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
frommpl_toolkits.axes_grid1.inset_locatorimport (
2222
zoomed_inset_axes,mark_inset,inset_axes,BboxConnectorPatch)
2323
importmpl_toolkits.axes_grid1.mpl_axes
24-
24+
importmatplotlib.unitsasunits
25+
importmatplotlib.tickerasticker
2526
importpytest
2627

2728
importnumpyasnp
@@ -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+
deftest_twin_axes_both_with_units():
90+
mpl.rcParams.update(
91+
{"font.size":8,"xtick.labelsize":8,"ytick.labelsize":8})
92+
classTestUnit:
93+
def__init__(self,val):
94+
self._val=val
95+
96+
classUnitA(TestUnit):
97+
fmt="%0.1f Unit A"
98+
classUnitB(TestUnit):
99+
fmt="%0.1f Unit B"
100+
101+
classUnitConverter(units.ConversionInterface):
102+
@staticmethod
103+
defconvert(value,unit,axis):
104+
return [x._valforxinvalue]
105+
106+
@staticmethod
107+
defaxisinfo(unit,axis):
108+
returnunits.AxisInfo(majfmt=ticker.FormatStrFormatter(unit.fmt))
109+
110+
@staticmethod
111+
defdefault_units(x,axis):
112+
returnx[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)forxin (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)forxin (0,2,2)])
123+
124+
87125
deftest_axesgrid_colorbar_log_smoketest():
88126
fig=plt.figure()
89127
grid=AxesGrid(fig,111,# modified to be only subplot

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp