def test_discrete_bins(self, long_df): h = Hist(discrete=True) x = long_df["x"].astype(int) bin_kws = h._define_bin_params(long_df.assign(x=x), "x", "continuous") assert bin_kws["range"] == (x.min() - .5, x.max() + .5) assert bin_kws["bins"] == (x.max() - x.min() + 1)
def test_int_bins(self, long_df): n = 24 h = Hist(bins=n) bin_kws = h._define_bin_params(long_df, "x", "continuous") assert bin_kws["range"] == (long_df["x"].min(), long_df["x"].max()) assert bin_kws["bins"] == n
def test_discrete_bins_from_nominal_scale(self, rng): h = Hist() x = rng.randint(0, 5, 10) df = pd.DataFrame({"x": x}) bin_kws = h._define_bin_params(df, "x", "nominal") assert bin_kws["range"] == (x.min() - .5, x.max() + .5) assert bin_kws["bins"] == (x.max() - x.min() + 1)
def test_binwidth(self, long_df): binwidth = .5 h = Hist(binwidth=binwidth) bin_kws = h._define_bin_params(long_df, "x", "continuous") n_bins = bin_kws["bins"] left, right = bin_kws["range"] assert (right - left) / n_bins == pytest.approx(binwidth)
def test_binrange(self, long_df): binrange = (-4, 4) h = Hist(binrange=binrange) bin_kws = h._define_bin_params(long_df, "x", "continuous") assert bin_kws["range"] == binrange
def test_array_bins(self, long_df): bins = [-3, -2, 1, 2, 3] h = Hist(bins=bins) bin_kws = h._define_bin_params(long_df, "x", "continuous") assert_array_equal(bin_kws["bins"], bins)
def test_string_bins(self, long_df): h = Hist(bins="sqrt") bin_kws = h._define_bin_params(long_df, "x", "continuous") assert bin_kws["range"] == (long_df["x"].min(), long_df["x"].max()) assert bin_kws["bins"] == int(np.sqrt(len(long_df)))