Skip to content

Commit d1a843b

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

File tree

1 file changed

+108
-46
lines changed

1 file changed

+108
-46
lines changed

pandas/plotting/_core.py

Lines changed: 108 additions & 46 deletions
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,23 @@
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+
1938

2039
def hist_series(
2140
self,
@@ -588,7 +607,7 @@ class PlotAccessor(PandasObject):
588607
labels with "(right)" in the legend
589608
include_bool : bool, default is False
590609
If True, boolean values can be plotted.
591-
**kwargs
610+
`**kwargs` : keywords
592611
Options to pass to matplotlib plotting method.
593612
594613
Returns
@@ -795,8 +814,7 @@ def __call__(self, *args, **kwargs):
795814

796815
return plot_backend.plot(data, kind=kind, **kwargs)
797816

798-
def line(self, x=None, y=None, **kwargs):
799-
"""
817+
_line_docs = """
800818
Plot Series or DataFrame as lines.
801819
802820
This function is useful to plot lines using DataFrame's values
@@ -812,6 +830,7 @@ def line(self, x=None, y=None, **kwargs):
812830
The values to be plotted.
813831
Either the location or the label of the columns to be used.
814832
By default, it will use the remaining DataFrame numeric columns.
833+
%s
815834
**kwargs
816835
Keyword arguments to pass on to :meth:`DataFrame.plot`.
817836
@@ -862,10 +881,14 @@ def line(self, x=None, y=None, **kwargs):
862881
863882
>>> lines = df.plot.line(x='pig', y='horse')
864883
"""
865-
return self(kind="line", x=x, y=y, **kwargs)
866884

867-
def bar(self, x=None, y=None, **kwargs):
868-
"""
885+
@Appender(_line_docs % _shared_docs)
886+
def line(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
887+
sharey=False, layout=None, title=None, **kwargs):
888+
return self(kind="line", x=x, y=y, figsize=figsize, subplots=subplots,
889+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
890+
891+
_bar_docs = """
869892
Vertical bar plot.
870893
871894
A bar plot is a plot that presents categorical data with
@@ -882,6 +905,7 @@ def bar(self, x=None, y=None, **kwargs):
882905
y : label or position, optional
883906
Allows plotting of one column versus another. If not specified,
884907
all numerical columns are used.
908+
%s
885909
**kwargs
886910
Additional keyword arguments are documented in
887911
:meth:`DataFrame.plot`.
@@ -947,10 +971,14 @@ def bar(self, x=None, y=None, **kwargs):
947971
948972
>>> ax = df.plot.bar(x='lifespan', rot=0)
949973
"""
950-
return self(kind="bar", x=x, y=y, **kwargs)
951974

952-
def barh(self, x=None, y=None, **kwargs):
953-
"""
975+
@Appender(_bar_docs % _shared_docs)
976+
def bar(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
977+
sharey=False, layout=None, title=None, **kwargs):
978+
return self(kind="bar", x=x, y=y, figsize=figsize, subplots=subplots,
979+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
980+
981+
_barh_docs = """
954982
Make a horizontal bar plot.
955983
956984
A horizontal bar plot is a plot that presents quantitative data with
@@ -965,6 +993,7 @@ def barh(self, x=None, y=None, **kwargs):
965993
Column to be used for categories.
966994
y : label or position, default All numeric columns in dataframe
967995
Columns to be plotted from the DataFrame.
996+
%s
968997
**kwargs
969998
Keyword arguments to pass on to :meth:`DataFrame.plot`.
970999
@@ -1026,11 +1055,15 @@ def barh(self, x=None, y=None, **kwargs):
10261055
>>> df = pd.DataFrame({'speed': speed,
10271056
... 'lifespan': lifespan}, index=index)
10281057
>>> ax = df.plot.barh(x='lifespan')
1029-
"""
1030-
return self(kind="barh", x=x, y=y, **kwargs)
1058+
"""
10311059

1032-
def box(self, by=None, **kwargs):
1033-
r"""
1060+
@Appender(_barh_docs % _shared_docs)
1061+
def barh(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1062+
sharey=False, layout=None, title=None, **kwargs):
1063+
return self(kind="barh", x=x, y=y, figsize=figsize, subplots=subplots,
1064+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1065+
1066+
_box_docs = r"""
10341067
Make a box plot of the DataFrame columns.
10351068
10361069
A box plot is a method for graphically depicting groups of numerical
@@ -1051,7 +1084,8 @@ def box(self, by=None, **kwargs):
10511084
----------
10521085
by : str or sequence
10531086
Column in the DataFrame to group by.
1054-
**kwargs
1087+
%s
1088+
**kwargs : optional
10551089
Additional keywords are documented in
10561090
:meth:`DataFrame.plot`.
10571091
@@ -1077,10 +1111,14 @@ def box(self, by=None, **kwargs):
10771111
>>> df = pd.DataFrame(data, columns=list('ABCD'))
10781112
>>> ax = df.plot.box()
10791113
"""
1080-
return self(kind="box", by=by, **kwargs)
10811114

1082-
def hist(self, by=None, bins=10, **kwargs):
1083-
"""
1115+
@Appender(_box_docs % _shared_docs)
1116+
def box(self, by=None, figsize=None, subplots=False, sharex=None, sharey=False,
1117+
layout=None, title=None, **kwargs):
1118+
return self(kind="box", by=by, figsize=figsize, subplots=subplots,
1119+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1120+
1121+
_hist_docs = """
10841122
Draw one histogram of the DataFrame's columns.
10851123
10861124
A histogram is a representation of the distribution of data.
@@ -1094,6 +1132,7 @@ def hist(self, by=None, bins=10, **kwargs):
10941132
Column in the DataFrame to group by.
10951133
bins : int, default 10
10961134
Number of histogram bins to be used.
1135+
%s
10971136
**kwargs
10981137
Additional keyword arguments are documented in
10991138
:meth:`DataFrame.plot`.
@@ -1124,10 +1163,13 @@ def hist(self, by=None, bins=10, **kwargs):
11241163
>>> df['two'] = df['one'] + np.random.randint(1, 7, 6000)
11251164
>>> ax = df.plot.hist(bins=12, alpha=0.5)
11261165
"""
1127-
return self(kind="hist", by=by, bins=bins, **kwargs)
11281166

1129-
def kde(self, bw_method=None, ind=None, **kwargs):
1130-
"""
1167+
@Appender(_hist_docs % _shared_docs)
1168+
def hist(self, by=None, bins=10, figsize=None, subplots=False, sharex=None,
1169+
sharey=False, layout=None, title=None, **kwargs):
1170+
return self(kind="hist", by=by, bins=bins, figsize=figsize, subplots=subplots,
1171+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1172+
_kde_docs = """
11311173
Generate Kernel Density Estimate plot using Gaussian kernels.
11321174
11331175
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
@@ -1150,9 +1192,10 @@ def kde(self, bw_method=None, ind=None, **kwargs):
11501192
1000 equally spaced points are used. If `ind` is a NumPy array, the
11511193
KDE is evaluated at the points passed. If `ind` is an integer,
11521194
`ind` number of equally spaced points are used.
1153-
**kwargs
1195+
%s
1196+
**kwargs : optional
11541197
Additional keyword arguments are documented in
1155-
:meth:`pandas.%(this-datatype)s.plot`.
1198+
:meth:`pandas.%%(this-datatype)s.plot`.
11561199
11571200
Returns
11581201
-------
@@ -1232,12 +1275,17 @@ def kde(self, bw_method=None, ind=None, **kwargs):
12321275
12331276
>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
12341277
"""
1235-
return self(kind="kde", bw_method=bw_method, ind=ind, **kwargs)
1278+
1279+
@Appender(_kde_docs % _shared_docs)
1280+
def kde(self, bw_method=None, ind=None, figsize=None, subplots=False, sharex=None,
1281+
sharey=False, layout=None, title=None, **kwargs):
1282+
return self(kind="kde", bw_method=bw_method, ind=ind, figsize=figsize,
1283+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1284+
**kwargs)
12361285

12371286
density = kde
12381287

1239-
def area(self, x=None, y=None, **kwargs):
1240-
"""
1288+
_area_docs = """
12411289
Draw a stacked area plot.
12421290
12431291
An area plot displays quantitative data visually.
@@ -1252,7 +1300,8 @@ def area(self, x=None, y=None, **kwargs):
12521300
stacked : bool, default True
12531301
Area plots are stacked by default. Set to False to create a
12541302
unstacked plot.
1255-
**kwargs
1303+
%s
1304+
**kwargs : optional
12561305
Additional keyword arguments are documented in
12571306
:meth:`DataFrame.plot`.
12581307
@@ -1307,10 +1356,14 @@ def area(self, x=None, y=None, **kwargs):
13071356
... })
13081357
>>> ax = df.plot.area(x='day')
13091358
"""
1310-
return self(kind="area", x=x, y=y, **kwargs)
13111359

1312-
def pie(self, **kwargs):
1313-
"""
1360+
@Appender(_area_docs % _shared_docs)
1361+
def area(self, x=None, y=None, figsize=None, subplots=False, sharex=None,
1362+
sharey=False, layout=None, title=None, **kwargs):
1363+
return self(kind="area", x=x, y=y, figsize=figsize, subplots=subplots,
1364+
sharex=sharex, sharey=sharey, layout=layout, title=title, **kwargs)
1365+
1366+
_pie_docs = """
13141367
Generate a pie plot.
13151368
13161369
A pie plot is a proportional representation of the numerical data in a
@@ -1324,6 +1377,7 @@ def pie(self, **kwargs):
13241377
y : int or label, optional
13251378
Label or position of the column to plot.
13261379
If not provided, ``subplots=True`` argument must be passed.
1380+
%s
13271381
**kwargs
13281382
Keyword arguments to pass on to :meth:`DataFrame.plot`.
13291383
@@ -1356,16 +1410,16 @@ def pie(self, **kwargs):
13561410
13571411
>>> plot = df.plot.pie(subplots=True, figsize=(6, 3))
13581412
"""
1359-
if (
1360-
isinstance(self._parent, ABCDataFrame)
1361-
and kwargs.get("y", None) is None
1362-
and not kwargs.get("subplots", False)
1363-
):
1413+
1414+
@Appender(_pie_docs % _shared_docs)
1415+
def pie(self, y=None, figsize=None, subplots=False, sharex=None,
1416+
sharey=False, layout=None, title=None, **kwargs):
1417+
if isinstance(self._parent, ABCDataFrame) and y is None and not subplots:
13641418
raise ValueError("pie requires either y column or 'subplots=True'")
1365-
return self(kind="pie", **kwargs)
1419+
return self(kind="pie", y=y, figsize=figsize, subplots=subplots, sharex=sharex,
1420+
sharey=sharey, layout=layout, title=title, **kwargs)
13661421

1367-
def scatter(self, x, y, s=None, c=None, **kwargs):
1368-
"""
1422+
_scatter_docs = """
13691423
Create a scatter plot with varying marker point size and color.
13701424
13711425
The coordinates of each point are defined by two dataframe columns and
@@ -1405,7 +1459,7 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
14051459
14061460
- A column name or position whose values will be used to color the
14071461
marker points according to a colormap.
1408-
1462+
%s
14091463
**kwargs
14101464
Keyword arguments to pass on to :meth:`DataFrame.plot`.
14111465
@@ -1443,10 +1497,15 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
14431497
... c='species',
14441498
... colormap='viridis')
14451499
"""
1446-
return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs)
14471500

1448-
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
1449-
"""
1501+
@Appender(_scatter_docs % _shared_docs)
1502+
def scatter(self, x, y, s=None, c=None, figsize=None, subplots=False, sharex=None,
1503+
sharey=False, layout=None, title=None, **kwargs):
1504+
return self(kind="scatter", x=x, y=y, s=s, c=c, figsize=figsize,
1505+
subplots=subplots, sharex=sharex, sharey=sharey, layout=layout,
1506+
title=title, **kwargs)
1507+
1508+
_hexbin_docs = """
14501509
Generate a hexagonal binning plot.
14511510
14521511
Generate a hexagonal binning plot of `x` versus `y`. If `C` is `None`
@@ -1478,6 +1537,7 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
14781537
Alternatively, gridsize can be a tuple with two elements
14791538
specifying the number of hexagons in the x-direction and the
14801539
y-direction.
1540+
%s
14811541
**kwargs
14821542
Additional keyword arguments are documented in
14831543
:meth:`DataFrame.plot`.
@@ -1527,12 +1587,14 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
15271587
... gridsize=10,
15281588
... cmap="viridis")
15291589
"""
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
15341590

1535-
return self(kind="hexbin", x=x, y=y, C=C, **kwargs)
1591+
@Appender(_hexbin_docs % _shared_docs)
1592+
def hexbin(self, x, y, C=None, reduce_C_function=np.mean, gridsize=100,
1593+
figsize=None, subplots=False, sharex=None, sharey=False, layout=None,
1594+
title=None, **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)