Skip to content

Commit 81dea8a

Browse files
author
Marco Gorelli
committed
Rebase, add docstrings
1 parent 9a999fe commit 81dea8a

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

pandas/plotting/_core.py

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

pandas/plotting/_matplotlib/core.py

Lines changed: 1 addition & 0 deletions
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)