Skip to content

Commit e3babf2

Browse files
charlesdong1991CGe0516
authored andcommitted
DOC: Add user_guide examples and docstring example for df.plot.box and df.plot.hist (pandas-dev#42520)
1 parent 5e58c6e commit e3babf2

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

doc/source/user_guide/visualization.rst

+54
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,34 @@ The ``by`` keyword can be specified to plot grouped histograms:
316316
@savefig grouped_hist.png
317317
data.hist(by=np.random.randint(0, 4, 1000), figsize=(6, 4));
318318
319+
.. ipython:: python
320+
:suppress:
321+
322+
plt.close("all")
323+
np.random.seed(123456)
324+
325+
In addition, the ``by`` keyword can also be specified in :meth:`DataFrame.plot.hist`.
326+
327+
.. versionchanged:: 1.4.0
328+
329+
.. ipython:: python
330+
331+
data = pd.DataFrame(
332+
{
333+
"a": np.random.choice(["x", "y", "z"], 1000),
334+
"b": np.random.choice(["e", "f", "g"], 1000),
335+
"c": np.random.randn(1000),
336+
"d": np.random.randn(1000) - 1,
337+
},
338+
)
339+
340+
@savefig grouped_hist_by.png
341+
data.plot.hist(by=["a", "b"], figsize=(10, 5));
342+
343+
.. ipython:: python
344+
:suppress:
345+
346+
plt.close("all")
319347
320348
.. _visualization.box:
321349

@@ -448,6 +476,32 @@ columns:
448476
449477
plt.close("all")
450478
479+
You could also create groupings with :meth:`DataFrame.plot.box`, for instance:
480+
481+
.. versionchanged:: 1.4.0
482+
483+
.. ipython:: python
484+
:suppress:
485+
486+
plt.close("all")
487+
np.random.seed(123456)
488+
489+
.. ipython:: python
490+
:okwarning:
491+
492+
df = pd.DataFrame(np.random.rand(10, 3), columns=["Col1", "Col2", "Col3"])
493+
df["X"] = pd.Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
494+
495+
plt.figure();
496+
497+
@savefig box_plot_ex4.png
498+
bp = df.plot.box(column=["Col1", "Col2"], by="X")
499+
500+
.. ipython:: python
501+
:suppress:
502+
503+
plt.close("all")
504+
451505
.. _visualization.box.return:
452506

453507
In ``boxplot``, the return type can be controlled by the ``return_type``, keyword. The valid choices are ``{"axes", "dict", "both", None}``.

pandas/plotting/_core.py

+12
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,18 @@ def box(self, by=None, **kwargs):
12671267
>>> data = np.random.randn(25, 4)
12681268
>>> df = pd.DataFrame(data, columns=list('ABCD'))
12691269
>>> ax = df.plot.box()
1270+
1271+
You can also generate groupings if you specify the `by` parameter (which
1272+
can take a column name, or a list or tuple of column names):
1273+
1274+
.. versionchanged:: 1.4.0
1275+
1276+
.. plot::
1277+
:context: close-figs
1278+
1279+
>>> age_list = [8, 10, 12, 14, 72, 74, 76, 78, 20, 25, 30, 35, 60, 85]
1280+
>>> df = pd.DataFrame({"gender": list("MMMMMMMMFFFFFF"), "age": age_list})
1281+
>>> ax = df.plot.box(column="age", by="gender", figsize=(10, 8))
12701282
"""
12711283
return self(kind="box", by=by, **kwargs)
12721284

0 commit comments

Comments
 (0)