Skip to content

Commit f0c41a0

Browse files
author
Hancar, Pavel
committed
11042 refactor(plot sub-methods): signatures
1 parent 2d65e38 commit f0c41a0

File tree

1 file changed

+112
-47
lines changed

1 file changed

+112
-47
lines changed

pandas/plotting/_core.py

+112-47
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@
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+
"""
38+
2139

2240
def hist_series(
2341
self,
@@ -588,7 +606,7 @@ class PlotAccessor(PandasObject):
588606
labels with "(right)" in the legend
589607
include_bool : bool, default is False
590608
If True, boolean values can be plotted.
591-
`**kwds` : keywords
609+
`**kwargs` : keywords
592610
Options to pass to matplotlib plotting method.
593611
594612
Returns
@@ -795,8 +813,7 @@ def __call__(self, *args, **kwargs):
795813

796814
return plot_backend.plot(data, kind=kind, **kwargs)
797815

798-
def line(self, x=None, y=None, **kwargs):
799-
"""
816+
_line_docs = """
800817
Plot Series or DataFrame as lines.
801818
802819
This function is useful to plot lines using DataFrame's values
@@ -812,7 +829,8 @@ def line(self, x=None, y=None, **kwargs):
812829
The values to be plotted.
813830
Either the location or the label of the columns to be used.
814831
By default, it will use the remaining DataFrame numeric columns.
815-
**kwds
832+
%s
833+
**kwargs
816834
Keyword arguments to pass on to :meth:`DataFrame.plot`.
817835
818836
Returns
@@ -862,9 +880,15 @@ def line(self, x=None, y=None, **kwargs):
862880
863881
>>> lines = df.plot.line(x='pig', y='horse')
864882
"""
865-
return self(kind="line", x=x, y=y, **kwargs)
866883

