Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 54 additions & 0 deletions doc/source/user_guide/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,34 @@ The ``by`` keyword can be specified to plot grouped histograms:
@savefig grouped_hist.png
data.hist(by=np.random.randint(0, 4, 1000), figsize=(6, 4));

.. ipython:: python
:suppress:

plt.close("all")
np.random.seed(123456)

In addition, the ``by`` keyword can also be specified in :meth:`DataFrame.plot.hist`.

.. versionchanged:: 1.4.0

.. ipython:: python

data = pd.DataFrame(
{
"a": np.random.choice(["x", "y", "z"], 1000),
"b": np.random.choice(["e", "f", "g"], 1000),
"c": np.random.randn(1000),
"d": np.random.randn(1000) - 1,
},
)

@savefig grouped_hist_by.png
data.plot.hist(by=["a", "b"], figsize=(10, 5));

.. ipython:: python
:suppress:

plt.close("all")

.. _visualization.box:

Expand Down Expand Up @@ -448,6 +476,32 @@ columns:

plt.close("all")

You could also create groupings with :meth:`DataFrame.plot.box`, for instance:

.. versionchanged:: 1.4.0

.. ipython:: python
:suppress:

plt.close("all")
np.random.seed(123456)

.. ipython:: python
:okwarning:

df = pd.DataFrame(np.random.rand(10, 3), columns=["Col1", "Col2", "Col3"])
df["X"] = pd.Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])

plt.figure();

@savefig box_plot_ex4.png
bp = df.plot.box(column=["Col1", "Col2"], by="X")

.. ipython:: python
:suppress:

plt.close("all")

.. _visualization.box.return:

In ``boxplot``, the return type can be controlled by the ``return_type``, keyword. The valid choices are ``{"axes", "dict", "both", None}``.
Expand Down
12 changes: 12 additions & 0 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,18 @@ def box(self, by=None, **kwargs):
>>> data = np.random.randn(25, 4)
>>> df = pd.DataFrame(data, columns=list('ABCD'))
>>> ax = df.plot.box()

You can also generate groupings if you specify the `by` parameter (which
can take a column name, or a list or tuple of column names):

.. versionchanged:: 1.4.0

.. plot::
:context: close-figs

>>> age_list = [8, 10, 12, 14, 72, 74, 76, 78, 20, 25, 30, 35, 60, 85]
>>> df = pd.DataFrame({"gender": list("MMMMMMMMFFFFFF"), "age": age_list})
>>> ax = df.plot.box(column="age", by="gender", figsize=(10, 8))
"""
return self(kind="box", by=by, **kwargs)

Expand Down