diff --git a/doc/source/user_guide/visualization.rst b/doc/source/user_guide/visualization.rst index ab4893537f834..4a4c3624aa614 100644 --- a/doc/source/user_guide/visualization.rst +++ b/doc/source/user_guide/visualization.rst @@ -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: @@ -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}``. diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 8b2ac31532344..524a26b2d3fa6 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -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)