def test_rng_context(): import numpy as np expected0 = 1.764052345967664 expected1 = 1.6243453636632417 with tm.RNGContext(0): with tm.RNGContext(1): assert np.random.randn() == expected1 assert np.random.randn() == expected0
def test_scatter_matrix_axis(self): from pandas.plotting._matplotlib.compat import _mpl_ge_3_0_0 scatter_matrix = plotting.scatter_matrix with tm.RNGContext(42): df = DataFrame(randn(100, 3)) # we are plotting multiples on a sub-plot with tm.assert_produces_warning( UserWarning, raise_on_extra_warnings=_mpl_ge_3_0_0() ): axes = _check_plot_works( scatter_matrix, filterwarnings="always", frame=df, range_padding=0.1 ) axes0_labels = axes[0][0].yaxis.get_majorticklabels() # GH 5662 expected = ["-2", "0", "2"] self._check_text_labels(axes0_labels, expected) self._check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0) df[0] = (df[0] - 2) / 3 # we are plotting multiples on a sub-plot with tm.assert_produces_warning(UserWarning): axes = _check_plot_works( scatter_matrix, filterwarnings="always", frame=df, range_padding=0.1 ) axes0_labels = axes[0][0].yaxis.get_majorticklabels() expected = ["-1.0", "-0.5", "0.0"] self._check_text_labels(axes0_labels, expected) self._check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0)
def hist_df(): n = 100 with tm.RNGContext(42): gender = np.random.choice(["Male", "Female"], size=n) classroom = np.random.choice(["A", "B", "C"], size=n) hist_df = DataFrame({ "gender": gender, "classroom": classroom, "height": np.random.normal(66, 4, size=n), "weight": np.random.normal(161, 32, size=n), "category": np.random.randint(4, size=n), "datetime": to_datetime( np.random.randint( 812419200000000000, 819331200000000000, size=n, dtype=np.int64, )), }) return hist_df
def setup_method(self, method): import matplotlib as mpl from pandas.plotting._matplotlib import compat self.compat = compat mpl.rcdefaults() self.start_date_to_int64 = 812419200000000000 self.end_date_to_int64 = 819331200000000000 self.mpl_ge_2_2_3 = compat.mpl_ge_2_2_3() self.mpl_ge_3_0_0 = compat.mpl_ge_3_0_0() self.mpl_ge_3_1_0 = compat.mpl_ge_3_1_0() self.mpl_ge_3_2_0 = compat.mpl_ge_3_2_0() self.bp_n_objects = 7 self.polycollection_factor = 2 self.default_figsize = (6.4, 4.8) self.default_tick_position = "left" n = 100 with tm.RNGContext(42): gender = np.random.choice(["Male", "Female"], size=n) classroom = np.random.choice(["A", "B", "C"], size=n) self.hist_df = DataFrame({ "gender": gender, "classroom": classroom, "height": np.random.normal(66, 4, size=n), "weight": np.random.normal(161, 32, size=n), "category": np.random.randint(4, size=n), "datetime": to_datetime( np.random.randint( self.start_date_to_int64, self.end_date_to_int64, size=n, dtype=np.int64, )), }) self.tdf = tm.makeTimeDataFrame() self.hexbin_df = DataFrame({ "A": np.random.uniform(size=20), "B": np.random.uniform(size=20), "C": np.arange(20) + np.random.uniform(size=20), })
def test_grouped_hist_legacy2(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_int = np.random.choice([0, 1], size=n) df_int = DataFrame({"height": height, "weight": weight, "gender": gender_int}) gb = df_int.groupby("gender") axes = gb.hist() assert len(axes) == 2 assert len(self.plt.get_fignums()) == 2 tm.close()
def test_hist_df_with_nonnumerics(self): # GH 9853 with tm.RNGContext(1): df = DataFrame(np.random.randn(10, 4), columns=["A", "B", "C", "D"]) df["E"] = ["x", "y"] * 5 _, ax = self.plt.subplots() ax = df.plot.hist(bins=5, ax=ax) assert len(ax.patches) == 20 _, ax = self.plt.subplots() ax = df.plot.hist(ax=ax) # bins=10 assert len(ax.patches) == 40
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_scatter_matrix_axis(self, pass_axis): scatter_matrix = plotting.scatter_matrix ax = None if pass_axis: _, ax = self.plt.subplots(3, 3) with tm.RNGContext(42): df = DataFrame(np.random.randn(100, 3)) # we are plotting multiples on a sub-plot with tm.assert_produces_warning(UserWarning, raise_on_extra_warnings=True): axes = _check_plot_works( scatter_matrix, filterwarnings="always", frame=df, range_padding=0.1, ax=ax, ) axes0_labels = axes[0][0].yaxis.get_majorticklabels() # GH 5662 expected = ["-2", "0", "2"] self._check_text_labels(axes0_labels, expected) self._check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0) df[0] = (df[0] - 2) / 3 # we are plotting multiples on a sub-plot with tm.assert_produces_warning(UserWarning): axes = _check_plot_works( scatter_matrix, filterwarnings="always", frame=df, range_padding=0.1, ax=ax, ) axes0_labels = axes[0][0].yaxis.get_majorticklabels() expected = ["-1.0", "-0.5", "0.0"] self._check_text_labels(axes0_labels, expected) self._check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0)
def test_grouped_plot_fignums(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) df = DataFrame({"height": height, "weight": weight, "gender": gender}) gb = df.groupby("gender") res = gb.plot() assert len(self.plt.get_fignums()) == 2 assert len(res) == 2 tm.close() res = gb.boxplot(return_type="axes") assert len(self.plt.get_fignums()) == 1 assert len(res) == 2 tm.close() # now works with GH 5610 as gender is excluded res = df.groupby("gender").hist() tm.close()