Skip to content

Commit ac5a96f

Browse files
author
Marco Gorelli
committed
Rebase, add docstrings
1 parent 8f7ee75 commit ac5a96f

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

pandas/plotting/_core.py

+73
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,21 @@ def line(self, x=None, y=None, **kwargs):
864864
The values to be plotted.
865865
Either the location or the label of the columns to be used.
866866
By default, it will use the remaining DataFrame numeric columns.
867+
color : str, int, array_like, or dict, optional
868+
The color of each line for each row. Possible values are:
869+
870+
- A single color string referred to by name, RGB or RGBA code,
871+
for instance 'red' or '#a98d19'.
872+
873+
- A sequence of color strings referred to by name, RGB or RGBA
874+
code, which will be used for each line for each row recursively. For
875+
instance ['green','yellow'] all lines for each row will be filled in green
876+
or yellow, alternatively.
877+
878+
- A dict of the form {column name : color}, so that each row's lines will be
879+
colored accordingly. For example, if your columns are called `a` and `b`,
880+
then passing {'a': 'green', 'b': 'red'} will color the lines for column
881+
`a` in green and lines for column `b` in red.
867882
**kwargs
868883
Keyword arguments to pass on to :meth:`DataFrame.plot`.
869884
@@ -906,6 +921,16 @@ def line(self, x=None, y=None, **kwargs):
906921
>>> type(axes)
907922
<class 'numpy.ndarray'>
908923
924+
.. plot::
925+
:context: close-figs
926+
927+
Let's repeat the same example, but specifying colors for
928+
each column (in this case, for each animal).
929+
930+
>>> axes = df.plot.line(
931+
... subplots=True, color={"pig": "pink", "horse": "#742802"}
932+
... )
933+
909934
.. plot::
910935
:context: close-figs
911936
@@ -934,6 +959,21 @@ def bar(self, x=None, y=None, **kwargs):
934959
y : label or position, optional
935960
Allows plotting of one column versus another. If not specified,
936961
all numerical columns are used.
962+
color : str, int, array_like, or dict, optional
963+
The color of each bar for each row. Possible values are:
964+
965+
- A single color string referred to by name, RGB or RGBA code,
966+
for instance 'red' or '#a98d19'.
967+
968+
- A sequence of color strings referred to by name, RGB or RGBA
969+
code, which will be used for each bar for each row recursively. For
970+
instance ['green','yellow'] all bars for each row will be filled in green
971+
or yellow, alternatively.
972+
973+
- A dict of the form {column name : color}, so that each row's bars will be
974+
colored accordingly. For example, if your columns are called `a` and `b`,
975+
then passing {'a': 'green', 'b': 'red'} will color bars for column `a` in
976+
green and bars for column `b` in red.
937977
**kwargs
938978
Additional keyword arguments are documented in
939979
:meth:`DataFrame.plot`.
@@ -985,6 +1025,17 @@ def bar(self, x=None, y=None, **kwargs):
9851025
>>> axes = df.plot.bar(rot=0, subplots=True)
9861026
>>> axes[1].legend(loc=2) # doctest: +SKIP
9871027
1028+
If we don't like the default colours, we can specify how we'd
1029+
like each column to be colored.
1030+
1031+
.. plot::
1032+
:context: close-figs
1033+
1034+
>>> axes = df.plot.bar(
1035+
... rot=0, subplots=True, color={"speed": "red", "lifespan": "green"}
1036+
... )
1037+
>>> axes[1].legend(loc=2) # doctest: +SKIP
1038+
9881039
Plot a single column.
9891040
9901041
.. plot::
@@ -1017,6 +1068,21 @@ def barh(self, x=None, y=None, **kwargs):
10171068
Column to be used for categories.
10181069
y : label or position, default All numeric columns in dataframe
10191070
Columns to be plotted from the DataFrame.
1071+
color : str, int, array_like, or dict, optional
1072+
The color of each bar for each row. Possible values are:
1073+
1074+
- A single color string referred to by name, RGB or RGBA code,
1075+
for instance 'red' or '#a98d19'.
1076+
1077+
- A sequence of color strings referred to by name, RGB or RGBA
1078+
code, which will be used for each bar for each row recursively. For
1079+
instance ['green','yellow'] all bars for each row will be filled in green
1080+
or yellow, alternatively.
1081+
1082+
- A dict of the form {column name : color}, so that each row's bars will be
1083+
colored accordingly. For example, if your columns are called `a` and `b`,
1084+
then passing {'a': 'green', 'b': 'red'} will color bars for column `a` in
1085+
green and bars for column `b` in red.
10201086
**kwargs
10211087
Keyword arguments to pass on to :meth:`DataFrame.plot`.
10221088
@@ -1053,6 +1119,13 @@ def barh(self, x=None, y=None, **kwargs):
10531119
... 'lifespan': lifespan}, index=index)
10541120
>>> ax = df.plot.barh()
10551121
1122+
We can specify colors for each column
1123+
1124+
.. plot::
1125+
:context: close-figs
1126+
1127+
>>> ax = df.plot.barh(color={"speed": "red", "lifespan": "green"})
1128+
10561129
Plot a column of the DataFrame to a horizontal bar plot
10571130
10581131
.. plot::

pandas/plotting/_matplotlib/core.py

+1
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ def _make_plot(self):
13441344

13451345
pos_prior = neg_prior = np.zeros(len(self.data))
13461346
K = self.nseries
1347+
13471348
for i, (label, y) in enumerate(self._iter_data(fillna=0)):
13481349
ax = self._get_ax(i)
13491350
kwds = self.kwds.copy()

0 commit comments

Comments
 (0)