42
42
rcParams = matplotlib .rcParams
43
43
44
44
45
- def _has_item (data , name ):
46
- """Return whether *data* can be item-accessed with *name*.
47
-
48
- This supports data with a dict-like interface (`in` checks item
49
- availability) and with numpy.arrays.
50
- """
51
- try :
52
- return data .dtype .names is not None and name in data .dtype .names
53
- except AttributeError : # not a numpy array
54
- return name in data
55
-
56
-
57
- def _plot_args_replacer (args , data ):
58
- if len (args ) == 1 :
59
- return ["y" ]
60
- elif len (args ) == 2 :
61
- # this can be two cases: x,y or y,c
62
- if not _has_item (data , args [1 ]):
63
- return ["y" , "c" ]
64
- # it's data, but could be a color code like 'ro' or 'b--'
65
- # -> warn the user in that case...
66
- try :
67
- _process_plot_format (args [1 ])
68
- except ValueError :
69
- pass
70
- else :
71
- cbook ._warn_external (
72
- "Second argument {!r} is ambiguous: could be a color spec but "
73
- "is in data; using as data. Either rename the entry in data "
74
- "or use three arguments to plot." .format (args [1 ]),
75
- RuntimeWarning )
76
- return ["x" , "y" ]
77
- elif len (args ) == 3 :
78
- return ["x" , "y" , "c" ]
79
- else :
80
- raise ValueError ("Using arbitrary long args with data is not "
81
- "supported due to ambiguity of arguments.\n Use "
82
- "multiple plotting calls instead." )
83
-
84
-
85
45
def _make_inset_locator (bounds , trans , parent ):
86
46
"""
87
47
Helper function to locate inset axes, used in
@@ -1154,8 +1114,7 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
1154
1114
1155
1115
@_preprocess_data (replace_names = ["positions" , "lineoffsets" ,
1156
1116
"linelengths" , "linewidths" ,
1157
- "colors" , "linestyles" ],
1158
- label_namer = None )
1117
+ "colors" , "linestyles" ])
1159
1118
@docstring .dedent_interpd
1160
1119
def eventplot (self , positions , orientation = 'horizontal' , lineoffsets = 1 ,
1161
1120
linelengths = 1 , linewidths = None , colors = None ,
@@ -1369,13 +1328,12 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
1369
1328
1370
1329
return colls
1371
1330
1372
- # ### Basic plotting
1373
- # The label_naming happens in `matplotlib.axes._base._plot_args`
1374
- @_preprocess_data (replace_names = ["x" , "y" ],
1375
- positional_parameter_names = _plot_args_replacer ,
1376
- label_namer = None )
1331
+ #### Basic plotting
1332
+
1333
+ # Uses a custom implementation of data-kwarg handling in
1334
+ # _process_plot_var_args.
1377
1335
@docstring .dedent_interpd
1378
- def plot (self , * args , scalex = True , scaley = True , ** kwargs ):
1336
+ def plot (self , * args , scalex = True , scaley = True , data = None , ** kwargs ):
1379
1337
"""
1380
1338
Plot y versus x as lines and/or markers.
1381
1339
@@ -1486,7 +1444,6 @@ def plot(self, *args, scalex=True, scaley=True, **kwargs):
1486
1444
You may suppress the warning by adding an empty format string
1487
1445
`plot('n', 'o', '', data=obj)`.
1488
1446
1489
-
1490
1447
Other Parameters
1491
1448
----------------
1492
1449
scalex, scaley : bool, optional, default: True
@@ -1513,13 +1470,11 @@ def plot(self, *args, scalex=True, scaley=True, **kwargs):
1513
1470
lines
1514
1471
A list of `.Line2D` objects representing the plotted data.
1515
1472
1516
-
1517
1473
See Also
1518
1474
--------
1519
1475
scatter : XY scatter plot with markers of varying size and/or color (
1520
1476
sometimes also called bubble chart).
1521
1477
1522
-
1523
1478
Notes
1524
1479
-----
1525
1480
**Format Strings**
@@ -1606,14 +1561,10 @@ def plot(self, *args, scalex=True, scaley=True, **kwargs):
1606
1561
additionally use any `matplotlib.colors` spec, e.g. full names
1607
1562
(``'green'``) or hex strings (``'#008000'``).
1608
1563
"""
1609
- lines = []
1610
-
1611
1564
kwargs = cbook .normalize_kwargs (kwargs , mlines .Line2D ._alias_map )
1612
-
1613
- for line in self . _get_lines ( * args , ** kwargs ) :
1565
+ lines = [ * self . _get_lines ( * args , data = data , ** kwargs )]
1566
+ for line in lines :
1614
1567
self .add_line (line )
1615
- lines .append (line )
1616
-
1617
1568
self .autoscale_view (scalex = scalex , scaley = scaley )
1618
1569
return lines
1619
1570
@@ -1996,8 +1947,8 @@ def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none,
1996
1947
1997
1948
#### Specialized plotting
1998
1949
1999
- @_preprocess_data (replace_names = [ "x" , "y" ], label_namer = "y" )
2000
- def step (self , x , y , * args , where = 'pre' , ** kwargs ):
1950
+ # @_preprocess_data() # let 'plot' do the unpacking..
1951
+ def step (self , x , y , * args , where = 'pre' , data = None , ** kwargs ):
2001
1952
"""
2002
1953
Make a step plot.
2003
1954
@@ -2062,17 +2013,9 @@ def step(self, x, y, *args, where='pre', **kwargs):
2062
2013
raise ValueError ("'where' argument to step must be "
2063
2014
"'pre', 'post' or 'mid'" )
2064
2015
kwargs ['drawstyle' ] = 'steps-' + where
2065
- return self .plot (x , y , * args , ** kwargs )
2066
-
2067
- @_preprocess_data (replace_names = ["x" , "left" ,
2068
- "height" , "width" ,
2069
- "y" , "bottom" ,
2070
- "color" , "edgecolor" , "linewidth" ,
2071
- "tick_label" , "xerr" , "yerr" ,
2072
- "ecolor" ],
2073
- label_namer = None ,
2074
- replace_all_args = True
2075
- )
2016
+ return self .plot (x , y , * args , data = data , ** kwargs )
2017
+
2018
+ @_preprocess_data ()
2076
2019
@docstring .dedent_interpd
2077
2020
def bar (self , x , height , width = 0.8 , bottom = None , * , align = "center" ,
2078
2021
** kwargs ):
@@ -2464,7 +2407,7 @@ def barh(self, y, width, height=0.8, left=None, *, align="center",
2464
2407
align = align , ** kwargs )
2465
2408
return patches
2466
2409
2467
- @_preprocess_data (label_namer = None )
2410
+ @_preprocess_data ()
2468
2411
@docstring .dedent_interpd
2469
2412
def broken_barh (self , xranges , yrange , ** kwargs ):
2470
2413
"""
@@ -2535,9 +2478,9 @@ def broken_barh(self, xranges, yrange, **kwargs):
2535
2478
2536
2479
return col
2537
2480
2538
- @_preprocess_data (replace_all_args = True , label_namer = None )
2539
- def stem (self , * args , linefmt = None , markerfmt = None , basefmt = None ,
2540
- bottom = 0 , label = None ):
2481
+ @_preprocess_data ()
2482
+ def stem (self , * args , linefmt = None , markerfmt = None , basefmt = None , bottom = 0 ,
2483
+ label = None ):
2541
2484
"""
2542
2485
Create a stem plot.
2543
2486
@@ -2695,8 +2638,7 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None,
2695
2638
2696
2639
return stem_container
2697
2640
2698
- @_preprocess_data (replace_names = ["x" , "explode" , "labels" , "colors" ],
2699
- label_namer = None )
2641
+ @_preprocess_data (replace_names = ["x" , "explode" , "labels" , "colors" ])
2700
2642
def pie (self , x , explode = None , labels = None , colors = None ,
2701
2643
autopct = None , pctdistance = 0.6 , shadow = False , labeldistance = 1.1 ,
2702
2644
startangle = None , radius = None , counterclock = True ,
@@ -3313,7 +3255,7 @@ def extract_err(err, data):
3313
3255
3314
3256
return errorbar_container # (l0, caplines, barcols)
3315
3257
3316
- @_preprocess_data (label_namer = None )
3258
+ @_preprocess_data ()
3317
3259
def boxplot (self , x , notch = None , sym = None , vert = None , whis = None ,
3318
3260
positions = None , widths = None , patch_artist = None ,
3319
3261
bootstrap = None , usermedians = None , conf_intervals = None ,
@@ -4908,7 +4850,7 @@ def _quiver_units(self, args, kw):
4908
4850
return args
4909
4851
4910
4852
# args can by a combination if X, Y, U, V, C and all should be replaced
4911
- @_preprocess_data (replace_all_args = True , label_namer = None )
4853
+ @_preprocess_data ()
4912
4854
def
10000
quiver (self , * args , ** kw ):
4913
4855
# Make sure units are handled for x and y values
4914
4856
args = self ._quiver_units (args , kw )
@@ -4921,13 +4863,12 @@ def quiver(self, *args, **kw):
4921
4863
quiver .__doc__ = mquiver .Quiver .quiver_doc
4922
4864
4923
4865
# args can by either Y or y1,y2,... and all should be replaced
4924
- @_preprocess_data (replace_all_args = True , label_namer = None )
4866
+ @_preprocess_data ()
4925
4867
def stackplot (self , x , * args , ** kwargs ):
4926
4868
return mstack .stackplot (self , x , * args , ** kwargs )
4927
4869
stackplot .__doc__ = mstack .stackplot .__doc__
4928
4870
4929
- @_preprocess_data (replace_names = ["x" , "y" , "u" , "v" , "start_points" ],
4930
- label_namer = None )
4871
+ @_preprocess_data (replace_names = ["x" , "y" , "u" , "v" , "start_points" ])
4931
4872
def streamplot (self , x , y , u , v , density = 1 , linewidth = None , color = None ,
4932
4873
cmap = None , norm = None , arrowsize = 1 , arrowstyle = '-|>' ,
4933
4874
minlength = 0.1 , transform = None , zorder = None ,
@@ -4952,7 +4893,7 @@ def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
4952
4893
streamplot .__doc__ = mstream .streamplot .__doc__
4953
4894
4954
4895
# args can be some combination of X, Y, U, V, C and all should be replaced
4955
- @_preprocess_data (replace_all_args = True , label_namer = None )
4896
+ @_preprocess_data ()
4956
4897
@docstring .dedent_interpd
4957
4898
def barbs (self , * args , ** kw ):
4958
4899
"""
@@ -4966,9 +4907,9 @@ def barbs(self, *args, **kw):
4966
4907
self .autoscale_view ()
4967
4908
return b
4968
4909
4969
- @ _preprocess_data ( replace_names = [ "x" , "y" ], label_namer = None ,
4970
- positional_parameter_names = [ "x" , "y" , "c" ])
4971
- def fill (self , * args , ** kwargs ):
4910
+ # Uses a custom implementation of data-kwarg handling in
4911
+ # _process_plot_var_args.
4912
+ def fill (self , * args , data = None , ** kwargs ):
4972
4913
"""
4973
4914
Plot filled polygons.
4974
4915
@@ -4991,6 +4932,13 @@ def fill(self, *args, **kwargs):
4991
4932
ax.fill(x, y, x2, y2) # two polygons
4992
4933
ax.fill(x, y, "b", x2, y2, "r") # a blue and a red polygon
4993
4934
4935
+ data : indexable object, optional
4936
+ An object with labelled data. If given, provide the label names to
4937
+ plot in *x* and *y*, e.g.::
4938
+
4939
+ ax.fill("time", "signal",
4940
+ data={"time": [0, 1, 2], "signal": [0, 1, 0]})
4941
+
4994
4942
Returns
4995
4943
-------
4996
4944
a list of :class:`~matplotlib.patches.Polygon`
@@ -5008,14 +4956,13 @@ def fill(self, *args, **kwargs):
5008
4956
kwargs = cbook .normalize_kwargs (kwargs , mlines .Line2D ._alias_map )
5009
4957
5010
4958
patches = []
5011
- for poly in self ._get_patches_for_fill (* args , ** kwargs ):
4959
+ for poly in self ._get_patches_for_fill (* args , data = data , ** kwargs ):
5012
4960
self .add_patch (poly )
5013
4961
patches .append (poly )
5014
4962
self .autoscale_view ()
5015
4963
return patches
5016
4964
5017
- @_preprocess_data (replace_names = ["x" , "y1" , "y2" , "where" ],
5018
- label_namer = None )
4965
+ @_preprocess_data (replace_names = ["x" , "y1" , "y2" , "where" ])
5019
4966
@docstring .dedent_interpd
5020
4967
def fill_between (self , x , y1 , y2 = 0 , where = None , interpolate = False ,
5021
4968
step = None , ** kwargs ):
@@ -5197,8 +5144,7 @@ def get_interp_point(ind):
5197
5144
self .autoscale_view ()
5198
5145
return collection
5199
5146
5200
- @_preprocess_data (replace_names = ["y" , "x1" , "x2" , "where" ],
5201
- label_namer = None )
5147
+ @_preprocess_data (replace_names = ["y" , "x1" , "x2" , "where" ])
5202
5148
@docstring .dedent_interpd
5203
5149
def fill_betweenx (self , y , x1 , x2 = 0 , where = None ,
5204
5150
step = None , interpolate = False , ** kwargs ):
@@ -5380,7 +5326,7 @@ def get_interp_point(ind):
5380
5326
return collection
5381
5327
5382
5328
#### plotting z(x,y): imshow, pcolor and relatives, contour
5383
- @_preprocess_data (label_namer = None )
5329
+ @_preprocess_data ()
5384
5330
def imshow (self , X , cmap = None , norm = None , aspect = None ,
5385
5331
interpolation = None , alpha = None , vmin = None , vmax = None ,
5386
5332
origin = None , extent = None , shape = None , filternorm = 1 ,
@@ -5647,7 +5593,7 @@ def _pcolorargs(funcname, *args, allmatch=False):
5647
5593
C = cbook .safe_masked_invalid (C )
5648
5594
return X , Y , C
5649
5595
5650
- @_preprocess_data (label_namer = None )
5596
+ @_preprocess_data ()
5651
5597
@docstring .dedent_interpd
5652
5598
def pcolor (self , * args , alpha = None , norm = None , cmap = None , vmin = None ,
5653
5599
vmax = None , ** kwargs ):
@@ -5884,7 +5830,7 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5884
5830
self .autoscale_view ()
5885
5831
return collection
5886
5832
5887
- @_preprocess_data (label_namer = None )
5833
+ @_preprocess_data ()
5888
5834
@docstring .dedent_interpd
5889
5835
def pcolormesh (self , * args , alpha = None , norm = None , cmap = None , vmin = None ,
5890
5836
vmax = None , shading = 'flat' , antialiased = False , ** kwargs ):
@@ -6097,7 +6043,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
6097
6043
self .autoscale_view ()
6098
6044
return collection
6099
6045
6100
- @_preprocess_data (label_namer = None )
6046
+ @_preprocess_data ()
6101
6047
@docstring .dedent_interpd
6102
6048
def pcolorfast (self , * args , alpha = None , norm = None , cmap = None , vmin = None ,
6103
6049
vmax = None , ** kwargs ):
@@ -6863,7 +6809,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
6863
6809
else :
6864
6810
return tops , bins , cbook .silent_list ('Lists of Patches' , patches )
6865
6811
6866
- @_preprocess_data (replace_names = ["x" , "y" , "weights" ], label_namer = None )
6812
+ @_preprocess_data (replace_names = ["x" , "y" , "weights" ])
6867
6813
def hist2d (self , x , y , bins = 10 , range = None , normed = False , weights = None ,
6868
6814
cmin = None , cmax = None , ** kwargs ):
6869
6815
"""
@@ -6971,7 +6917,7 @@ def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None,
6971
6917
6972
6918
return h , xedges , yedges , pc
6973
6919
6974
- @_preprocess_data (replace_names = ["x" ], label_namer = None )
6920
+ @_preprocess_data (replace_names = ["x" ])
6975
6921
@docstring .dedent_interpd
6976
6922
def psd (self , x , NFFT = None , Fs = None , Fc = None , detrend = None ,
6977
6923
window = None , noverlap = None , pad_to = None ,
@@ -7206,7 +7152,7 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None,
7206
7152
else :
7207
7153
return pxy , freqs , line
7208
7154
7209
- @_preprocess_data (replace_names = ["x" ], label_namer = None )
7155
+ @_preprocess_data (replace_names = ["x" ])
7210
7156
@docstring .dedent_interpd
7211
7157
def magnitude_spectrum (self , x , Fs = None , Fc = None , window = None ,
7212
7158
pad_to = None , sides = None , scale = None ,
@@ -7309,7 +7255,7 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
7309
7255
7310
7256
return spec , freqs , lines [0 ]
7311
7257
7312
- @_preprocess_data (replace_names = ["x" ], label_namer = None )
7258
+ @_preprocess_data (replace_names = ["x" ])
7313
7259
@docstring .dedent_interpd
7314
7260
def angle_spectrum (self , x , Fs = None , Fc = None , window = None ,
7315
7261
pad_to = None , sides = None , ** kwargs ):
@@ -7391,7 +7337,7 @@ def angle_spectrum(self, x, Fs=None, Fc=None, window=None,
7391
7337
7392
7338
return spec , freqs , lines [0 ]
7393
7339
7394
- @_preprocess_data (replace_names = ["x" ], label_namer = None )
7340
+ @_preprocess_data (replace_names = ["x" ])
7395
7341
@docstring .dedent_interpd
7396
7342
def phase_spectrum (self , x , Fs = None , Fc = None , window = None ,
7397
7343
pad_to = None , sides = None , ** kwargs ):
@@ -7472,7 +7418,7 @@ def phase_spectrum(self, x, Fs=None, Fc=None, window=None,
7472
7418
7473
7419
return spec , freqs , lines [0 ]
7474
7420
7475
- @_preprocess_data (replace_names = ["x" , "y" ], label_namer = None )
7421
+ @_preprocess_data (replace_names = ["x" , "y" ])
7476
7422
B706
@docstring .dedent_interpd
7477
7423
def cohere (self , x , y , NFFT = 256 , Fs = 2 , Fc = 0 , detrend = mlab .detrend_none ,
7478
7424
window = mlab .window_hanning , noverlap = 0 , pad_to = None ,
@@ -7537,7 +7483,7 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
7537
7483
7538
7484
return cxy , freqs
7539
7485
7540
- @_preprocess_data (replace_names = ["x" ], label_namer = None )
7486
+ @_preprocess_data (replace_names = ["x" ])
7541
7487
@docstring .dedent_interpd
7542
7488
def specgram (self , x , NFFT = None , Fs = None , Fc = None , detrend = None ,
7543
7489
window = None , noverlap = None ,
@@ -7889,7 +7835,7 @@ def matshow(self, Z, **kwargs):
7889
7835
integer = True ))
7890
7836
return im
7891
7837
7892
- @_preprocess_data (replace_names = ["dataset" ], label_namer = None )
7838
+ @_preprocess_data (replace_names = ["dataset" ])
7893
7839
def violinplot (self , dataset , positions = None , vert = True , widths = 0.5 ,
7894
7840
showmeans = False , showextrema = True , showmedians = False ,
7895
7841
points = 100 , bw_method = None ):
0 commit comments