123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- # coding: utf-8
- """ Test cases for GroupBy.plot """
- import numpy as np
- import pandas.util._test_decorators as td
- from pandas import DataFrame, Series
- import pandas._testing as tm
- from pandas.tests.plotting.common import TestPlotBase
- @td.skip_if_no_mpl
- class TestDataFrameGroupByPlots(TestPlotBase):
- def test_series_groupby_plotting_nominally_works(self):
- n = 10
- weight = Series(np.random.normal(166, 20, size=n))
- height = Series(np.random.normal(60, 10, size=n))
- with tm.RNGContext(42):
- gender = np.random.choice(["male", "female"], size=n)
- weight.groupby(gender).plot()
- tm.close()
- height.groupby(gender).hist()
- tm.close()
- # Regression test for GH8733
- height.groupby(gender).plot(alpha=0.5)
- tm.close()
- def test_plotting_with_float_index_works(self):
- # GH 7025
- df = DataFrame(
- {"def": [1, 1, 1, 2, 2, 2, 3, 3, 3], "val": np.random.randn(9)},
- index=[1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0],
- )
- df.groupby("def")["val"].plot()
- tm.close()
- df.groupby("def")["val"].apply(lambda x: x.plot())
- tm.close()
- def test_hist_single_row(self):
- # GH10214
- bins = np.arange(80, 100 + 2, 1)
- df = DataFrame({"Name": ["AAA", "BBB"], "ByCol": [1, 2], "Mark": [85, 89]})
- df["Mark"].hist(by=df["ByCol"], bins=bins)
- df = DataFrame({"Name": ["AAA"], "ByCol": [1], "Mark": [85]})
- df["Mark"].hist(by=df["ByCol"], bins=bins)
- def test_plot_submethod_works(self):
- df = DataFrame({"x": [1, 2, 3, 4, 5], "y": [1, 2, 3, 2, 1], "z": list("ababa")})
- df.groupby("z").plot.scatter("x", "y")
- tm.close()
- df.groupby("z")["x"].plot.line()
- tm.close()
- def test_plot_kwargs(self):
- df = DataFrame({"x": [1, 2, 3, 4, 5], "y": [1, 2, 3, 2, 1], "z": list("ababa")})
- res = df.groupby("z").plot(kind="scatter", x="x", y="y")
- # check that a scatter plot is effectively plotted: the axes should
- # contain a PathCollection from the scatter plot (GH11805)
- assert len(res["a"].collections) == 1
- res = df.groupby("z").plot.scatter(x="x", y="y")
- assert len(res["a"].collections) == 1
|