867-
def bar(self, x=None, y=None, **kwargs):
884+
@Appender(_line_docs % _shared_docs)
885+
def line(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
886+
sharey=False, layout=None, title=None, **kwargs):
887+
return self(kind="line", x=x, y=y, figsize=figsize, subplots=subplots,
888+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
889+
890+
def bar(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
891+
sharey=False, layout=None, title=None, **kwargs):
868892
"""
869893
Vertical bar plot.
870894
@@ -882,7 +906,8 @@ def bar(self, x=None, y=None, **kwargs):
882906
y : label or position, optional
883907
Allows plotting of one column versus another. If not specified,
884908
all numerical columns are used.
885-
**kwds
909+
%s
910+
**kwargs
886911
Additional keyword arguments are documented in
887912
:meth:`DataFrame.plot`.
888913
@@ -947,10 +972,10 @@ def bar(self, x=None, y=None, **kwargs):
947972
948973
>>> ax = df.plot.bar(x='lifespan', rot=0)
949974
"""
950-
return self(kind="bar", x=x, y=y, **kwargs)
975+
return self(kind="bar", x=x, y=y, figsize=figsize, subplots=subplots,
976+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
951977

952-
def barh(self, x=None, y=None, **kwargs):
953-
"""
978+
_barh_docs = """
954979
Make a horizontal bar plot.
955980
956981
A horizontal bar plot is a plot that presents quantitative data with
@@ -965,7 +990,8 @@ def barh(self, x=None, y=None, **kwargs):
965990
Column to be used for categories.
966991
y : label or position, default All numeric columns in dataframe
967992
Columns to be plotted from the DataFrame.
968-
**kwds
993+
%s
994+
**kwargs
969995
Keyword arguments to pass on to :meth:`DataFrame.plot`.
970996
971997
Returns
@@ -1026,11 +1052,15 @@ def barh(self, x=None, y=None, **kwargs):
10261052
>>> df = pd.DataFrame({'speed': speed,
10271053
... 'lifespan': lifespan}, index=index)
10281054
>>> ax = df.plot.barh(x='lifespan')
1029-
"""
1030-
return self(kind="barh", x=x, y=y, **kwargs)
1055+
"""
10311056

1032-
def box(self, by=None, **kwargs):
1033-
r"""
1057+
@Appender(_barh_docs % _shared_docs)
1058+
def barh(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1059+
sharey=False, layout=None, title=None, **kwargs):
1060+
return self(kind="barh", x=x, y=y, figsize=figsize, subplots=subplots,
1061+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1062+
1063+
_box_docs = r"""
10341064
Make a box plot of the DataFrame columns.
10351065
10361066
A box plot is a method for graphically depicting groups of numerical
@@ -1051,7 +1081,8 @@ def box(self, by=None, **kwargs):
10511081
----------
10521082
by : str or sequence
10531083
Column in the DataFrame to group by.
1054-
**kwds : optional
1084+
%s
1085+
**kwargs : optional
10551086
Additional keywords are documented in
10561087
:meth:`DataFrame.plot`.
10571088
@@ -1077,10 +1108,14 @@ def box(self, by=None, **kwargs):
10771108
>>> df = pd.DataFrame(data, columns=list('ABCD'))
10781109
>>> ax = df.plot.box()
10791110
"""
1080-
return self(kind="box", by=by, **kwargs)
10811111

1082-
def hist(self, by=None, bins=10, **kwargs):
1083-
"""
1112+
@Appender(_box_docs % _shared_docs)
1113+
def box(self, by=None, figsize=None, subplots=False, sharex=None, sharey=False,
1114+
layout=None, title=None, **kwargs):
1115+
return self(kind="box", by=by, figsize=figsize, subplots=subplots,
1116+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1117+
1118+
_hist_docs = """
10841119
Draw one histogram of the DataFrame's columns.
10851120
10861121
A histogram is a representation of the distribution of data.
@@ -1094,7 +1129,8 @@ def hist(self, by=None, bins=10, **kwargs):
10941129
Column in the DataFrame to group by.
10951130
bins : int, default 10
10961131
Number of histogram bins to be used.
1097-
**kwds
1132+
%s
1133+
**kwargs
10981134
Additional keyword arguments are documented in
10991135
:meth:`DataFrame.plot`.
11001136
@@ -1124,10 +1160,13 @@ def hist(self, by=None, bins=10, **kwargs):
11241160
>>> df['two'] = df['one'] + np.random.randint(1, 7, 6000)
11251161
>>> ax = df.plot.hist(bins=12, alpha=0.5)
11261162
"""
1127-
return self(kind="hist", by=by, bins=bins, **kwargs)
11281163

1129-
def kde(self, bw_method=None, ind=None, **kwargs):
1130-
"""
1164+
@Appender(_hist_docs % _shared_docs)
1165+
def hist(self, by=None, bins=10, figsize=None, subplots=False, sharex=None,
1166+
sharey=False, layout=None, title=None, **kwargs):
1167+
return self(kind="hist", by=by, bins=bins, figsize=figsize, subplots=subplots,
1168+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1169+
_kde_docs = """
11311170
Generate Kernel Density Estimate plot using Gaussian kernels.
11321171
11331172
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
@@ -1150,9 +1189,10 @@ def kde(self, bw_method=None, ind=None, **kwargs):
11501189
1000 equally spaced points are used. If `ind` is a NumPy array, the
11511190
KDE is evaluated at the points passed. If `ind` is an integer,
11521191
`ind` number of equally spaced points are used.
1153-
**kwds : optional
1192+
%s
1193+
**kwargs : optional
11541194
Additional keyword arguments are documented in
1155-
:meth:`pandas.%(this-datatype)s.plot`.
1195+
:meth:`pandas.%%(this-datatype)s.plot`.
11561196
11571197
Returns
11581198
-------
@@ -1232,12 +1272,17 @@ def kde(self, bw_method=None, ind=None, **kwargs):
12321272
12331273
>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
12341274
"""
1235-
return self(kind="kde", bw_method=bw_method, ind=ind, **kwargs)
1275+
1276+
@Appender(_kde_docs % _shared_docs)
1277+
def kde(self, bw_method=None, ind=None, figsize=None, subplots=False, sharex=None,
1278+
sharey=False, layout=None, title=None, **kwargs):
1279+
return self(kind="kde", bw_method=bw_method, ind=ind, figsize=figsize,
1280+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1281+
**kwargs)
12361282

12371283
density = kde
12381284

1239-
def area(self, x=None, y=None, **kwargs):
1240-
"""
1285+
_area_docs = """
12411286
Draw a stacked area plot.
12421287
12431288
An area plot displays quantitative data visually.
@@ -1252,7 +1297,8 @@ def area(self, x=None, y=None, **kwargs):
12521297
stacked : bool, default True
12531298
Area plots are stacked by default. Set to False to create a
12541299
unstacked plot.
1255-
**kwds : optional
1300+
%s
1301+
**kwargs : optional
12561302
Additional keyword arguments are documented in
12571303
:meth:`DataFrame.plot`.
12581304
@@ -1307,10 +1353,14 @@ def area(self, x=None, y=None, **kwargs):
13071353
... })
13081354
>>> ax = df.plot.area(x='day')
13091355
"""
1310-
return self(kind="area", x=x, y=y, **kwargs)
13111356

1312-
def pie(self, **kwargs):
1313-
"""
1357+
@Appender(_area_docs % _shared_docs)
1358+
def area(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1359+
sharey=False, layout=None, title=None, **kwargs):
1360+
return self(kind="area", x=x, y=y, figsize=figsize, subplots=subplots,
1361+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1362+
1363+
_pie_docs = """
13141364
Generate a pie plot.
13151365
13161366
A pie plot is a proportional representation of the numerical data in a
@@ -1324,7 +1374,8 @@ def pie(self, **kwargs):
13241374
y : int or label, optional
13251375
Label or position of the column to plot.
13261376
If not provided, ``subplots=True`` argument must be passed.
1327-
**kwds
1377+
%s
1378+
**kwargs
13281379
Keyword arguments to pass on to :meth:`DataFrame.plot`.
13291380
13301381
Returns
@@ -1356,16 +1407,16 @@ def pie(self, **kwargs):
13561407
13571408
>>> plot = df.plot.pie(subplots=True, figsize=(6, 3))
13581409
"""
1359-
if (
1360-
isinstance(self._parent, ABCDataFrame)
1361-
and kwargs.get("y", None) is None
1362-
and not kwargs.get("subplots", False)
1363-
):
1410+
1411+
@Appender(_pie_docs % _shared_docs)
1412+
def pie(self, y=None, figsize=None, subplots=False, sharex=None,
1413+
sharey=False, layout=None, title=None, **kwargs):
1414+
if isinstance(self._parent, ABCDataFrame) and y is None and not subplots:
13641415
raise ValueError("pie requires either y column or 'subplots=True'")
1365-
return self(kind="pie", **kwargs)
1416+
return self(kind="pie", y=y, figsize=figsize, subplots=subplots, sharex=sharex,
1417+
sharey=sharey, layout=layout, title=title, **kwargs)
13661418

1367-
def scatter(self, x, y, s=None, c=None, **kwargs):
1368-
"""
1419+
_scatter_docs = """
13691420
Create a scatter plot with varying marker point size and color.
13701421
13711422
The coordinates of each point are defined by two dataframe columns and
@@ -1405,8 +1456,9 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
14051456
14061457
- A column name or position whose values will be used to color the
14071458
marker points according to a colormap.
1459+
%s
14081460
1409-
**kwds
1461+
**kwargs
14101462
Keyword arguments to pass on to :meth:`DataFrame.plot`.
14111463
14121464
Returns
@@ -1443,10 +1495,15 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
14431495
... c='species',
14441496
... colormap='viridis')
14451497
"""
1446-
return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs)
14471498

1448-
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
1449-
"""
1499+
@Appender(_scatter_docs % _shared_docs)
1500+
def scatter(self, x, y, s=None, c=None, figsize=None, subplots=False, sharex=None,
1501+
sharey=False, layout=None, title=None, **kwargs):
1502+
return self(kind="scatter", x=x, y=y, s=s, c=c, figsize=figsize,
1503+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1504+
title=title, **kwargs)
1505+
1506+
_hexbin_docs = """
14501507
Generate a hexagonal binning plot.
14511508
14521509
Generate a hexagonal binning plot of `x` versus `y`. If `C` is `None`
@@ -1478,7 +1535,8 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
14781535
Alternatively, gridsize can be a tuple with two elements
14791536
specifying the number of hexagons in the x-direction and the
14801537
y-direction.
1481-
**kwds
1538+
%s
1539+
**kwargs
14821540
Additional keyword arguments are documented in
14831541
:meth:`DataFrame.plot`.
14841542
@@ -1527,12 +1585,19 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
15271585
... gridsize=10,
15281586
... cmap="viridis")
15291587
"""
1588+
1589+
@Appender(_hexbin_docs % _shared_docs)
1590+
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, figsize=None,
1591+
subplots=False, sharex=None, sharey=False, layout=None, title=None,
1592+
**kwargs):
15301593
if reduce_C_function is not None:
15311594
kwargs["reduce_C_function"] = reduce_C_function
15321595
if gridsize is not None:
15331596
kwargs["gridsize"] = gridsize
15341597

1535-
return self(kind="hexbin", x=x, y=y, C=C, **kwargs)
1598+
return self(kind="hexbin", x=x, y=y, C=C, reduce_C_function=reduce_C_function,
1599+
gridsize=gridsize, figsize=figsize, subplots=subplots,
1600+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
15361601

15371602

15381603
_backends = {}

0 commit comments

Comments
 (0)