Skip to content

Commit 9a2f226

Browse files
author
Hancar, Pavel
committed
11042 refactor(plot sub-methods): signatures
1 parent 9cfb8b5 commit 9a2f226

File tree

1 file changed

+109
-46
lines changed

1 file changed

+109
-46
lines changed

pandas/plotting/_core.py

+109-46
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.compat._optional import import_optional_dependency
@@ -16,6 +18,24 @@
1618
# we can lazily import matplotlib.
1719
import_optional_dependency("pandas.plotting._matplotlib", raise_on_missing=False)
1820

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+
1939

2040
def hist_series(
2141
self,
@@ -588,7 +608,7 @@ class PlotAccessor(PandasObject):
588608
labels with "(right)" in the legend
589609
include_bool : bool, default is False
590610
If True, boolean values can be plotted.
591-
**kwargs
611+
`**kwargs` : keywords
592612
Options to pass to matplotlib plotting method.
593613
594614
Returns
@@ -795,8 +815,7 @@ def __call__(self, *args, **kwargs):
795815

796816
return plot_backend.plot(data, kind=kind, **kwargs)
797817

798-
def line(self, x=None, y=None, **kwargs):
799-
"""
818+
_line_docs = """
800819
Plot Series or DataFrame as lines.
801820
802821
This function is useful to plot lines using DataFrame's values
@@ -812,6 +831,7 @@ def line(self, x=None, y=None, **kwargs):
812831
The values to be plotted.
813832
Either the location or the label of the columns to be used.
814833
By default, it will use the remaining DataFrame numeric columns.
834+
%s
815835
**kwargs
816836
Keyword arguments to pass on to :meth:`DataFrame.plot`.
817837
@@ -862,10 +882,14 @@ def line(self, x=None, y=None, **kwargs):
862882
863883
>>> lines = df.plot.line(x='pig', y='horse')
864884
"""
865-
return self(kind="line", x=x, y=y, **kwargs)
866885

867-
def bar(self, x=None, y=None, **kwargs):
868-
"""
886+
@Appender(_line_docs % _shared_docs)
887+
def line(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
888+
sharey=False, layout=None, title=None, **kwargs):
889+
return self(kind="line", x=x, y=y, figsize=figsize, subplots=subplots,
890+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
891+
892+
_bar_docs = """
869893
Vertical bar plot.
870894
871895
A bar plot is a plot that presents categorical data with
@@ -882,6 +906,7 @@ 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.
909+
%s
885910
**kwargs
886911
Additional keyword arguments are documented in
887912
:meth:`DataFrame.plot`.
@@ -947,10 +972,14 @@ 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)
951975

952-
def barh(self, x=None, y=None, **kwargs):
953-
"""
976+
@Appender(_bar_docs % _shared_docs)
977+
def bar(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
978+
sharey=False, layout=None, title=None, **kwargs):
979+
return self(kind="bar", x=x, y=y, figsize=figsize, subplots=subplots,
980+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
981+
982+
_barh_docs = """
954983
Make a horizontal bar plot.
955984
956985
A horizontal bar plot is a plot that presents quantitative data with
@@ -965,6 +994,7 @@ def barh(self, x=None, y=None, **kwargs):
965994
Column to be used for categories.
966995
y : label or position, default All numeric columns in dataframe
967996
Columns to be plotted from the DataFrame.
997+
%s
968998
**kwargs
969999
Keyword arguments to pass on to :meth:`DataFrame.plot`.
9701000
@@ -1026,11 +1056,15 @@ def barh(self, x=None, y=None, **kwargs):
10261056
>>> df = pd.DataFrame({'speed': speed,
10271057
... 'lifespan': lifespan}, index=index)
10281058
>>> ax = df.plot.barh(x='lifespan')
1029-
"""
1030-
return self(kind="barh", x=x, y=y, **kwargs)
1059+
"""
10311060

1032-
def box(self, by=None, **kwargs):
1033-
r"""
1061+
@Appender(_barh_docs % _shared_docs)
1062+
def barh(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1063+
sharey=False, layout=None, title=None, **kwargs):
1064+
return self(kind="barh", x=x, y=y, figsize=figsize, subplots=subplots,
1065+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1066+
1067+
_box_docs = r"""
10341068
Make a box plot of the DataFrame columns.
10351069
10361070
A box plot is a method for graphically depicting groups of numerical
@@ -1051,7 +1085,8 @@ def box(self, by=None, **kwargs):
10511085
----------
10521086
by : str or sequence
10531087
Column in the DataFrame to group by.
1054-
**kwargs
1088+
%s
1089+
**kwargs : optional
10551090
Additional keywords are documented in
10561091
:meth:`DataFrame.plot`.
10571092
@@ -1077,10 +1112,14 @@ def box(self, by=None, **kwargs):
10771112
>>> df = pd.DataFrame(data, columns=list('ABCD'))
10781113
>>> ax = df.plot.box()
10791114
"""
1080-
return self(kind="box", by=by, **kwargs)
10811115

1082-
def hist(self, by=None, bins=10, **kwargs):
1083-
"""
1116+
@Appender(_box_docs % _shared_docs)
1117+
def box(self, by=None, figsize=None, subplots=False, sharex=None, sharey=False,
1118+
layout=None, title=None, **kwargs):
1119+
return self(kind="box", by=by, figsize=figsize, subplots=subplots,
1120+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1121+
1122+
_hist_docs = """
10841123
Draw one histogram of the DataFrame's columns.
10851124
10861125
A histogram is a representation of the distribution of data.
@@ -1094,6 +1133,7 @@ def hist(self, by=None, bins=10, **kwargs):
10941133
Column in the DataFrame to group by.
10951134
bins : int, default 10
10961135
Number of histogram bins to be used.
1136+
%s
10971137
**kwargs
10981138
Additional keyword arguments are documented in
10991139
:meth:`DataFrame.plot`.
@@ -1124,10 +1164,13 @@ def hist(self, by=None, bins=10, **kwargs):
11241164
>>> df['two'] = df['one'] + np.random.randint(1, 7, 6000)
11251165
>>> ax = df.plot.hist(bins=12, alpha=0.5)
11261166
"""
1127-
return self(kind="hist", by=by, bins=bins, **kwargs)
11281167

1129-
def kde(self, bw_method=None, ind=None, **kwargs):
1130-
"""
1168+
@Appender(_hist_docs % _shared_docs)
1169+
def hist(self, by=None, bins=10, figsize=None, subplots=False, sharex=None,
1170+
sharey=False, layout=None, title=None, **kwargs):
1171+
return self(kind="hist", by=by, bins=bins, figsize=figsize, subplots=subplots,
1172+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1173+
_kde_docs = """
11311174
Generate Kernel Density Estimate plot using Gaussian kernels.
11321175
11331176
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
@@ -1150,9 +1193,10 @@ def kde(self, bw_method=None, ind=None, **kwargs):
11501193
1000 equally spaced points are used. If `ind` is a NumPy array, the
11511194
KDE is evaluated at the points passed. If `ind` is an integer,
11521195
`ind` number of equally spaced points are used.
1153-
**kwargs
1196+
%s
1197+
**kwargs : optional
11541198
Additional keyword arguments are documented in
1155-
:meth:`pandas.%(this-datatype)s.plot`.
1199+
:meth:`pandas.%%(this-datatype)s.plot`.
11561200
11571201
Returns
11581202
-------
@@ -1232,12 +1276,17 @@ def kde(self, bw_method=None, ind=None, **kwargs):
12321276
12331277
>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
12341278
"""
1235-
return self(kind="kde", bw_method=bw_method, ind=ind, **kwargs)
1279+
1280+
@Appender(_kde_docs % _shared_docs)
1281+
def kde(self, bw_method=None, ind=None, figsize=None, subplots=False, sharex=None,
1282+
sharey=False, layout=None, title=None, **kwargs):
1283+
return self(kind="kde", bw_method=bw_method, ind=ind, figsize=figsize,
1284+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1285+
**kwargs)
12361286

12371287
density = kde
12381288

1239-
def area(self, x=None, y=None, **kwargs):
1240-
"""
1289+
_area_docs = """
12411290
Draw a stacked area plot.
12421291
12431292
An area plot displays quantitative data visually.
@@ -1252,7 +1301,8 @@ def area(self, x=None, y=None, **kwargs):
12521301
stacked : bool, default True
12531302
Area plots are stacked by default. Set to False to create a
12541303
unstacked plot.
1255-
**kwargs
1304+
%s
1305+
**kwargs : optional
12561306
Additional keyword arguments are documented in
12571307
:meth:`DataFrame.plot`.
12581308
@@ -1307,10 +1357,14 @@ def area(self, x=None, y=None, **kwargs):
13071357
... })
13081358
>>> ax = df.plot.area(x='day')
13091359
"""
1310-
return self(kind="area", x=x, y=y, **kwargs)
13111360

1312-
def pie(self, **kwargs):
1313-
"""
1361+
@Appender(_area_docs % _shared_docs)
1362+
def area(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1363+
sharey=False, layout=None, title=None, **kwargs):
1364+
return self(kind="area", x=x, y=y, figsize=figsize, subplots=subplots,
1365+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1366+
1367+
_pie_docs = """
13141368
Generate a pie plot.
13151369
13161370
A pie plot is a proportional representation of the numerical data in a
@@ -1324,6 +1378,7 @@ def pie(self, **kwargs):
13241378
y : int or label, optional
13251379
Label or position of the column to plot.
13261380
If not provided, ``subplots=True`` argument must be passed.
1381+
%s
13271382
**kwargs
13281383
Keyword arguments to pass on to :meth:`DataFrame.plot`.
13291384
@@ -1356,16 +1411,16 @@ def pie(self, **kwargs):
13561411
13571412
>>> plot = df.plot.pie(subplots=True, figsize=(6, 3))
13581413
"""
1359-
if (
1360-
isinstance(self._parent, ABCDataFrame)
1361-
and kwargs.get("y", None) is None
1362-
and not kwargs.get("subplots", False)
1363-
):
1414+
1415+
@Appender(_pie_docs % _shared_docs)
1416+
def pie(self, y=None, figsize=None, subplots=False, sharex=None,
1417+
sharey=False, layout=None, title=None, **kwargs):
1418+
if isinstance(self._parent, ABCDataFrame) and y is None and not subplots:
13641419
raise ValueError("pie requires either y column or 'subplots=True'")
1365-
return self(kind="pie", **kwargs)
1420+
return self(kind="pie", y=y, figsize=figsize, subplots=subplots, sharex=sharex,
1421+
sharey=sharey, layout=layout, title=title, **kwargs)
13661422

1367-
def scatter(self, x, y, s=None, c=None, **kwargs):
1368-
"""
1423+
_scatter_docs = """
13691424
Create a scatter plot with varying marker point size and color.
13701425
13711426
The coordinates of each point are defined by two dataframe columns and
@@ -1405,7 +1460,7 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
14051460
14061461
- A column name or position whose values will be used to color the
14071462
marker points according to a colormap.
1408-
1463+
%s
14091464
**kwargs
14101465
Keyword arguments to pass on to :meth:`DataFrame.plot`.
14111466
@@ -1443,10 +1498,15 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
14431498
... c='species',
14441499
... colormap='viridis')
14451500
"""
1446-
return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs)
14471501

1448-
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
1449-
"""
1502+
@Appender(_scatter_docs % _shared_docs)
1503+
def scatter(self, x, y, s=None, c=None, figsize=None, subplots=False, sharex=None,
1504+
sharey=False, layout=None, title=None, **kwargs):
1505+
return self(kind="scatter", x=x, y=y, s=s, c=c, figsize=figsize,
1506+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1507+
title=title, **kwargs)
1508+
1509+
_hexbin_docs = """
14501510
Generate a hexagonal binning plot.
14511511
14521512
Generate a hexagonal binning plot of `x` versus `y`. If `C` is `None`
@@ -1478,6 +1538,7 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
14781538
Alternatively, gridsize can be a tuple with two elements
14791539
specifying the number of hexagons in the x-direction and the
14801540
y-direction.
1541+
%s
14811542
**kwargs
14821543
Additional keyword arguments are documented in
14831544
:meth:`DataFrame.plot`.
@@ -1527,12 +1588,14 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
15271588
... gridsize=10,
15281589
... cmap="viridis")
15291590
"""
1530-
if reduce_C_function is not None:
1531-
kwargs["reduce_C_function"] = reduce_C_function
1532-
if gridsize is not None:
1533-
kwargs["gridsize"] = gridsize
15341591

1535-
return self(kind="hexbin", x=x, y=y, C=C, **kwargs)
1592+
@Appender(_hexbin_docs % _shared_docs)
1593+
def hexbin(self, x, y, C=None, reduce_C_function=np.mean, gridsize=100,
1594+
figsize=None, subplots=False, sharex=None, sharey=False, layout=None,
1595+
title=None, **kwargs):
1596+
return self(kind="hexbin", x=x, y=y, C=C, reduce_C_function=reduce_C_function,
1597+
gridsize=gridsize, figsize=figsize, subplots=subplots,
1598+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
15361599

15371600

15381601
_backends = {}

0 commit comments

Comments
 (0)