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

Commit2960490

Browse files
committed
FIX: restore creating new axes via plt.subplot with different kwargs
This adds a small amount of additional state to the Axes created viaFigure.add_axes and Figure.add_subplot (which the other Axes creationmethods eventually funnel through) to track the kwargs passedthrough.We then use that state in `pyplot.subplot` to determine if we shouldre-use an Axes found at a given position or create a new one (andimplicitly destroy the existing one).closes#19432
1 parentea68032 commit2960490

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

‎lib/matplotlib/figure.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,12 @@ def add_axes(self, *args, **kwargs):
576576
ifnotnp.isfinite(rect).all():
577577
raiseValueError('all entries in rect must be finite '
578578
'not {}'.format(rect))
579-
projection_class,kwargs=self._process_projection_requirements(
579+
projection_class,pkw=self._process_projection_requirements(
580580
*args,**kwargs)
581581

582582
# create the new axes using the axes class given
583-
a=projection_class(self,rect,**kwargs)
584-
returnself._add_axes_internal(a)
583+
a=projection_class(self,rect,**pkw)
584+
returnself._add_axes_internal(a,kwargs)
585585

586586
@docstring.dedent_interpd
587587
defadd_subplot(self,*args,**kwargs):
@@ -706,17 +706,18 @@ def add_subplot(self, *args, **kwargs):
706706
if (len(args)==1andisinstance(args[0],Integral)
707707
and100<=args[0]<=999):
708708
args=tuple(map(int,str(args[0])))
709-
projection_class,kwargs=self._process_projection_requirements(
709+
projection_class,pkw=self._process_projection_requirements(
710710
*args,**kwargs)
711-
ax=subplot_class_factory(projection_class)(self,*args,**kwargs)
712-
returnself._add_axes_internal(ax)
711+
ax=subplot_class_factory(projection_class)(self,*args,**pkw)
712+
returnself._add_axes_internal(ax,kwargs)
713713

714-
def_add_axes_internal(self,ax):
714+
def_add_axes_internal(self,ax,input_kwargs):
715715
"""Private helper for `add_axes` and `add_subplot`."""
716716
self._axstack.push(ax)
717717
self._localaxes.push(ax)
718718
self.sca(ax)
719719
ax._remove_method=self.delaxes
720+
ax._init_kwargs=input_kwargs
720721
self.stale=True
721722
ax.stale_callback=_stale_figure_callback
722723
returnax

‎lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,8 +1229,8 @@ def subplot(*args, **kwargs):
12291229
ifhasattr(ax,'get_subplotspec')andax.get_subplotspec()==key),
12301230
None)
12311231

1232-
# If no existing axesmatch, then create a new one.
1233-
ifaxisNone:
1232+
# If no existing axesmatches, then create a new one.
1233+
ifaxisNoneorgetattr(ax,'_init_kwargs', {})!=kwargs:
12341234
ax=fig.add_subplot(*args,**kwargs)
12351235

12361236
bbox=ax.bbox

‎lib/matplotlib/tests/test_figure.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,18 +1010,27 @@ def test_axes_kwargs():
10101010
plt.close()
10111011

10121012
# plt.subplot() searches for axes with the same subplot spec, and if one
1013-
# exists, returns it,regardless of whether the axes kwargs were the same.
1013+
# exists,and the kwargs matchreturns it,create a new one if they do not
10141014
fig=plt.figure()
10151015
ax=plt.subplot(1,2,1)
10161016
ax1=plt.subplot(1,2,1)
10171017
ax2=plt.subplot(1,2,2)
1018+
# This will delete ax / ax1 as they fully overlap
10181019
ax3=plt.subplot(1,2,1,projection='polar')
1020+
ax4=plt.subplot(1,2,1,projection='polar')
10191021
assertaxisnotNone
10201022
assertax1isax
10211023
assertax2isnotax
1022-
assertax3isax
1024+
assertax3isnotax
1025+
assertax3isax4
1026+
1027+
assertaxnotinfig.axes
1028+
assertax2infig.axes
1029+
assertax3infig.axes
1030+
10231031
assertax.name=='rectilinear'
1024-
assertax3.name=='rectilinear'
1032+
assertax2.name=='rectilinear'
1033+
assertax3.name=='polar'
10251034
plt.close()
10261035

10271036
# plt.gca() returns an existing axes, unless there were no axes.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp