Skip to content

Commit 78c9620

Browse files
author
Hancar, Pavel
committed
11042 refactor(plot sub-methods): signatures
1 parent f8a924b commit 78c9620

File tree

1 file changed

+110
-48
lines changed

1 file changed

+110
-48
lines changed

pandas/plotting/_core.py

+110-48
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@
1818
except ImportError:
1919
pass
2020

21+
_shared_docs = """figsize : a tuple (width, height) in inches
22+
subplots : boolean, default False
23+
Make separate subplots for each column
24+
sharex : boolean, default True if ax is None else False
25+
In case subplots=True, share x axis and set some x axis labels to invisible;
26+
defaults to True if ax is None otherwise False if an ax is passed in;
27+
Be aware, that passing in both an ax and sharex=True will alter all x axis
28+
labels for all axis in a figure!
29+
sharey : boolean, default False
30+
In case subplots=True, share y axis and set some y axis labels to subplots
31+
layout : tuple (optional)
32+
(rows, columns) for the layout of subplots
33+
title : string or list
34+
Title to use for the plot. If a string is passed, print the string at the
35+
top of the figure. If a list is passed and subplots is True, print each item
36+
in the list above the corresponding subplot.
37+
"""
2138

2239
def hist_series(
2340
self,
@@ -588,7 +605,7 @@ class PlotAccessor(PandasObject):
588605
labels with "(right)" in the legend
589606
include_bool : bool, default is False
590607
If True, boolean values can be plotted.
591-
`**kwds` : keywords
608+
`**kwargs` : keywords
592609
Options to pass to matplotlib plotting method.
593610
594611
Returns
@@ -795,8 +812,7 @@ def __call__(self, *args, **kwargs):
795812

796813
return plot_backend.plot(data, kind=kind, **kwargs)
797814

798-
def line(self, x=None, y=None, **kwargs):
799-
"""
815+
line_docs = """
800816
Plot Series or DataFrame as lines.
801817
802818
This function is useful to plot lines using DataFrame's values
@@ -812,7 +828,8 @@ def line(self, x=None, y=None, **kwargs):
812828
The values to be plotted.
813829
Either the location or the label of the columns to be used.
814830
By default, it will use the remaining DataFrame numeric columns.
815-
**kwds
831+
%s
832+
**kwargs
816833
Keyword arguments to pass on to :meth:`DataFrame.plot`.
817834
818835
Returns
@@ -862,9 +879,14 @@ def line(self, x=None, y=None, **kwargs):
862879
863880
>>> lines = df.plot.line(x='pig', y='horse')
864881
"""
865-
return self(kind="line", x=x, y=y, **kwargs)
866-
867-
def bar(self, x=None, y=None, **kwargs):
882+
@Appender(_line_docs % _shared_docs)
883+
def line(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
884+
sharey=False, layout=None, title=None, **kwargs):
885+
return self(kind="line", x=x, y=y, figsize=figsize, subplots=subplots,
886+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
887+
888+
def bar(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
889+
sharey=False, layout=None, title=None, **kwargs):
868890
"""
869891
Vertical bar plot.
870892
@@ -882,7 +904,8 @@ def bar(self, x=None, y=None, **kwargs):
882904
y : label or position, optional
883905
Allows plotting of one column versus another. If not specified,
884906
all numerical columns are used.
885-
**kwds
907+
%s
908+
**kwargs
886909
Additional keyword arguments are documented in
887910
:meth:`DataFrame.plot`.
888911
@@ -947,10 +970,10 @@ def bar(self, x=None, y=None, **kwargs):
947970
948971
>>> ax = df.plot.bar(x='lifespan', rot=0)
949972
"""
950-
return self(kind="bar", x=x, y=y, **kwargs)
973+
return self(kind="bar", x=x, y=y, figsize=figsize, subplots=subplots,
974+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
951975

952-
def barh(self, x=None, y=None, **kwargs):
953-
"""
976+
_barh_docs = """
954977
Make a horizontal bar plot.
955978
956979
A horizontal bar plot is a plot that presents quantitative data with
@@ -965,7 +988,8 @@ def barh(self, x=None, y=None, **kwargs):
965988
Column to be used for categories.
966989
y : label or position, default All numeric columns in dataframe
967990
Columns to be plotted from the DataFrame.
968-
**kwds
991+
%s
992+
**kwargs
969993
Keyword arguments to pass on to :meth:`DataFrame.plot`.
970994
971995
Returns
@@ -1026,11 +1050,14 @@ def barh(self, x=None, y=None, **kwargs):
10261050
>>> df = pd.DataFrame({'speed': speed,
10271051
... 'lifespan': lifespan}, index=index)
10281052
>>> ax = df.plot.barh(x='lifespan')
1029-
"""
1030-
return self(kind="barh", x=x, y=y, **kwargs)
1053+
"""
1054+
@Appender(_barh_docs % shared_docs)
1055+
def barh(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1056+
sharey=False, layout=None, title=None, **kwargs):
1057+
return self(kind="barh", x=x, y=y, figsize=figsize, subplots=subplots,
1058+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
10311059

1032-
def box(self, by=None, **kwargs):
1033-
r"""
1060+
_box_docs = r"""
10341061
Make a box plot of the DataFrame columns.
10351062
10361063
A box plot is a method for graphically depicting groups of numerical
@@ -1051,7 +1078,8 @@ def box(self, by=None, **kwargs):
10511078
----------
10521079
by : str or sequence
10531080
Column in the DataFrame to group by.
1054-
**kwds : optional
1081+
%s
1082+
**kwargs : optional
10551083
Additional keywords are documented in
10561084
:meth:`DataFrame.plot`.
10571085
@@ -1077,10 +1105,14 @@ def box(self, by=None, **kwargs):
10771105
>>> df = pd.DataFrame(data, columns=list('ABCD'))
10781106
>>> ax = df.plot.box()
10791107
"""
1080-
return self(kind="box", by=by, **kwargs)
10811108

1082-
def hist(self, by=None, bins=10, **kwargs):
1083-
"""
1109+
@Appender(_box_docs % _shared_docs)
1110+
def box(self, by=None, figsize=None, subplots=False, sharex=None, sharey=False,
1111+
layout=None, title=None, **kwargs):
1112+
return self(kind="box", by=by, figsize=figsize, subplots=subplots,
1113+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1114+
1115+
_hist_docs = """
10841116
Draw one histogram of the DataFrame's columns.
10851117
10861118
A histogram is a representation of the distribution of data.
@@ -1094,7 +1126,8 @@ def hist(self, by=None, bins=10, **kwargs):
10941126
Column in the DataFrame to group by.
10951127
bins : int, default 10
10961128
Number of histogram bins to be used.
1097-
**kwds
1129+
%s
1130+
**kwargs
10981131
Additional keyword arguments are documented in
10991132
:meth:`DataFrame.plot`.
11001133
@@ -1124,10 +1157,13 @@ def hist(self, by=None, bins=10, **kwargs):
11241157
>>> df['two'] = df['one'] + np.random.randint(1, 7, 6000)
11251158
>>> ax = df.plot.hist(bins=12, alpha=0.5)
11261159
"""
1127-
return self(kind="hist", by=by, bins=bins, **kwargs)
1128-
1129-
def kde(self, bw_method=None, ind=None, **kwargs):
1130-
"""
1160+
1161+
@Appender(_hist_docs % _shared_docs)
1162+
def hist(self, by=None, bins=10, figsize=None, subplots=False, sharex=None,
1163+
sharey=False, layout=None, title=None, **kwargs):
1164+
return self(kind="hist", by=by, bins=bins, figsize=figsize, subplots=subplots,
1165+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1166+
kde_docs = """
11311167
Generate Kernel Density Estimate plot using Gaussian kernels.
11321168
11331169
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
@@ -1150,7 +1186,8 @@ def kde(self, bw_method=None, ind=None, **kwargs):
11501186
1000 equally spaced points are used. If `ind` is a NumPy array, the
11511187
KDE is evaluated at the points passed. If `ind` is an integer,
11521188
`ind` number of equally spaced points are used.
1153-
**kwds : optional
1189+
%s
1190+
**kwargs : optional
11541191
Additional keyword arguments are documented in
11551192
:meth:`pandas.%(this-datatype)s.plot`.
11561193
@@ -1232,12 +1269,17 @@ def kde(self, bw_method=None, ind=None, **kwargs):
12321269
12331270
>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
12341271
"""
1235-
return self(kind="kde", bw_method=bw_method, ind=ind, **kwargs)
1272+
1273+
@Appender(_kde_docs % _shared_docs)
1274+
def kde(self, bw_method=None, ind=None, figsize=None, subplots=False, sharex=None,
1275+
sharey=False, layout=None, title=None, **kwargs):
1276+
return self(kind="kde", bw_method=bw_method, ind=ind, figsize=figsize,
1277+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1278+
**kwargs)
12361279

12371280
density = kde
12381281

1239-
def area(self, x=None, y=None, **kwargs):
1240-
"""
1282+
_area_docs = """
12411283
Draw a stacked area plot.
12421284
12431285
An area plot displays quantitative data visually.
@@ -1252,7 +1294,8 @@ def area(self, x=None, y=None, **kwargs):
12521294
stacked : bool, default True
12531295
Area plots are stacked by default. Set to False to create a
12541296
unstacked plot.
1255-
**kwds : optional
1297+
%s
1298+
**kwargs : optional
12561299
Additional keyword arguments are documented in
12571300
:meth:`DataFrame.plot`.
12581301
@@ -1307,10 +1350,14 @@ def area(self, x=None, y=None, **kwargs):
13071350
... })
13081351
>>> ax = df.plot.area(x='day')
13091352
"""
1310-
return self(kind="area", x=x, y=y, **kwargs)
13111353

1312-
def pie(self, **kwargs):
1313-
"""
1354+
@Appender(_area_docs % _shared_docs)
1355+
def area(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1356+
sharey=False, layout=None, title=None, **kwargs):
1357+
return self(kind="area", x=x, y=y, figsize=figsize, subplots=subplots,
1358+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1359+
1360+
_pie_docs = """
13141361
Generate a pie plot.
13151362
13161363
A pie plot is a proportional representation of the numerical data in a
@@ -1324,7 +1371,8 @@ def pie(self, **kwargs):
13241371
y : int or label, optional
13251372
Label or position of the column to plot.
13261373
If not provided, ``subplots=True`` argument must be passed.
1327-
**kwds
1374+
%s
1375+
**kwargs
13281376
Keyword arguments to pass on to :meth:`DataFrame.plot`.
13291377
13301378
Returns
@@ -1356,16 +1404,16 @@ def pie(self, **kwargs):
13561404
13571405
>>> plot = df.plot.pie(subplots=True, figsize=(6, 3))
13581406
"""
1359-
if (
1360-
isinstance(self._parent, ABCDataFrame)
1361-
and kwargs.get("y", None) is None
1362-
and not kwargs.get("subplots", False)
1363-
):
1407+
1408+
@Appender(_pie_docs % _shared_docs)
1409+
def pie(self, y=None, figsize=None, subplots=False, sharex=None,
1410+
sharey=False, layout=None, title=None, **kwargs):
1411+
if isinstance(self._parent, ABCDataFrame) and y is None and not subplots:
13641412
raise ValueError("pie requires either y column or 'subplots=True'")
1365-
return self(kind="pie", **kwargs)
1413+
return self(kind="pie", y=y, figsize=figsize, subplots=subplots, sharex=sharex,
1414+
sharey=sharey, layout=layout, title=title, **kwargs)
13661415

1367-
def scatter(self, x, y, s=None, c=None, **kwargs):
1368-
"""
1416+
_scatter_docs = """
13691417
Create a scatter plot with varying marker point size and color.
13701418
13711419
The coordinates of each point are defined by two dataframe columns and
@@ -1405,8 +1453,9 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
14051453
14061454
- A column name or position whose values will be used to color the
14071455
marker points according to a colormap.
1456+
%s
14081457
1409-
**kwds
1458+
**kwargs
14101459
Keyword arguments to pass on to :meth:`DataFrame.plot`.
14111460
14121461
Returns
@@ -1443,10 +1492,15 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
14431492
... c='species',
14441493
... colormap='viridis')
14451494
"""
1446-
return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs)
14471495

1448-
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
1449-
"""
1496+
@Appender(_scatter_docs % _shared_docs)
1497+
def scatter(self, x, y, s=None, c=None, figsize=None, subplots=False, sharex=None,
1498+
sharey=False, layout=None, title=None, **kwargs):
1499+
return self(kind="scatter", x=x, y=y, s=s, c=c, figsize=figsize,
1500+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1501+
title=title, **kwargs)
1502+
1503+
_hexbin_docs = """
14501504
Generate a hexagonal binning plot.
14511505
14521506
Generate a hexagonal binning plot of `x` versus `y`. If `C` is `None`
@@ -1478,7 +1532,8 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
14781532
Alternatively, gridsize can be a tuple with two elements
14791533
specifying the number of hexagons in the x-direction and the
14801534
y-direction.
1481-
**kwds
1535+
%s
1536+
**kwargs
14821537
Additional keyword arguments are documented in
14831538
:meth:`DataFrame.plot`.
14841539
@@ -1527,12 +1582,19 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
15271582
... gridsize=10,
15281583
... cmap="viridis")
15291584
"""
1585+
1586+
@Appender(_hexbin_docs % _shared_docs)
1587+
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, figsize=None,
1588+
subplots=False, sharex=None, sharey=False, layout=None, title=None,
1589+
**kwargs):
15301590
if reduce_C_function is not None:
15311591
kwargs["reduce_C_function"] = reduce_C_function
15321592
if gridsize is not None:
15331593
kwargs["gridsize"] = gridsize
15341594

1535-
return self(kind="hexbin", x=x, y=y, C=C, **kwargs)
1595+
return self(kind="hexbin", x=x, y=y, C=C, reduce_C_function=reduce_C_function,
1596+
gridsize=gridsize, figsize=figsize, subplots=subplots,
1597+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
15361598

15371599

15381600
_backends = {}

0 commit comments

Comments
 (0)