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

Commit2c03e25

Browse files
committed
Make kwargs names in scale.py not include the axis direction.
- Make names of kwargs to Scale subclasses independent of whether the underlying axis is x or y (so that after the deprecation period, one can just have a normal signature -- `def __init__(self, *, base=10, ...)`. We could possibly also change semilogx and semilogy to use the unsuffixed names (with deprecation), leaving only loglog with the suffixed names, or even also make loglog use unsuffixed names, in which case it would become impossible to set separate x and y bases directly in loglog -- one should then do `gca().set_xscale("log", base=...)` which doesn't seem too onerous. (loglog is the only case where the suffixed names has *some* utility.) In general, note that having kwargs that depend on the axis direction doesn't really scale -- for example, if we were to finally add log-scale support to mplot3d, should scale.py know about it and add `basez`? Should we have `baser` for log-scale polar plots? (at least in the radial direction, they make sense)- Move argument validation up the the Transform subclasses.- Make SymmetricalLogScale.{base,linthresh,linscale} properties mapping to the underlying transform so that they can't go out of sync.
1 parent53c8d6a commit2c03e25

File tree

9 files changed

+93
-100
lines changed

9 files changed

+93
-100
lines changed

‎doc/api/next_api_changes/deprecations.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,13 @@ Debian 8 (2015, EOL 06/2020) and Ubuntu 14.04 (EOL 04/2019) were the
184184
last versions of Debian and Ubuntu to ship avconv. It remains possible
185185
to force the use of avconv by using the ffmpeg-based writers with
186186
:rc:`animation.ffmpeg_path` set to "avconv".
187+
188+
Signature of `.LogScale` and `.SymmetricalLogScale`
189+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190+
These classes used to take keyword arguments that depends on the axis
191+
orientation ("basex" vs "basey", "nonposx" vs "nonposy"); these parameter
192+
names are now deprecated in favor of "base", "nonpos", etc. This deprecation
193+
also affects e.g. ``ax.set_yscale("log", basey=...)`` which must now be
194+
spelled ``ax.set_yscale("log", base=...)``. The only place where "basex", etc.
195+
remain in use is in the helper functions `~.Axes.loglog`, `~.Axes.semilogx`,
196+
and `~.Axes.semilogy` (because `~.Axes.loglog` takes both "basex" and "basey").

‎examples/scales/log_demo.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
ax2.grid()
2727

2828
# log x and y axis
29-
ax3.loglog(t,20*np.exp(-t/10.0),basex=2)
29+
ax3.loglog(t,20*np.exp(-t/10.0))
30+
ax3.set_xscale('log',base=2)
3031
ax3.set(title='loglog base 2 on x')
3132
ax3.grid()
3233

@@ -35,8 +36,8 @@
3536
x=10.0**np.linspace(0.0,2.0,20)
3637
y=x**2.0
3738

38-
ax4.set_xscale("log",nonposx='clip')
39-
ax4.set_yscale("log",nonposy='clip')
39+
ax4.set_xscale("log",nonpos='clip')
40+
ax4.set_yscale("log",nonpos='clip')
4041
ax4.set(title='Errorbars go negative')
4142
ax4.errorbar(x,y,xerr=0.1*x,yerr=5.0+0.75*y)
4243
# ylim must be set after errorbar to allow errorbar to autoscale limits

‎examples/scales/scales.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
# symmetric log
4646
ax=axs[1,1]
4747
ax.plot(x,y-y.mean())
48-
ax.set_yscale('symlog',linthreshy=0.02)
48+
ax.set_yscale('symlog',linthresh=0.02)
4949
ax.set_title('symlog')
5050
ax.grid(True)
5151

‎examples/scales/symlog_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
plt.subplot(313)
2828
plt.plot(x,np.sin(x/3.0))
2929
plt.xscale('symlog')
30-
plt.yscale('symlog',linthreshy=0.015)
30+
plt.yscale('symlog',linthresh=0.015)
3131
plt.grid(True)
3232
plt.ylabel('symlog both')
3333

‎lib/matplotlib/axes/_axes.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,16 +1795,13 @@ def loglog(self, *args, **kwargs):
17951795
**kwargs
17961796
All parameters supported by `.plot`.
17971797
"""
1798-
dx= {k:kwargs.pop(k)forkin ['basex','subsx','nonposx']
1798+
dx= {k[:-1]:kwargs.pop(k)forkin ['basex','subsx','nonposx']
17991799
ifkinkwargs}
1800-
dy= {k:kwargs.pop(k)forkin ['basey','subsy','nonposy']
1801-
ifkinkwargs}
1802-
18031800
self.set_xscale('log',**dx)
1801+
dy= {k[:-1]:kwargs.pop(k)forkin ['basey','subsy','nonposy']
1802+
ifkinkwargs}
18041803
self.set_yscale('log',**dy)
1805-
1806-
l=self.plot(*args,**kwargs)
1807-
returnl
1804+
returnself.plot(*args,**kwargs)
18081805

18091806
# @_preprocess_data() # let 'plot' do the unpacking..
18101807
@docstring.dedent_interpd
@@ -1848,12 +1845,10 @@ def semilogx(self, *args, **kwargs):
18481845
**kwargs
18491846
All parameters supported by `.plot`.
18501847
"""
1851-
d= {k:kwargs.pop(k)forkin ['basex','subsx','nonposx']
1848+
d= {k[:-1]:kwargs.pop(k)forkin ['basex','subsx','nonposx']
18521849
ifkinkwargs}
1853-
18541850
self.set_xscale('log',**d)
1855-
l=self.plot(*args,**kwargs)
1856-
returnl
1851+
returnself.plot(*args,**kwargs)
18571852

18581853
# @_preprocess_data() # let 'plot' do the unpacking..
18591854
@docstring.dedent_interpd
@@ -1897,12 +1892,10 @@ def semilogy(self, *args, **kwargs):
18971892
**kwargs
18981893
All parameters supported by `.plot`.
18991894
"""
1900-
d= {k:kwargs.pop(k)forkin ['basey','subsy','nonposy']
1895+
d= {k[:-1]:kwargs.pop(k)forkin ['basey','subsy','nonposy']
19011896
ifkinkwargs}
19021897
self.set_yscale('log',**d)
1903-
l=self.plot(*args,**kwargs)
1904-
1905-
returnl
1898+
returnself.plot(*args,**kwargs)
19061899

19071900
@_preprocess_data(replace_names=["x"],label_namer="x")
19081901
defacorr(self,x,**kwargs):
@@ -2343,11 +2336,11 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
23432336
iforientation=='vertical':
23442337
self._process_unit_info(xdata=x,ydata=height,kwargs=kwargs)
23452338
iflog:
2346-
self.set_yscale('log',nonposy='clip')
2339+
self.set_yscale('log',nonpos='clip')
23472340
eliforientation=='horizontal':
23482341
self._process_unit_info(xdata=width,ydata=y,kwargs=kwargs)
23492342
iflog:
2350-
self.set_xscale('log',nonposx='clip')
2343+
self.set_xscale('log',nonpos='clip')
23512344

23522345
# lets do some conversions now since some types cannot be
23532346
# subtracted uniformly
@@ -6715,9 +6708,9 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
67156708

67166709
iflog:
67176710
iforientation=='horizontal':
6718-
self.set_xscale('log',nonposx='clip')
6711+
self.set_xscale('log',nonpos='clip')
67196712
else:# orientation == 'vertical'
6720-
self.set_yscale('log',nonposy='clip')
6713+
self.set_yscale('log',nonpos='clip')
67216714

67226715
ifalign=='left':
67236716
x-=0.5*(bins[1]-bins[0])

‎lib/matplotlib/scale.py

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,11 @@ class LogTransform(Transform):
281281

282282
def__init__(self,base,nonpos='clip'):
283283
Transform.__init__(self)
284+
ifbase<=0orbase==1:
285+
raiseValueError('The log base cannot be <= 0 or == 1')
284286
self.base=base
285-
self._clip= {"clip":True,"mask":False}[nonpos]
287+
self._clip=cbook._check_getitem(
288+
{"clip":True,"mask":False},nonpos=nonpos)
286289

287290
def__str__(self):
288291
return"{}(base={}, nonpos={!r})".format(
@@ -362,41 +365,32 @@ def __init__(self, axis, **kwargs):
362365
----------
363366
axis : `~matplotlib.axis.Axis`
364367
The axis for the scale.
365-
basex, basey : float, default: 10
368+
base : float, default: 10
366369
The base of the logarithm.
367-
nonposx, nonposy : {'clip', 'mask'}, default: 'clip'
370+
nonpos : {'clip', 'mask'}, default: 'clip'
368371
Determines the behavior for non-positive values. They can either
369372
be masked as invalid, or clipped to a very small positive number.
370-
subsx, subsy : sequence of int, default: None
371-
Where to place the subticks between each major tick.
372-
For example, in a log10 scale: ``[2, 3, 4, 5, 6, 7, 8, 9]``
373-
will place 8 logarithmically spaced minor ticks between
374-
each major tick.
373+
subs : sequence of int, default: None
374+
Where to place the subticks between each major tick. For example,
375+
in a log10 scale, ``[2, 3, 4, 5, 6, 7, 8, 9]`` will place 8
376+
logarithmically spaced minor ticks between each major tick.
375377
"""
376-
ifaxis.axis_name=='x':
377-
base=kwargs.pop('basex',10.0)
378-
subs=kwargs.pop('subsx',None)
379-
nonpos=kwargs.pop('nonposx','clip')
380-
cbook._check_in_list(['mask','clip'],nonposx=nonpos)
381-
else:
382-
base=kwargs.pop('basey',10.0)
383-
subs=kwargs.pop('subsy',None)
384-
nonpos=kwargs.pop('nonposy','clip')
385-
cbook._check_in_list(['mask','clip'],nonposy=nonpos)
386-
387-
ifkwargs:
388-
raiseTypeError(f"LogScale got an unexpected keyword "
389-
f"argument{next(iter(kwargs))!r}")
390-
391-
ifbase<=0orbase==1:
392-
raiseValueError('The log base cannot be <= 0 or == 1')
393-
378+
# After the deprecation, the whole (outer) __init__ can be replaced by
379+
# def __init__(self, axis, *, base=10, subs=None, nonpos="clip"): ...
380+
# The following is to emit the right warnings depending on the axis
381+
# used, as the *old* kwarg names depended on the axis.
382+
axis_name=getattr(axis,"axis_name","x")
383+
@cbook._rename_parameter("3.3",f"base{axis_name}","base")
384+
@cbook._rename_parameter("3.3",f"subs{axis_name}","subs")
385+
@cbook._rename_parameter("3.3",f"nonpos{axis_name}","nonpos")
386+
def__init__(*,base=10,subs=None,nonpos="clip"):
387+
returnbase,subs,nonpos
388+
389+
base,subs,nonpos=__init__(**kwargs)
394390
self._transform=LogTransform(base,nonpos)
395391
self.subs=subs
396392

397-
@property
398-
defbase(self):
399-
returnself._transform.base
393+
base=property(lambdaself:self._transform.base)
400394

401395
defset_default_locators_and_formatters(self,axis):
402396
# docstring inherited
@@ -463,6 +457,12 @@ class SymmetricalLogTransform(Transform):
463457

464458
def__init__(self,base,linthresh,linscale):
465459
Transform.__init__(self)
460+
ifbase<=1.0:
461+
raiseValueError("'base' must be larger than 1")
462+
iflinthresh<=0.0:
463+
raiseValueError("'linthresh' must be positive")
464+
iflinscale<=0.0:
465+
raiseValueError("'linscale' must be positive")
466466
self.base=base
467467
self.linthresh=linthresh
468468
self.linscale=linscale
@@ -523,19 +523,19 @@ class SymmetricalLogScale(ScaleBase):
523523
524524
Parameters
525525
----------
526-
basex, basey : float, default: 10
526+
base : float, default: 10
527527
The base of the logarithm.
528528
529-
linthreshx, linthreshy : float, default: 2
529+
linthresh : float, default: 2
530530
Defines the range ``(-x, x)``, within which the plot is linear.
531531
This avoids having the plot go to infinity around zero.
532532
533-
subsx, subsy : sequence of int
533+
subs : sequence of int
534534
Where to place the subticks between each major tick.
535535
For example, in a log10 scale: ``[2, 3, 4, 5, 6, 7, 8, 9]`` will place
536536
8 logarithmically spaced minor ticks between each major tick.
537537
538-
linscalex, linscaley : float, optional
538+
linscale : float, optional
539539
This allows the linear range ``(-linthresh, linthresh)`` to be
540540
stretched relative to the logarithmic range. Its value is the number of
541541
decades to use for each half of the linear range. For example, when
@@ -557,40 +557,31 @@ def InvertedSymmetricalLogTransform(self):
557557
returnInvertedSymmetricalLogTransform
558558

559559
def__init__(self,axis,**kwargs):
560-
ifaxis.axis_name=='x':
561-
base=kwargs.pop('basex',10.0)
562-
linthresh=kwargs.pop('linthreshx',2.0)
563-
subs=kwargs.pop('subsx',None)
564-
linscale=kwargs.pop('linscalex',1.0)
565-
else:
566-
base=kwargs.pop('basey',10.0)
567-
linthresh=kwargs.pop('linthreshy',2.0)
568-
subs=kwargs.pop('subsy',None)
569-
linscale=kwargs.pop('linscaley',1.0)
570-
ifkwargs:
571-
warn_deprecated(
572-
'3.2.0',
573-
message=(
574-
f"SymmetricalLogScale got an unexpected keyword "
575-
f"argument{next(iter(kwargs))!r}. "
576-
'In the future this will raise TypeError')
577-
)
578-
# raise TypeError(f"SymmetricalLogScale got an unexpected keyword "
579-
# f"argument {next(iter(kwargs))!r}")
580-
581-
ifbase<=1.0:
582-
raiseValueError("'basex/basey' must be larger than 1")
583-
iflinthresh<=0.0:
584-
raiseValueError("'linthreshx/linthreshy' must be positive")
585-
iflinscale<=0.0:
586-
raiseValueError("'linscalex/linthreshy' must be positive")
587-
560+
axis_name=getattr(axis,"axis_name","x")
561+
# See explanation in LogScale.__init__.
562+
@cbook._rename_parameter("3.3",f"base{axis_name}","base")
563+
@cbook._rename_parameter("3.3",f"linthresh{axis_name}","linthresh")
564+
@cbook._rename_parameter("3.3",f"subs{axis_name}","subs")
565+
@cbook._rename_parameter("3.3",f"linscale{axis_name}","linscale")
566+
def__init__(*,base=10,linthresh=2,subs=None,linscale=1,**kwargs):
567+
ifkwargs:
568+
warn_deprecated(
569+
"3.2.0",
570+
message=(
571+
f"SymmetricalLogScale got an unexpected keyword "
572+
f"argument{next(iter(kwargs))!r}; in the future, "
573+
f"this will raise a TypeError")
574+
)
575+
returnbase,linthresh,subs,linscale
576+
577+
base,linthresh,subs,linscale=__init__(**kwargs)
588578
self._transform=SymmetricalLogTransform(base,linthresh,linscale)
589-
self.base=base
590-
self.linthresh=linthresh
591-
self.linscale=linscale
592579
self.subs=subs
593580

581+
base=property(lambdaself:self._transform.base)
582+
linthresh=property(lambdaself:self._transform.linthresh)
583+
linscale=property(lambdaself:self._transform.linscale)
584+
594585
defset_default_locators_and_formatters(self,axis):
595586
# docstring inherited
596587
axis.set_major_locator(SymmetricalLogLocator(self.get_transform()))

‎lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,9 +1150,9 @@ def test_symlog2():
11501150
x=np.arange(-50,50,0.001)
11511151

11521152
fig,axs=plt.subplots(5,1)
1153-
forax,linthreshxinzip(axs, [20.,2.,1.,0.1,0.01]):
1153+
forax,linthreshinzip(axs, [20.,2.,1.,0.1,0.01]):
11541154
ax.plot(x,x)
1155-
ax.set_xscale('symlog',linthreshx=linthreshx)
1155+
ax.set_xscale('symlog',linthresh=linthresh)
11561156
ax.grid(True)
11571157
axs[-1].set_ylim(-0.1,0.1)
11581158

@@ -2241,9 +2241,9 @@ def test_log_scales():
22412241
fig=plt.figure()
22422242
ax=fig.add_subplot(1,1,1)
22432243
ax.plot(np.log(np.linspace(0.1,100)))
2244-
ax.set_yscale('log',basey=5.5)
2244+
ax.set_yscale('log',base=5.5)
22452245
ax.invert_yaxis()
2246-
ax.set_xscale('log',basex=9.0)
2246+
ax.set_xscale('log',base=9.0)
22472247

22482248

22492249
deftest_log_scales_no_data():

‎lib/matplotlib/tests/test_scale.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_log_scatter():
8888

8989
deftest_logscale_subs():
9090
fig,ax=plt.subplots()
91-
ax.set_yscale('log',subsy=np.array([2,3,4]))
91+
ax.set_yscale('log',subs=np.array([2,3,4]))
9292
# force draw
9393
fig.canvas.draw()
9494

@@ -108,16 +108,14 @@ def test_logscale_mask():
108108
deftest_extra_kwargs_raise_or_warn():
109109
fig,ax=plt.subplots()
110110

111-
# with pytest.raises(TypeError):
112111
withpytest.warns(MatplotlibDeprecationWarning):
113-
ax.set_yscale('linear',nonpos='mask')
112+
ax.set_yscale('linear',foo='mask')
114113

115114
withpytest.raises(TypeError):
116-
ax.set_yscale('log',nonpos='mask')
115+
ax.set_yscale('log',foo='mask')
117116

118-
# with pytest.raises(TypeError):
119117
withpytest.warns(MatplotlibDeprecationWarning):
120-
ax.set_yscale('symlog',nonpos='mask')
118+
ax.set_yscale('symlog',foo='mask')
121119

122120

123121
deftest_logscale_invert_transform():
@@ -150,7 +148,7 @@ def test_logscale_nonpos_values():
150148
ax1.hist(xs,range=(-5,5),bins=10)
151149
ax1.set_yscale('log')
152150
ax2.hist(xs,range=(-5,5),bins=10)
153-
ax2.set_yscale('log',nonposy='mask')
151+
ax2.set_yscale('log',nonpos='mask')
154152

155153
xdata=np.arange(0,10,0.01)
156154
ydata=np.exp(-xdata)

‎tutorials/introductory/pyplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def f(t):
443443
# symmetric log
444444
plt.subplot(223)
445445
plt.plot(x,y-y.mean())
446-
plt.yscale('symlog',linthreshy=0.01)
446+
plt.yscale('symlog',linthresh=0.01)
447447
plt.title('symlog')
448448
plt.grid(True)
449449

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp