From ac5a96f44f2d8f9a9fc626056f34d54527a4955e Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Thu, 16 Jan 2020 11:21:08 +0000 Subject: [PATCH] Rebase, add docstrings --- pandas/plotting/_core.py | 73 +++++++++++++++++++++++++++++ pandas/plotting/_matplotlib/core.py | 1 + 2 files changed, 74 insertions(+) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index dd907457f7c323..68156245f47018 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -864,6 +864,21 @@ def line(self, x=None, y=None, **kwargs): 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 of each line for each row. 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 line for each row recursively. For + instance ['green','yellow'] all lines for each row will be filled in green + or yellow, alternatively. + + - A dict of the form {column name : color}, so that each row's lines will be + colored accordingly. For example, if your columns are called `a` and `b`, + then passing {'a': 'green', 'b': 'red'} will color the lines for column + `a` in green and lines for column `b` in red. **kwargs Keyword arguments to pass on to :meth:`DataFrame.plot`. @@ -906,6 +921,16 @@ def line(self, x=None, y=None, **kwargs): >>> type(axes) + .. plot:: + :context: close-figs + + Let's repeat the same example, but specifying colors for + each column (in this case, for each animal). + + >>> axes = df.plot.line( + ... subplots=True, color={"pig": "pink", "horse": "#742802"} + ... ) + .. plot:: :context: close-figs @@ -934,6 +959,21 @@ def bar(self, x=None, y=None, **kwargs): 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 of each bar for each row. 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 bar for each row recursively. For + instance ['green','yellow'] all bars for each row will be filled in green + or yellow, alternatively. + + - A dict of the form {column name : color}, so that each row's bars 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`. @@ -985,6 +1025,17 @@ def bar(self, x=None, y=None, **kwargs): >>> axes = df.plot.bar(rot=0, subplots=True) >>> axes[1].legend(loc=2) # doctest: +SKIP + If we don't like the default colours, we can specify how we'd + like each column to be colored. + + .. plot:: + :context: close-figs + + >>> axes = df.plot.bar( + ... rot=0, subplots=True, color={"speed": "red", "lifespan": "green"} + ... ) + >>> axes[1].legend(loc=2) # doctest: +SKIP + Plot a single column. .. plot:: @@ -1017,6 +1068,21 @@ def barh(self, x=None, y=None, **kwargs): 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 of each bar for each row. 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 bar for each row recursively. For + instance ['green','yellow'] all bars for each row will be filled in green + or yellow, alternatively. + + - A dict of the form {column name : color}, so that each row's bars 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`. @@ -1053,6 +1119,13 @@ def barh(self, x=None, y=None, **kwargs): ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.barh() + We can specify colors for each column + + .. plot:: + :context: close-figs + + >>> ax = df.plot.barh(color={"speed": "red", "lifespan": "green"}) + Plot a column of the DataFrame to a horizontal bar plot .. plot:: diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 6ab7342978ab5b..de09460bb833d3 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1344,6 +1344,7 @@ def _make_plot(self): pos_prior = neg_prior = np.zeros(len(self.data)) K = self.nseries + for i, (label, y) in enumerate(self._iter_data(fillna=0)): ax = self._get_ax(i) kwds = self.kwds.copy()