8000 ENH: accept a dictionary in plot colors by MarcoGorelli · Pull Request #31071 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: accept a dictionary in plot colors #31071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 26, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
🔀 fix conflict
  • Loading branch information
Marco Gorelli committed Jan 23, 2020
commit a71f49aa26ff41f5dd03e7b92e3e00f659caf512
190 changes: 75 additions & 115 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,45 @@ def hist_frame(
"""


_bar_or_line_doc = """
Parameters
----------
x : label or position, optional
Allows plotting of one column versus another. If not specified,
the index of the DataFrame is used.
y : label or position, optional
Allows plotting of one column versus another. If not specified,
all numerical columns are used.
color : str, array_like, or dict, optional
The color for each of the DataFrame's columns. Possible values are:

- A single color string referred to by name, RGB or RGBA code,
for instance 'red' or '#a98d19'.

- A sequence of color strings referred to by name, RGB or RGBA
code, which will be used for each column recursively. For
instance ['green','yellow'] each column's %(kind)s will be filled in
green or yellow, alternatively.

- A dict of the form {column name : color}, so that each column will be
colored accordingly. For example, if your columns are called `a` and
`b`, then passing {'a': 'green', 'b': 'red'} will color %(kind)ss for
column `a` in green and %(kind)ss for column `b` in red.

.. versionadded:: 1.1.0

**kwargs
Additional keyword arguments are documented in
:meth:`DataFrame.plot`.

Returns
-------
matplotlib.axes.Axes or np.ndarray of them
An ndarray is returned with one :class:`matplotlib.axes.Axes`
per column when ``subplots=True``.
"""


@Substitution(backend="")
@Appender(_boxplot_doc)
def boxplot(
Expand Down Expand Up @@ -848,46 +887,8 @@ def __call__(self, *args, **kwargs):

__call__.__doc__ = __doc__

def line(self, x=None, y=None, **kwargs):
@Appender(
"""
Plot Series or DataFrame as lines.

This function is useful to plot lines using DataFrame's values
as coordinates.

Parameters
----------
x : int or str, optional
Columns to use for the horizontal axis.
Either the location or the label of the columns to be used.
By default, it will use the DataFrame indices.
y : int, str, or list of them, optional
The values to be plotted.
Either the location or the label of the columns to be used.
By default, it will use the remaining DataFrame numeric columns.
color : str, int, array_like, or dict, optional
The color for each of the DataFrame's columns. Possible values are:

- A single color string referred to by name, RGB or RGBA code,
for instance 'red' or '#a98d19'.

- A sequence of color strings referred to by name, RGB or RGBA
code, which will be used for each column recursively. For
instance ['green','yellow'] each column's line will be coloured in
green or yellow, alternatively.

- A dict of the form {column name : color}, so that each column will be
colored accordingly. For example, if your columns are called `a` and `b`,
then passing {'a': 'green', 'b': 'red'} will color lines for column `a` in
green and lines for column `b` in red.
**kwargs
Keyword arguments to pass on to :meth:`DataFrame.plot`.

Returns
-------
:class:`matplotlib.axes.Axes` or :class:`numpy.ndarray`
Return an ndarray when ``subplots=True``.

See Also
--------
matplotlib.pyplot.plot : Plot y versus x as lines and/or markers.
Expand Down Expand Up @@ -940,51 +941,20 @@ def line(self, x=None, y=None, **kwargs):

>>> lines = df.plot.line(x='pig', y='horse')
"""
return self(kind="line", x=x, y=y, **kwargs)

def bar(self, x=None, y=None, **kwargs):
)
@Substitution(kind="line")
@Appender(_bar_or_line_doc)
def line(self, x=None, y=None, **kwargs):
"""
Vertical bar plot.

A bar plot is a plot that presents categorical data with
rectangular bars with lengths proportional to the values that they
represent. A bar plot shows comparisons among discrete categories. One
axis of the plot shows the specific categories being compared, and the
other axis represents a measured value.

Parameters
----------
x : label or position, optional
Allows plotting of one column versus another. If not specified,
the index of the DataFrame is used.
y : label or position, optional
Allows plotting of one column versus another. If not specified,
all numerical columns are used.
color : str, int, array_like, or dict, optional
The color for each of the DataFrame's columns. Possible values are:

- A single color string referred to by name, RGB or RGBA code,
for instance 'red' or '#a98d19'.

- A sequence of color strings referred to by name, RGB or RGBA
code, which will be used for each column recursively. For
instance ['green','yellow'] each column's bar will be filled in
green or yellow, alternatively.

- A dict of the form {column name : color}, so that each column will be
colored accordingly. For example, if your columns are called `a` and `b`,
then passing {'a': 'green', 'b': 'red'} will color bars for column `a` in
green and bars for column `b` in red.
**kwargs
Additional keyword arguments are documented in
:meth:`DataFrame.plot`.
Plot Series or DataFrame as lines.

Returns
-------
matplotlib.axes.Axes or np.ndarray of them
An ndarray is returned with one :class:`matplotlib.axes.Axes`
per column when ``subplots=True``.
This function is useful to plot lines using DataFrame's values
as coordinates.
"""
return self(kind="line", x=x, y=y, **kwargs)

@Appender(
"""
See Also
--------
DataFrame.plot.barh : Horizontal bar plot.
Expand Down Expand Up @@ -1050,47 +1020,24 @@ def bar(self, x=None, y=None, **kwargs):
:context: close-figs

>>> ax = df.plot.bar(x='lifespan', rot=0)
"""
)
@Substitution(kind="bar")
@Appender(_bar_or_line_doc)
def bar(self, x=None, y=None, **kwargs):
"""
return self(kind="bar", x=x, y=y, **kwargs)

def barh(self, x=None, y=None, **kwargs):
"""
Make a horizontal bar plot.
Vertical bar plot.

A horizontal bar plot is a plot that presents quantitative data with
A bar plot is a plot that presents categorical data with
rectangular bars with lengths proportional to the values that they
represent. A bar plot shows comparisons among discrete categories. One
axis of the plot shows the specific categories being compared, and the
other axis represents a measured value.
"""
return self(kind="bar", x=x, y=y, **kwargs)

Parameters
----------
x : label or position, default DataFrame.index
Column to be used for categories.
y : label or position, default All numeric columns in dataframe
Columns to be plotted from the DataFrame.
color : str, int, array_like, or dict, optional
The color for each of the DataFrame's columns. Possible values are:

- A single color string referred to by name, RGB or RGBA code,
for instance 'red' or '#a98d19'.

- A sequence of color strings referred to by name, RGB or RGBA
code, which will be used for each column recursively. For
instance ['green','yellow'] each column's bar will be filled in
green or yellow, alternatively.

- A dict of the form {column name : color}, so that each column will be
colored accordingly. For example, if your columns are called `a` and `b`,
then passing {'a': 'green', 'b': 'red'} will color bars for column `a` in
green and bars for column `b` in red.
**kwargs
Keyword arguments to pass on to :meth:`DataFrame.plot`.

Returns
-------
:class:`matplotlib.axes.Axes` or numpy.ndarray of them

@Appender(
"""
See Also
--------
DataFrame.plot.bar: Vertical bar plot.
Expand Down Expand Up @@ -1152,6 +1099,19 @@ def barh(self, x=None, y=None, **kwargs):
>>> df = pd.DataFrame({'speed': speed,
... 'lifespan': lifespan}, index=index)
>>> ax = df.plot.barh(x='lifespan')
"""
)
@Substitution(kind="bar")
@Appender(_bar_or_line_doc)
def barh(self, x=None, y=None, **kwargs):
"""
Make a horizontal bar plot.

A horizontal bar plot is a plot that presents quantitative data with
rectangular bars with lengths proportional to the values that they
represent. A bar plot shows comparisons among discrete categories. One
axis of the plot shows the specific categories being compared, and the
other axis represents a measured value.
"""
return self(kind="barh", x=x, y=y, **kwargs)

Expand Down
0