def generate_chart(year): data = get_data(year) barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Team', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, legend='top_right', plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + str(year) + " Total Scores by Place" barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._xaxis.major_label_text_font_size = '0.6em' barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None return barchart
def plotAverage(self): bar = Bar(\ self.__table, values = blend('average', name = 'average', labels_name = 'average'),\ label = cat(columns = 'country', sort = False),\ stack = cat(columns = 'average'),\ agg = 'mean',\ title = 'Average of CO2 emissions in European Union',\ legend = False,\ tooltips = [('Average', '@average{1.11}' + ' g/km')]) return bar
def plotAverage(self): bar = Bar(\ self.__table, values = blend('average', name = 'average', labels_name = 'average'),\ label = cat(columns = 'fuel'),\ stack = cat(columns = 'average'),\ agg = 'mean',\ title = 'Average of CO2 emissions (by fuel type) in ' + self.__title,\ legend = False,\ tooltips = [('Average', '@average{1.11}' + ' g/km')]) return bar
def plot(self): bar = Bar(\ self.__fuel_table, values = blend('percentage', name = 'percentage', labels_name = 'percentage'),\ label = cat(columns = 'fuel'),\ stack = cat(columns = 'percentage'),\ agg = 'mean',\ title = self.__title,\ legend = False,\ tooltips = [('Percentage', '@percentage{0.00%}')]) bar._yaxis.formatter = NumeralTickFormatter(format = '0%') return bar
def plotTotal(self): bar = Bar(\ self.__table, values = blend('total', name = 'total', labels_name = 'total'),\ label = cat(columns = 'fuel'),\ stack = cat(columns = 'total'),\ agg = 'mean',\ title = 'Total of CO2 emissions (by fuel type) in ' + self.__title,\ legend = False,\ tooltips = [('Total', '@total{1,1}' + ' g/km')]) bar._yaxis.formatter = NumeralTickFormatter(format = '0,0' + ' g/km') return bar
def plot(self): bar = Bar(\ self.__table, values = blend('registrations', name = 'registrations', labels_name = 'registrations'),\ label = cat(columns = 'country', sort = False),\ stack = cat(columns = 'registrations'),\ agg = 'mean',\ title = 'Total of new registrations in European Union',\ legend = False,\ tooltips = [('Registrations', '@registrations{1,1}')]) bar._yaxis.formatter = NumeralTickFormatter(format='0,0') return bar
def generate_chart(team): data = generate_data(team) selectable_years = list(map(str, data.index.unique())) # Generate the chart UNFORTUNATELY using the high level plots. Streaming # a bunch of quads resulted in lots of graphics corruption when switching # teams. I would rather have the plots work all the time but really slow # then have the plot show the wrong information. barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Year', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan - " + team barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None # Hacky tooltips for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['Year'] place = data['Place'].loc[data['Year'] == year] score = data['Total Score'].loc[data['Year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[("Year", '@Year'), ("Selection", '@event'), ("Event Score", '@height'), ("Total Score", '%.2f' % score.values[0]), ("Overall Place", '%d' % place.values[0])]) barchart.add_tools(hover) return barchart
def aggregate(transaction_history): net = dict() buy = dict() sell = dict() interest = dict() dividend = dict() historical = dict() for t in transaction_history: quarter = "%s-Q%i" % (t.date.year, (t.date.month-1)//3+1) net[quarter] = float(handleNull(net.get(quarter))) + float(t.cashflow) if t.kind == Transaction.BUY: buy[quarter] = float(handleNull(buy.get(quarter))) + float(t.cashflow) elif t.kind == Transaction.SELL: sell[quarter] = float(handleNull(sell.get(quarter))) + float(t.cashflow) elif t.kind == Transaction.INTEREST: interest[quarter] = float(handleNull(interest.get(quarter))) + float(t.cashflow) elif t.kind == Transaction.DIVIDEND: dividend[quarter] = float(handleNull(dividend.get(quarter))) + float(t.cashflow) elif t.kind == Transaction.HISTORICAL or t.kind == Transaction.CURRENT: historical[quarter] = float(handleNull(historical.get(quarter))) + float(t.cashflow) net = addMissingQuarters(net) buy = addMissingQuarters(buy) sell = addMissingQuarters(sell) interest = addMissingQuarters(interest) dividend = addMissingQuarters(dividend) historical = addMissingQuarters(historical) d = {'net': pd.Series(net), 'buy': pd.Series(buy), 'sell': pd.Series(sell), 'interest':pd.Series(interest), 'dividend':pd.Series(dividend), 'historical':pd.Series(historical)} df = pd.DataFrame(d) df['label']=df.index p1 = Bar(df, values = blend('buy','sell','interest','dividend','historical',name='cashflow', labels_name='cf'), label=cat(columns='label',sort=False), stack=cat(columns='cf',sort=False)) p2 = Bar(df, values = blend('net'), label='label') output_file("test.html") show(vplot(p1, p2))
def box_riga_price_mean_by_district(): form = NoOutliersForm(request.args, csrf_enabled=False) outliers = not ('submit' in request.args and form.validate() and form.no_outliers.data) ds = g.ds[g.ds.city == u'Rīga'] stats = _base_stats(ds) df = odo(ds, pd.DataFrame) ordered = {v: k for k, v in list(enumerate(stats.sort_values(by='avg_price_m2')['district']))} df['pos_mean'] = pd.Series(ordered[d] for d in df['district']) df['price_m2'] = df['price'] / df['area'] df = _fix_unicode_names(df, 'district') df.sort(['pos_mean'], ascending=[True], inplace=True) p = BoxPlot(df, values='price_m2', label=cat(columns=['district'], sort=False), title="Price by m2 vs District (ordered by mean price of m2)", xlabel="District", ylabel="m2 price (EUR)", outliers=outliers, legend=False ) return render(p, dict(form=form))
def create_figure_overall_ranks(srcDF, season_num): overallRanksDF = get_overall_ranks_df(season_num, srcDF) overallRankspDF = overallRanksDF.toPandas() clr = get_color_list('Viridis', overallRanksDF.count()) # Brewing color hex values for each tuple('team') figureOverallRanks = Bar(overallRankspDF, values="Wins", color="Team",\ palette=clr, label=cat(columns="Team", sort=False),\ xgrid=True, xlabel="Team", ylabel="Wins",\ title="Overall Standings " + str(season_num),\ legend='top_right', plot_width=950, bar_width=0.6) # generating bar chart return figureOverallRanks
def plot_stacked_bar_chart(df): bar = Bar(df, values=blend('Murder', 'Rape', 'Robbery', 'Aggravated Assault', 'Burglary', 'Larceny Theft', 'Motor Vehicle Theft', name='Crime', labels_name='crime'), label=cat(columns='State', sort=False), stack=cat(columns='crime', sort=False), legend='top_right', title="Crime per state", width=1000, height=500, tooltips=[('crime', '@crime'), ('state', '@State')]) #output_file("stacked_bar.html", title="stacked_bar.py example") show(bar)
def stack_bokeh(infile): with open(infile, "r") as file: data = pd.read_csv(file) print(data) p = Bar(data, values=blend('locus dropout', 'allelic dropout', 'biallelic coverage', name='samp', labels_name="samps"), label=cat(columns='Sample', sort=False), stack=cat(columns='samps', sort=False), color=color(columns="samps", palette=["green", "red", "black"], sort=False), legend='top_right', title="sam") output_file("stacked_bar.html") curdoc().add_root(p) show(p)
def generate_chart(year): data = get_data(year) barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Team', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, legend='top_right', plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + str( year) + " Total Scores by Place" barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._xaxis.major_label_text_font_size = '0.6em' barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None return barchart
def get_concentration_bar_chart(df): """Creates bar chart visualizing portfolio concentration by position""" df = df[["Symbol", "Concentration"]].sort_values("Concentration", ascending=False) chart = Bar(df, label=cat(columns='Symbol', sort=False), values='Concentration', title='Portfolio Concentration By Position', ylabel='Concentration (%)', plot_width=1200, plot_height=400, legend=False, color='#4285f4') return file_html(chart, INLINE)
def bar(self, hot_users, top=5, width=800, height=300, plot=False): hot_users = hot_users.sort_values("hotness", ascending=False) bar = Bar(data=hot_users.head(top), values='hotness', label=cat(columns='user', sort=False), color="blue", width=width, height=height, legend=False) if plot: show(bar) output_file("temp/bar.html") script, div = components(bar) return {"script": script, "div": div}
def generate_chart(): data = get_data() # Bokeh doesn't let me control the order of the grouping! This is # frustrating since it will be different on every server launch barchart = Bar(data, label='year', values='count', group=cat(columns='size', ascending=True, sort=True), color=color(columns='size', palette=Spectral4), legend='top_left', xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan Engine Cylinders" barchart._xaxis.axis_label = "Year" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Frequency" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None hover = HoverTool(tooltips=[("Engine", '@size'), ("# Teams", '@height')]) barchart.add_tools(hover) return barchart
def plot_opt(name): with open('{}.txt'.format(name), 'r') as f: data = f.read() N = int(re.findall('\[I(\d*) \d*\]\n.* .* .*', data)[0]) B = int(re.findall('\[I\d* (\d*)\]\n.* .* .*', data)[0]) TP = ['with', 'without'] T1 = [float(x) for x in re.findall('\[I\d* \d*\]\n(.*) .* .*', data)] T2 = [float(x) for x in re.findall('\[I\d* \d*\]\n.* (.*) .*', data)] T3 = [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* (.*)', data)] T1 += [ float(x) for x in re.findall('\[I\d* \d*\]\n.* .* .*\n(.*) .* .*', data) ] T2 += [ float(x) for x in re.findall('\[I\d* \d*\]\n.* .* .*\n.* (.*) .*', data) ] T3 += [ float(x) for x in re.findall('\[I\d* \d*\]\n.* .* .*\n.* .* (.*)', data) ] df = pd.DataFrame({ 'type': TP, 'computation': T1, 'communication': T2, 'memcpy': T3 }) bar = Bar(df, label=['type'], values=blend('computation', 'communication', 'memcpy', name='times', labels_name='time'), xlabel='comparison', ylabel='total time(s)', stack=cat(columns=['time'], sort=False), title='{}'.format(name)) output_file('{}.html'.format(name), title='N={}, B={}'.format(N, B)) save(bar)
def _render_bar_riga_any_by_district(key, **kwargs): ds = g.ds[(g.ds.city == u'Rīga')] form = FlatsNoNewProjectsForm(request.args, csrf_enabled=False) if 'submit' in request.args and form.validate() and form.no_new.data: ds = ds[ds.project != u'Jaun.'] df = _base_stats(ds) df = _fix_unicode_names(df, 'district') df.sort([key, 'district'], ascending=[True, False], inplace=True) p = Bar(df, values=key, label=cat(columns=['district'], sort=False), xlabel="District", legend=False, **kwargs) set_numerical_axis(p) return render(p, dict(form=form))
def plot_weak(name): with open('{}.tot'.format(name), 'r') as f: data = f.read() Ns = [int(x) for x in re.findall('\[I(\d*) \d*\]\n.* .* .*', data)] Bs = [int(x) for x in re.findall('\[I\d* (\d*)\]\n.* .* .*', data)] T1s = [float(x) for x in re.findall('\[I\d* \d*\]\n(.*) .* .*', data)] T2s = [float(x) for x in re.findall('\[I\d* \d*\]\n.* (.*) .*', data)] T3s = [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* (.*)', data)] """ if 'cuda' not in name: Ns += Ns Bs += Bs T1s += [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* .*\n(.*) .* .*', data)] T2s += [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* .*\n.* (.*) .*', data)] T3s += [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* .*\n.* .* (.*)', data)] """ df = pd.DataFrame({ 'N': Ns, 'B': Bs, 'computation': T1s, 'communication': T2s, 'memcpy': T3s }) bar = Bar(df, label=['N', 'B'], values=blend('computation', 'communication', 'memcpy', name='times', labels_name='time'), xlabel='#node', ylabel='total time(s)', stack=cat(columns=['time'], sort=False), title='Weak Scalability') output_file('weak_{}.html'.format(name)) save(bar)
def plot_read_dist(rinfo): df = pd.read_table(rinfo) data = df[['length', 'mapper', 'count']].groupby(['length', 'mapper']).count() data = data.apply(lambda s: s / data.sum() * 100, axis=1).reset_index() p = Bar(data, plot_width=500, plot_height=400, label='length', values='count', agg='sum', stack=cat(columns='mapper', sort=False), legend='top_right', color=color(columns='mapper', palette=["#f98283", "#a4a4a4"], sort=False), xlabel='read length (nt)', ylabel='proportion (%)', ygrid=False, tooltips=[('length', '@length'), ('mapper', '@mapper'), ('percent', '@height')]) p.toolbar.logo = None p.outline_line_color = None return p
from bokeh.charts import Bar, output_file, show from bokeh.charts.operations import blend from bokeh.charts.attributes import cat, color from bokeh.charts.utils import df_from_json from bokeh.sampledata.olympics2014 import data # utilize utility to make it easy to get json/dict data converted to a dataframe df = df_from_json(data) # filter by countries with at least one medal and sort by total medals df = df[df['total'] > 0] df = df.sort("total", ascending=False) bar = Bar(df, values=blend('bronze', 'silver', 'gold', name='medals', labels_name='medal'), label=cat(columns='abbr', sort=False), stack=cat(columns='medal', sort=False), color=color(columns='medal', palette=['SaddleBrown', 'Silver', 'Goldenrod'], sort=False), legend='top_right', title="Medals per Country, Sorted by Total Medals", tooltips=[('medal', '@medal'), ('country', '@abbr')]) output_file("stacked_bar.html") show(bar)
from bokeh.charts import Bar, output_file, show from bokeh.charts.attributes import cat, color from bokeh.charts.operations import blend from bokeh.charts.utils import df_from_json from bokeh.sampledata.olympics2014 import data # utilize utility to make it easy to get json/dict data converted to a dataframe df = df_from_json(data) # filter by countries with at least one medal and sort by total medals df = df[df['total'] > 0] df = df.sort_values(by="total", ascending=False) bar = Bar(df, values=blend('bronze', 'silver', 'gold', name='medals', labels_name='medal'), label=cat(columns='abbr', sort=False), stack=cat(columns='medal', sort=False), color=color(columns='medal', palette=['SaddleBrown', 'Silver', 'Goldenrod'], sort=False), legend='top_right', title="Medals per Country, Sorted by Total Medals", tooltips=[('medal', '@medal'), ('country', '@abbr')]) output_file("stacked_bar.html", title="stacked_bar.py example") show(bar)
from bokeh.charts import Bar, output_file, show from bokeh.charts.attributes import cat, color from bokeh.charts.operations import blend from bokeh.charts.utils import df_from_json from bokeh.sampledata.olympics2014 import data # utilize utility to make it easy to get json/dict data converted to a dataframe df = df_from_json(data) # filter by countries with at least one medal and sort by total medals df = df[df["total"] > 0] df = df.sort("total", ascending=False) bar = Bar( df, values=blend("bronze", "silver", "gold", name="medals", labels_name="medal"), label=cat(columns="abbr", sort=False), stack=cat(columns="medal", sort=False), color=color(columns="medal", palette=["SaddleBrown", "Silver", "Goldenrod"], sort=False), legend="top_right", title="Medals per Country, Sorted by Total Medals", tooltips=[("medal", "@medal"), ("country", "@abbr")], ) output_file("stacked_bar.html") show(bar)
def portalDynamicity(df): def getWeekString(yearweek): if yearweek is None or len(str(yearweek)) == 0: return '' year = "'" + str(yearweek)[:2] week = int(str(yearweek)[2:]) # d = d - timedelta(d.weekday()) # dd=(week)*7 # dlt = timedelta(days = dd) # first= d + dlt # dlt = timedelta(days = (week)*7) # last= d + dlt + timedelta(days=6) return 'W' + str(week) + '-' + str(year) bp = figure(plot_width=600, plot_height=300, y_axis_type="datetime", responsive=True, tools='') # ,toolbar_location=None bp.toolbar.logo = None bp.toolbar_location = None label_dict={} for i, s in enumerate(df['snapshot']): label_dict[i] = getWeekString1(s) bp.yaxis[0].formatter = NumeralTickFormatter(format="0.0%") bp.xaxis[0].axis_label = 'Snapshots' bp.yaxis[0].axis_label = '% of portals' li = bp.line(df.index.values.tolist(), df['dyratio'], line_width=2, line_color='red', legend="dyratio") c = bp.circle(df.index.values.tolist(), df['dyratio'], line_width=2, line_color='red', legend="dyratio") li1 = bp.line(df.index.values.tolist(), df['adddelratio'], line_width=2, line_color='blue', legend="adddelratio") c = bp.circle(df.index.values.tolist(), df['adddelratio'], line_width=2, line_color='blue', legend="adddelratio") legend = bp.legend[0].legends bp.legend[0].legends = [] l = Legend(location=(0, -30)) l.items = legend bp.add_layout(l, 'right') labels=["staticRatio","updatedRatio","addRatio","delRatio"] #for l in labels: # df[l]= df[l]*100 print brewer.keys() colors = brewer["Pastel2"][len(labels)] bar = Bar(df, values=blend("staticRatio","updatedRatio","addRatio","delRatio", name='medals', labels_name='medal'), label=cat(columns='snapshot', sort=False), stack=cat(columns='medal', sort=False), color=color(columns='medal', palette=colors, sort=False), legend='top_right', bar_width=0.5, responsive=True, tooltips=[('ratio', '@medal'), ('snapshot', '@snapshot'),('Value of Total',' @height{0.00%}')]) legend = bar.legend[0].legends bar.legend[0].legends = [] l = Legend(location=(0, -30)) l.items = legend bar.add_layout(l, 'right') bar.xaxis[0].axis_label = 'Snapshots' bar.yaxis[0].axis_label = '% of datasets' bar.width=600 bar.height=300 bar.xaxis[0].formatter = FuncTickFormatter.from_py_func(getWeekStringTick) bar.toolbar.logo = None bar.toolbar_location = None bar.yaxis[0].formatter = NumeralTickFormatter(format="0.0%") return {'bar':bar,'lines':bp}
for hashtag in t ] trump_most_common_hashtags = Counter(trump_all_hashtags).most_common(15) trump_all_mentions = [ hashtag for t in tweet_df[tweet_df.handle == 'realDonaldTrump']['user_mentions'] for hashtag in t ] trump_most_common_mentions = Counter(trump_all_mentions).most_common(15) #put them on a plot labels, freq = zip(*trump_most_common_hashtags) data = {'data': freq, 'x': labels} bar = Bar(data, values='data',\ label=cat(columns='x', sort=False),\ title="Top Hashtags Used By Trump", \ legend = False, xlabel="Hashtags", ylabel="Number of Occurance") labels_2, freq_2 = zip(*trump_most_common_mentions) data_2 = {'data_2': freq_2, 'x_2': labels_2} bar_2 = Bar(data_2, values='data_2',\ label=cat(columns='x_2', sort=False),\ title="Top User Mentions By Trump", \ legend = False, xlabel="User Mentions", ylabel="Number of Occurance") output_file("trump_top_mentions.png.html") show(row(bar, bar_2))
for m in range(len(stat)): data['ACCEPTABLE'].append(stat[m][0]) data['UNACCEPTABLE'].append(stat[m][1] + stat[m][2]) data['Date'].append(unique_dates[m]) data['Total'] = [ data['ACCEPTABLE'][k] + data['UNACCEPTABLE'][k] for k in range(len(data['Date'])) ] df = pd.DataFrame(data) bar = Bar(df, values=blend('ACCEPTABLE', 'UNACCEPTABLE', name='Hours', labels_name='medal'), label=cat(columns='Date', sort=False), stack=cat(columns='medal', sort=False), color=color(columns='medal', palette=['Green', 'Red'], sort=False), legend='top_left', title="Respiration data quality of participant id " + str(ids[i]), width=800) output_file('Respiration_DataQuality_of_' + str(ids[i]) + ".html", title="RIP_BAR_Plot") source = ColumnDataSource(data) columns = [
def output_components(prefix, order, maxaln): rinfo = "{0}.rinfo".format(prefix) comp = "{0}.comp".format(prefix) def plot_read_dist(rinfo): df = pd.read_table(rinfo) data = df[['length', 'mapper', 'count']].groupby(['length', 'mapper']).count() data = data.apply(lambda s: s / data.sum() * 100, axis=1).reset_index() p = Bar(data, plot_width=500, plot_height=400, label='length', values='count', agg='sum', stack=cat(columns='mapper', sort=False), legend='top_right', color=color(columns='mapper', palette=["#f98283", "#a4a4a4"], sort=False), xlabel='read length (nt)', ylabel='proportion (%)', ygrid=False, tooltips=[('length', '@length'), ('mapper', '@mapper'), ('percent', '@height')]) p.toolbar.logo = None p.outline_line_color = None return p rdist = plot_read_dist(rinfo) df = pd.read_table(comp, index_col=0) total = df.sum() total = total * 100 / total.sum() df = df.apply(lambda s: s * 100 / s.sum(), axis=1) df = df.reset_index() #ftypes = df.columns[1:].tolist() ftypes = order colors = sns.color_palette("hls", len(ftypes)).as_hex() bar = Bar(df, values=blend(*ftypes, name="ftypes", labels_name="ftype"), x_range=rdist.x_range, y_range=(0, 100), label=cat(columns='rlen', sort=False), stack=cat(columns='ftype', sort=False), xlabel='read length (nt)', ylabel='proportion (%)', legend="top_right", ygrid=False, width=500, height=400, color=color(columns='ftype', palette=colors, sort=False), fill_alpha=1, tooltips=[("length", "@rlen"), ("feature", "@ftype"), ("percent", "@height")]) bar.toolbar.logo = None bar.outline_line_color = None start_angles = {} end_angles = {} start = 0 for ftype in ftypes: end = 2 * pi * total[ftype] / 100 start_angles[ftype] = start end_angles[ftype] = start + end start += end colors = dict(zip(ftypes, colors)) df = pd.DataFrame(total).reset_index() df.columns = ["ftype", "percent"] df["start"] = df.apply(lambda s: start_angles[s["ftype"]], axis=1) df["end"] = df.apply(lambda s: end_angles[s["ftype"]], axis=1) df["color"] = df.apply(lambda s: colors[s["ftype"]], axis=1) df["x"] = df.apply(lambda s: 1.2 * cos( (start_angles[s["ftype"]] + end_angles[s["ftype"]]) / 2), axis=1) df["y"] = df.apply(lambda s: 1.2 * sin( (start_angles[s["ftype"]] + end_angles[s["ftype"]]) / 2), axis=1) df["text"] = df.apply(lambda s: "{0:.3f}%".format(s["percent"]), axis=1) source = ColumnDataSource(data=df) pie = figure(width=400, height=400, x_range=(-1.4, 1.4), y_range=(-1.4, 1.4)) pie.toolbar.logo = None wr = pie.annular_wedge(x=0, y=0, inner_radius=0.5, outer_radius=1, start_angle="start", end_angle="end", fill_color="color", line_color="#ffffff", line_width=0.5, source=source) pie.axis.visible = False pie.grid.grid_line_color = None pie.outline_line_color = None hover = HoverTool(tooltips=[("feature", "@ftype"), ("percent", "@percent")], renderers=[wr]) pie.add_tools(hover) text_props = { "source": source, "angle": 0, "color": "black", "text_align": "center", "text_baseline": "middle", "text_font_size": "8pt", "text_font_style": "normal" } pie.text(x="x", y="y", text="text", **text_props) empty = figure(width=400, height=400, x_range=(-1.1, 1.1), y_range=(-1.1, 1.1)) empty.toolbar.logo = None empty.axis.visible = False empty.grid.grid_line_color = None empty.outline_line_color = None empty.toolbar_location = None stat = get_stat(prefix, maxaln) source = ColumnDataSource(data=stat) columns = [ TableColumn(field="Statistics", title="Statistics", width=200), TableColumn(field="Number of reads", title="Number of reads", formatter=NumberFormatter(format="0,0"), width=150), TableColumn(field="Proportion", title="Proportion", formatter=NumberFormatter(format="0.000%"), width=100), ] data_table = DataTable(source=source, columns=columns, width=450, row_headers=False) script, div = components( layout([[data_table, rdist], [pie, bar]], sizing_mode="scale_width")) return script, div
def stack_bar(data_dict): """ Creates a stacked bar graph showing percentages of participation for each member for each day/week (hover doesn't work) :param data_dict: Dictionary for stacked bar chart input. Maps each chart attribute to a list (with all lists being same length): {'labels': [Day1, Day2, ..., Day1, Day2, ...], 'members': [Member1, Member1, ..., Member2, Member2, ...], 'turns': [...], 'speak': [...] } :return: bokeh plot """ # Percentage of turns graph # Removed percentages from hover tool because didn't show correct percentages (y values accumulated) hover_turns = HoverTool( tooltips=""" <div> <div> <span style="font-size: 17px; font-weight: bold;">@member</span> </div> <div> <span style="font-size: 17px;">@labels</span> </div> </div> """ ) # turns = Bar(data=data_dict, values='turns', label='week_num', stack='member', turns = Bar(data=data_dict, values='turns', label=cat(columns='labels', sort=False), stack='member', title='Member Participation by Speaking Turns', legend='top_right', plot_width=800, plot_height=600, tools=[hover_turns]) # tools=[BoxZoomTool(), WheelZoomTool(), ResetTool()]) label_font_style = 'normal' # 'italic', 'bold' turns.below[0].axis_label = 'Dates' turns.below[0].major_label_orientation = 'horizontal' turns.below[0].axis_label_text_font_style = label_font_style turns.left[0].axis_label = 'Your Percentage of Participation (Based on Number of Speaking Turns)' turns.left[0].axis_label_text_font_style = label_font_style # Percentage of speaking time graph hover_speak = HoverTool( tooltips=""" <div> <div> <span style="font-size: 17px; font-weight: bold;">@member</span> </div> <div> <span style="font-size: 17px;">@labels</span> </div> </div> """ ) speak = Bar(data=data_dict, values='speak', label=cat(columns='labels', sort=False), stack='member', title='Member Participation by Speaking Time', legend='top_right', plot_width=800, plot_height=600, tools=[hover_speak]) # tools=[BoxZoomTool(), WheelZoomTool(), ResetTool()]) speak.below[0].axis_label = 'Dates' speak.below[0].major_label_orientation = 'horizontal' speak.below[0].axis_label_text_font_style = label_font_style speak.left[0].axis_label = 'Your Percentage of Participation (Based on Amount of Speaking Time)' speak.left[0].axis_label_text_font_style = label_font_style tab_turns = Panel(child=turns, title='Participation by Speaking Turns') tab_speak = Panel(child=speak, title='Participation by Speaking Time') bars = Tabs(tabs=[tab_speak, tab_turns]) return bars
def make_stacked_bar_chart(buckets, stack, data, palette, width=500, height=500): cp = data if buckets == "day": cp[buckets] = cp.apply(lambda row: row['pickup_datetime'].weekday(), axis=1) elif buckets == "month": cp[buckets] = cp.apply(lambda row: row['pickup_datetime'].month, axis=1) TOOLS = "pan,reset,hover,save" stacked_bar = Bar(cp, label=buckets, values=buckets, agg='count', stack=cat(stack, sort=True), tools=TOOLS, title="{0} Stacked Bar for {1}".format( ("Daily" if buckets == "day" else "Monthly"), stack.replace("_", " ").title()), palette=palette, plot_width=width, plot_height=height, toolbar_location="above", toolbar_sticky=False, legend_sort_field='color', legend_sort_direction='ascending', responsive=True) hover = stacked_bar.select_one(HoverTool) hover.point_policy = "follow_mouse" hover.tooltips = [("Frequency", "@height"), ("{0}".format(stack.replace("_", " ").title()), "@{0}".format(stack))] def day_ticker(): days = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ] if abs(int(round(tick)) - tick) < .05: return days[int(round(tick))] else: return "" def month_ticker(): months = [ "January", "February", "March", "April", "May", "June", "July" ] if abs(int(round(tick)) - tick) < .05: return months[int(round(tick))] else: return "" if buckets == "day": stacked_bar.xaxis.formatter = FuncTickFormatter.from_py_func( day_ticker) else: stacked_bar.xaxis.formatter = FuncTickFormatter.from_py_func( month_ticker) stacked_bar.grid.grid_line_color = "SlateGray" stacked_bar.grid.grid_line_alpha = .5 stacked_bar.grid.minor_grid_line_color = "SlateGray" stacked_bar.grid.minor_grid_line_alpha = .2 return stacked_bar
# Put data into dataframe broken into min, mean, and max values each for month ts = Series(vals, index=days[0]) firstmerge = pd.merge(ts.resample('M').min().to_frame(name="min"), ts.resample('M').mean().to_frame(name="mean"), left_index=True, right_index=True) frame = pd.merge(firstmerge, ts.resample('M').max().to_frame(name="max"), left_index=True, right_index=True) # You can use DataFrame index for bokeh x values but it doesn't like timestamp frame['Month'] = frame.index.strftime('%m-%Y') # Main object to render with stacking bar = Bar(frame, values=blend('min', 'mean', 'max', name='values', labels_name='stats'), label=cat(columns='Month', sort=False), stack=cat(columns='values', sort=False), color=color(columns='values', palette=['SaddleBrown', 'Silver', 'Goldenrod'], sort=True), legend=None, title="Statistical Values Grouped by Month", tooltips=[('Value', '@values')] ) # Legend info (if legend attribute is used it gets ugly with large dataset) factors = ["min", "mean", "max"] x = [0] * len(factors) y = factors pal = ['SaddleBrown', 'Silver', 'Goldenrod'] p = figure(width=100, toolbar_location=None, y_range=factors)