def test_init_groupby_data_kwarg(self): from bokeh.sampledata.autompg import autompg as df group = df.groupby(('origin', 'cyl')) ds = ColumnDataSource(data=group) s = group.describe() self.assertTrue(len(ds.column_names)) == 41 self.assertIsInstance(ds.data['origin_cyl'], np.ndarray) for key in s.columns.values: k2 = "_".join(key) self.assertIsInstance(ds.data[k2], np.ndarray) self.assertEquals(list(s[key]), list(ds.data[k2]))
def test_init_groupby_with_None_subindex_name(self): df = pd.DataFrame({"A": [1, 2, 3, 4] * 2, "B": [10, 20, 30, 40] * 2, "C": range(8)}) group = df.groupby(['A', [10, 20, 30, 40] * 2]) ds = ColumnDataSource(data=group) s = group.describe() self.assertTrue(len(ds.column_names)) == 41 self.assertIsInstance(ds.data['index'], np.ndarray) for key in s.columns.values: k2 = "_".join(key) self.assertIsInstance(ds.data[k2], np.ndarray) self.assertEquals(list(s[key]), list(ds.data[k2]))
def test_init_groupby_data_kwarg(self, pd): from bokeh.sampledata.autompg import autompg as df group = df.groupby(by=['origin', 'cyl']) ds = ColumnDataSource(data=group) s = group.describe() assert len(ds.column_names) == 49 assert isinstance(ds.data['origin_cyl'], np.ndarray) for key in s.columns.values: k2 = "_".join(key) assert isinstance(ds.data[k2], np.ndarray) assert list(s[key]) == list(ds.data[k2])
def test_init_groupby_arg(self, pd): from bokeh.sampledata.autompg import autompg as df group = df.groupby(by=['origin', 'cyl']) ds = bms.ColumnDataSource(group) s = group.describe() assert len(ds.column_names) == 49 assert isinstance(ds.data['origin_cyl'], np.ndarray) for key in s.columns.values: k2 = "_".join(key) assert isinstance(ds.data[k2], np.ndarray) assert list(s[key]) == list(ds.data[k2])
def test_data_accepts_groupby_with_None_subindex_name(self, pd) -> None: df = pd.DataFrame({"A": [1, 2, 3, 4] * 2, "B": [10, 20, 30, 40] * 2, "C": range(8)}) group = df.groupby(['A', [10, 20, 30, 40] * 2]) ds = bms.ColumnDataSource() assert ds.data == {} ds.data = group s = group.describe() assert len(ds.column_names) == 17 assert isinstance(ds.data['index'], np.ndarray) for key in s.columns.values: k2 = "_".join(key) assert isinstance(ds.data[k2], np.ndarray) assert list(s[key]) == list(ds.data[k2])
def test_init_groupby_with_None_subindex_name(self): df = pd.DataFrame({ "A": [1, 2, 3, 4] * 2, "B": [10, 20, 30, 40] * 2, "C": range(8) }) group = df.groupby(['A', [10, 20, 30, 40] * 2]) ds = ColumnDataSource(data=group) s = group.describe() self.assertTrue(len(ds.column_names)) == 41 self.assertIsInstance(ds.data['index'], np.ndarray) for key in s.columns.values: k2 = "_".join(key) self.assertIsInstance(ds.data[k2], np.ndarray) self.assertEquals(list(s[key]), list(ds.data[k2]))
""" Link https://bokeh.pydata.org/en/latest/docs/gallery/bar_pandas_groupby_colormapped.html """ #Libraries from bokeh.io import show, output_file from bokeh.palettes import Spectral5 from bokeh.plotting import figure from bokeh.sampledata.autompg import autompg as df from bokeh.transform import factor_cmap output_file("bar_pandas_groupby_colormapped.html") df.cyl = df.cyl.astype(str) group = df.groupby('cyl') cyl_cmap = factor_cmap('cyl', palette=Spectral5, factors=sorted(df.cyl.unique())) p = figure(plot_height=350, x_range=group, title="MPG by #Cylinders", toolbar_location=None, tools="") p.vbar(x='cyl', top='mpg_mean', width=1, source=group,
import pandas as pd # simple examples with inferred meaning # implied index d1 = Donut([2, 4, 5, 2, 8]) # explicit index d2 = Donut(pd.Series([2, 4, 5, 2, 8], index=['a', 'b', 'c', 'd', 'e'])) # given a categorical series of data with no aggregation d3 = Donut(autompg.cyl.astype(str)) # given a categorical series of data with no aggregation d4 = Donut(autompg.groupby('cyl').displ.mean()) # given a categorical series of data with no aggregation d5 = Donut(autompg.groupby(['cyl', 'origin']).displ.mean(), hover_text='mean') # no values specified d6 = Donut(autompg, label='cyl', agg='count') # explicit examples d7 = Donut(autompg, label='cyl', values='displ', agg='mean') # nested donut chart for the provided labels, with colors assigned # by the first level d8 = Donut(autompg, label=['cyl', 'origin'],
from bokeh.io import show, output_file from bokeh.palettes import Spectral5 from bokeh.plotting import figure from bokeh.sampledata.autompg import autompg as df from bokeh.transform import factor_cmap output_file("bar_pandas_groupby_colormapped.html") df.cyl = df.cyl.astype(str) group = df.groupby('cyl') cyl_cmap = factor_cmap('cyl', palette=Spectral5, factors=sorted(df.cyl.unique())) p = figure(plot_height=350, x_range=group, title="MPG by # Cylinders", toolbar_location=None, tools="") p.vbar(x='cyl', top='mpg_mean', width=1, source=group, line_color=cyl_cmap, fill_color=cyl_cmap) p.y_range.start = 0 p.xgrid.grid_line_color = None p.xaxis.axis_label = "some stuff" p.xaxis.major_label_orientation = 1.2 p.outline_line_color = None show(p)
import pandas as pd # simple examples with inferred meaning # implied index d1 = Donut([2, 4, 5, 2, 8]) # explicit index d2 = Donut(pd.Series([2, 4, 5, 2, 8], index=['a', 'b', 'c', 'd', 'e'])) # given a categorical series of data with no aggregation d3 = Donut(autompg.cyl.astype(str)) # given a categorical series of data with no aggregation d4 = Donut(autompg.groupby('cyl').displ.mean()) # given a categorical series of data with no aggregation d5 = Donut(autompg.groupby(['cyl', 'origin']).displ.mean(), hover_text='mean') # no values specified d6 = Donut(autompg, label='cyl', agg='count') # explicit examples d7 = Donut(autompg, label='cyl', values='displ', agg='mean') # nested donut chart for the provided labels, with colors assigned # by the first level d8 = Donut(autompg, label=['cyl', 'origin'], values='displ', agg='mean') # show altering the spacing in levels
import pandas as pd # simple examples with inferred meaning # implied index d1 = Donut([2, 4, 5, 2, 8]) # explicit index d2 = Donut(pd.Series([2, 4, 5, 2, 8], index=['a', 'b', 'c', 'd', 'e'])) # given a categorical series of data with no aggregation d3 = Donut(autompg.cyl.astype(str)) # given a categorical series of data with no aggregation d4 = Donut(autompg.groupby('cyl').displ.mean()) # given a categorical series of data with no aggregation d5 = Donut(autompg.groupby(['cyl', 'origin']).displ.mean(), hover_text='mean') # no values specified d6 = Donut(autompg, label='cyl', agg='count') # explicit examples d7 = Donut(autompg, label='cyl', values='displ', agg='mean') # nested donut chart for the provided labels, with colors assigned # by the first level d8 = Donut(autompg, label=['cyl', 'origin'],
p.xgrid.grid_line_color = None show(p) ## Pandas from bokeh.io import output_file, show from bokeh.models import ColumnDataSource from bokeh.palettes import Spectral5 from bokeh.plotting import figure from bokeh.sampledata.autompg import autompg as df from bokeh.transform import factor_cmap output_file("groupby.html") df.cyl = df.cyl.astype(str) group = df.groupby('cyl') source = ColumnDataSource(group) cyl_cmap = factor_cmap('cyl', palette=Spectral5, factors=sorted(df.cyl.unique())) p = figure(plot_height=350, x_range=group, title="MPG by # Cylinders", toolbar_location=None, tools="") p.vbar(x='cyl', top='mpg_mean', width=1, source=source, line_color=cyl_cmap, fill_color=cyl_cmap) p.y_range.start = 0 p.xgrid.grid_line_color = None p.xaxis.axis_label = "some stuff" p.xaxis.major_label_orientation = 1.2
return y df["more_labels"] = df["cyl"].map(new_labels) # Specifying your labels p1 = Bar(df, label='labels', values='mpg', title="Total MPG by CYL, remapped labels, p1", width=400, height=400, legend="top_right") p2 = Bar(df, label=['cyl', 'more_labels'], values='mpg', title="Total MPG by CYL, multiple labels, p2", width=400, height=400, legend="top_right") # Plot with "intermediate-level" bokeh.plotting interface new_df = DataFrame(df.groupby(['cyl'])['mpg'].sum()) factors = ["three", "four", "five", "six", "eight"] ordinate = new_df['mpg'].tolist() mpg = [x * 0.5 for x in ordinate] p3 = figure(x_range=factors, width=400, height=400, title="Total MPG by CYL, using 'rect' instead of 'bar', p3") p3.rect(factors, y=mpg, width=0.75, height=ordinate) p3.y_range = Range1d(0, 6000) p3.xaxis.axis_label = "x axis name" p3.yaxis.axis_label = "Sum(Mpg)" # With HoverTool, using 'quad' instead of 'rect' top = [int(x) for x in ordinate] bottom = [0] * len(top)
import pandas as pd # simple examples with inferred meaning # implied index d1 = Donut([2, 4, 5, 2, 8]) # explicit index d2 = Donut(pd.Series([2, 4, 5, 2, 8], index=["a", "b", "c", "d", "e"])) # given a categorical series of data with no aggregation d3 = Donut(autompg.cyl.astype(str)) # given a categorical series of data with no aggregation d4 = Donut(autompg.groupby("cyl").displ.mean()) # given a categorical series of data with no aggregation d5 = Donut(autompg.groupby(["cyl", "origin"]).displ.mean(), hover_text="mean") # no values specified d6 = Donut(autompg, label="cyl", agg="count") # explicit examples d7 = Donut(autompg, label="cyl", values="displ", agg="mean") # nested donut chart for the provided labels, with colors assigned # by the first level d8 = Donut(autompg, label=["cyl", "origin"], values="displ", agg="mean") # show altering the spacing in levels