Skip to content

Commit 572751a

Browse files
author
Hancar, Pavel
committed
11042 refactor(plot sub-methods): signatures
1 parent d134b47 commit 572751a

File tree

1 file changed

+106
-45
lines changed

1 file changed

+106
-45
lines changed

pandas/plotting/_core.py

+106-45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import importlib
22
import warnings
33

4+
import numpy as np
5+
46
from pandas._config import get_option
57

68
from pandas.util._decorators import Appender, Substitution
@@ -10,6 +12,22 @@
1012

1113
from pandas.core.base import PandasObject
1214

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

1432
def hist_series(
1533
self,
@@ -852,8 +870,7 @@ def __call__(self, *args, **kwargs):
852870

853871
return plot_backend.plot(data, kind=kind, **kwargs)
854872

855-
def line(self, x=None, y=None, **kwargs):
856-
"""
873+
_line_docs = """
857874
Plot Series or DataFrame as lines.
858875
859876
This function is useful to plot lines using DataFrame's values
@@ -869,6 +886,7 @@ def line(self, x=None, y=None, **kwargs):
869886
The values to be plotted.
870887
Either the location or the label of the columns to be used.
871888
By default, it will use the remaining DataFrame numeric columns.
889+
%s
872890
**kwargs
873891
Keyword arguments to pass on to :meth:`DataFrame.plot`.
874892
@@ -919,10 +937,14 @@ def line(self, x=None, y=None, **kwargs):
919937
920938
>>> lines = df.plot.line(x='pig', y='horse')
921939
"""
922-
return self(kind="line", x=x, y=y, **kwargs)
923940

924-
def bar(self, x=None, y=None, **kwargs):
925-
"""
941+
@Appender(_line_docs % _shared_docs)
942+
def line(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
943+
sharey=False, layout=None, title=None, **kwargs):
944+
return self(kind="line", x=x, y=y, figsize=figsize, subplots=subplots,
945+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
946+
947+
_bar_docs = """
926948
Vertical bar plot.
927949
928950
A bar plot is a plot that presents categorical data with
@@ -939,6 +961,7 @@ def bar(self, x=None, y=None, **kwargs):
939961
y : label or position, optional
940962
Allows plotting of one column versus another. If not specified,
941963
all numerical columns are used.
964+
%s
942965
**kwargs
943966
Additional keyword arguments are documented in
944967
:meth:`DataFrame.plot`.
@@ -1004,10 +1027,14 @@ def bar(self, x=None, y=None, **kwargs):
10041027
10051028
>>> ax = df.plot.bar(x='lifespan', rot=0)
10061029
"""
1007-
return self(kind="bar", x=x, y=y, **kwargs)
10081030

1009-
def barh(self, x=None, y=None, **kwargs):
1010-
"""
1031+
@Appender(_bar_docs % _shared_docs)
1032+
def bar(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1033+
sharey=False, layout=None, title=None, **kwargs):
1034+
return self(kind="bar", x=x, y=y, figsize=figsize, subplots=subplots,
1035+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1036+
1037+
_barh_docs = """
10111038
Make a horizontal bar plot.
10121039
10131040
A horizontal bar plot is a plot that presents quantitative data with
@@ -1022,6 +1049,7 @@ def barh(self, x=None, y=None, **kwargs):
10221049
Column to be used for categories.
10231050
y : label or position, default All numeric columns in dataframe
10241051
Columns to be plotted from the DataFrame.
1052+
%s
10251053
**kwargs
10261054
Keyword arguments to pass on to :meth:`DataFrame.plot`.
10271055
@@ -1083,11 +1111,15 @@ def barh(self, x=None, y=None, **kwargs):
10831111
>>> df = pd.DataFrame({'speed': speed,
10841112
... 'lifespan': lifespan}, index=index)
10851113
>>> ax = df.plot.barh(x='lifespan')
1086-
"""
1087-
return self(kind="barh", x=x, y=y, **kwargs)
1114+
"""
1115+
1116+
@Appender(_barh_docs % _shared_docs)
1117+
def barh(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1118+
sharey=False, layout=None, title=None, **kwargs):
1119+
return self(kind="barh", x=x, y=y, figsize=figsize, subplots=subplots,
1120+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
10881121

1089-
def box(self, by=None, **kwargs):
1090-
r"""
1122+
_box_docs = r"""
10911123
Make a box plot of the DataFrame columns.
10921124
10931125
A box plot is a method for graphically depicting groups of numerical
@@ -1108,7 +1140,8 @@ def box(self, by=None, **kwargs):
11081140
----------
11091141
by : str or sequence
11101142
Column in the DataFrame to group by.
1111-
**kwargs
1143+
%s
1144+
**kwargs : optional
11121145
Additional keywords are documented in
11131146
:meth:`DataFrame.plot`.
11141147
@@ -1134,10 +1167,14 @@ def box(self, by=None, **kwargs):
11341167
>>> df = pd.DataFrame(data, columns=list('ABCD'))
11351168
>>> ax = df.plot.box()
11361169
"""
1137-
return self(kind="box", by=by, **kwargs)
11381170

1139-
def hist(self, by=None, bins=10, **kwargs):
1140-
"""
1171+
@Appender(_box_docs % _shared_docs)
1172+
def box(self, by=None, figsize=None, subplots=False, sharex=None, sharey=False,
1173+
layout=None, title=None, **kwargs):
1174+
return self(kind="box", by=by, figsize=figsize, subplots=subplots,
1175+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1176+
1177+
_hist_docs = """
11411178
Draw one histogram of the DataFrame's columns.
11421179
11431180
A histogram is a representation of the distribution of data.
@@ -1151,6 +1188,7 @@ def hist(self, by=None, bins=10, **kwargs):
11511188
Column in the DataFrame to group by.
11521189
bins : int, default 10
11531190
Number of histogram bins to be used.
1191+
%s
11541192
**kwargs
11551193
Additional keyword arguments are documented in
11561194
:meth:`DataFrame.plot`.
@@ -1181,10 +1219,13 @@ def hist(self, by=None, bins=10, **kwargs):
11811219
>>> df['two'] = df['one'] + np.random.randint(1, 7, 6000)
11821220
>>> ax = df.plot.hist(bins=12, alpha=0.5)
11831221
"""
1184-
return self(kind="hist", by=by, bins=bins, **kwargs)
11851222

1186-
def kde(self, bw_method=None, ind=None, **kwargs):
1187-
"""
1223+
@Appender(_hist_docs % _shared_docs)
1224+
def hist(self, by=None, bins=10, figsize=None, subplots=False, sharex=None,
1225+
sharey=False, layout=None, title=None, **kwargs):
1226+
return self(kind="hist", by=by, bins=bins, figsize=figsize, subplots=subplots,
1227+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1228+
_kde_docs = """
11881229
Generate Kernel Density Estimate plot using Gaussian kernels.
11891230
11901231
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
@@ -1207,9 +1248,10 @@ def kde(self, bw_method=None, ind=None, **kwargs):
12071248
1000 equally spaced points are used. If `ind` is a NumPy array, the
12081249
KDE is evaluated at the points passed. If `ind` is an integer,
12091250
`ind` number of equally spaced points are used.
1210-
**kwargs
1251+
%s
1252+
**kwargs : optional
12111253
Additional keyword arguments are documented in
1212-
:meth:`pandas.%(this-datatype)s.plot`.
1254+
:meth:`pandas.%%(this-datatype)s.plot`.
12131255
12141256
Returns
12151257
-------
@@ -1289,12 +1331,17 @@ def kde(self, bw_method=None, ind=None, **kwargs):
12891331
12901332
>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
12911333
"""
1292-
return self(kind="kde", bw_method=bw_method, ind=ind, **kwargs)
1334+
1335+
@Appender(_kde_docs % _shared_docs)
1336+
def kde(self, bw_method=None, ind=None, figsize=None, subplots=False, sharex=None,
1337+
sharey=False, layout=None, title=None, **kwargs):
1338+
return self(kind="kde", bw_method=bw_method, ind=ind, figsize=figsize,
1339+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1340+
**kwargs)
12931341

12941342
density = kde
12951343

1296-
def area(self, x=None, y=None, **kwargs):
1297-
"""
1344+
_area_docs = """
12981345
Draw a stacked area plot.
12991346
13001347
An area plot displays quantitative data visually.
@@ -1309,7 +1356,8 @@ def area(self, x=None, y=None, **kwargs):
13091356
stacked : bool, default True
13101357
Area plots are stacked by default. Set to False to create a
13111358
unstacked plot.
1312-
**kwargs
1359+
%s
1360+
**kwargs : optional
13131361
Additional keyword arguments are documented in
13141362
:meth:`DataFrame.plot`.
13151363
@@ -1364,10 +1412,14 @@ def area(self, x=None, y=None, **kwargs):
13641412
... })
13651413
>>> ax = df.plot.area(x='day')
13661414
"""
1367-
return self(kind="area", x=x, y=y, **kwargs)
13681415

1369-
def pie(self, **kwargs):
1370-
"""
1416+
@Appender(_area_docs % _shared_docs)
1417+
def area(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1418+
sharey=False, layout=None, title=None, **kwargs):
1419+
return self(kind="area", x=x, y=y, figsize=figsize, subplots=subplots,
1420+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1421+
1422+
_pie_docs = """
13711423
Generate a pie plot.
13721424
13731425
A pie plot is a proportional representation of the numerical data in a
@@ -1381,6 +1433,7 @@ def pie(self, **kwargs):
13811433
y : int or label, optional
13821434
Label or position of the column to plot.
13831435
If not provided, ``subplots=True`` argument must be passed.
1436+
%s
13841437
**kwargs
13851438
Keyword arguments to pass on to :meth:`DataFrame.plot`.
13861439
@@ -1413,16 +1466,16 @@ def pie(self, **kwargs):
14131466
14141467
>>> plot = df.plot.pie(subplots=True, figsize=(6, 3))
14151468
"""
1416-
if (
1417-
isinstance(self._parent, ABCDataFrame)
1418-
and kwargs.get("y", None) is None
1419-
and not kwargs.get("subplots", False)
1420-
):
1469+
1470+
@Appender(_pie_docs % _shared_docs)
1471+
def pie(self, y=None, figsize=None, subplots=False, sharex=None,
1472+
sharey=False, layout=None, title=None, **kwargs):
1473+
if isinstance(self._parent, ABCDataFrame) and y is None and not subplots:
14211474
raise ValueError("pie requires either y column or 'subplots=True'")
1422-
return self(kind="pie", **kwargs)
1475+
return self(kind="pie", y=y, figsize=figsize, subplots=subplots, sharex=sharex,
1476+
sharey=sharey, layout=layout, title=title, **kwargs)
14231477

1424-
def scatter(self, x, y, s=None, c=None, **kwargs):
1425-
"""
1478+
_scatter_docs = """
14261479
Create a scatter plot with varying marker point size and color.
14271480
14281481
The coordinates of each point are defined by two dataframe columns and
@@ -1462,7 +1515,7 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
14621515
14631516
- A column name or position whose values will be used to color the
14641517
marker points according to a colormap.
1465-
1518+
%s
14661519
**kwargs
14671520
Keyword arguments to pass on to :meth:`DataFrame.plot`.
14681521
@@ -1500,10 +1553,15 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
15001553
... c='species',
15011554
... colormap='viridis')
15021555
"""
1503-
return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs)
15041556

1505-
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
1506-
"""
1557+
@Appender(_scatter_docs % _shared_docs)
1558+
def scatter(self, x, y, s=None, c=None, figsize=None, subplots=False, sharex=None,
1559+
sharey=False, layout=None, title=None, **kwargs):
1560+
return self(kind="scatter", x=x, y=y, s=s, c=c, figsize=figsize,
1561+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1562+
title=title, **kwargs)
1563+
1564+
_hexbin_docs = """
15071565
Generate a hexagonal binning plot.
15081566
15091567
Generate a hexagonal binning plot of `x` versus `y`. If `C` is `None`
@@ -1535,6 +1593,7 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
15351593
Alternatively, gridsize can be a tuple with two elements
15361594
specifying the number of hexagons in the x-direction and the
15371595
y-direction.
1596+
%s
15381597
**kwargs
15391598
Additional keyword arguments are documented in
15401599
:meth:`DataFrame.plot`.
@@ -1584,12 +1643,14 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
15841643
... gridsize=10,
15851644
... cmap="viridis")
15861645
"""
1587-
if reduce_C_function is not None:
1588-
kwargs["reduce_C_function"] = reduce_C_function
1589-
if gridsize is not None:
1590-
kwargs["gridsize"] = gridsize
15911646

1592-
return self(kind="hexbin", x=x, y=y, C=C, **kwargs)
1647+
@Appender(_hexbin_docs % _shared_docs)
1648+
def hexbin(self, x, y, C=None, reduce_C_function=np.mean, gridsize=100,
1649+
figsize=None, subplots=False, sharex=None, sharey=False, layout=None,
1650+
title=None, **kwargs):
1651+
return self(kind="hexbin", x=x, y=y, C=C, reduce_C_function=reduce_C_function,
1652+
gridsize=gridsize, figsize=figsize, subplots=subplots,
1653+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
15931654

15941655

15951656
_backends = {}

0 commit comments

Comments
 (0)