def plt_barchart_grupped(data, param_dict, mode='object', output_path=None): ''' data: rows = list of dicts(): keywords with neat naming straight to put in charts param_dict: dictionary with settings as follows: {'x':'name of column to put on x axis', 'y':'name of column to put in y axis', 'agg':'name of aggregate function: 'sum'/'count' /etc', 'color':'name of name of column to create siloses by/to apply aggr funct', 'tooltips':['list() of strings with column names to put in tooltip cloud']} mode: 'object'/'save'/'show'/'embed' - either returns plot object / saves to path + returns True / saves to path and shows plot output_path: string with html ext to save chart to. If None then emmbed is returned returns: True(saved to file) or emmbed data code ''' # tooltips=[ # ("Date", "$x"), # ("version", "@HVAC_version")] # p = Bar(data, label='Date', values='HVAC_version', agg='count', group='HVAC_version', # title="Median MPG by YR, grouped by ORIGIN", legend='bottom_right', bar_width=3.0, # plot_width=1000, plot_height=400, tooltips = tooltips) # p.legend.background_fill_alpha = 0.8 # output_file("bar.html") # show(p) #to_date(data,'%Y-%m-01') tooltips = create_tooltips(param_dict) title = "{} of {} by {}, grouped by {}".format(param_dict['agg'], param_dict['y'].upper(), param_dict['x'].upper(), param_dict['color'].upper()) p = Bar(data=data, label=param_dict['x'], values=param_dict['y'], agg=param_dict['agg'], group=param_dict['color'], title=title, legend='bottom_right', bar_width=3.0, plot_width=1000, plot_height=600, tooltips=tooltips) #legend_sort_field = param_dict['group']) p.legend.background_fill_alpha = 0.8 if type(data[0][param_dict['x']]) == str: x_rng = sort_axis(data, param_dict['x'], False, True) p.x_range = x_rng if type(data[0][param_dict['y']]) == str: y_rng = sort_axis(data, param_dict['y'], False, True) p.y_range = y_rng return do_output(p, mode, output_path)
def bar_plot_table(table, **kwargs): """Plot a tabular DataFrame with an index and multiple columns representing categories using a Bokeh bar chart. In addition to the keyword parameters accepted by bokeh.charts.Bar, this function accepts the following additional keyword arguments: number_of_categories: integer The number of categories ranked by total value to use. xaxis_formatter: TickFormatter The formatter to use for x axis values. yaxis_formatter: TickFormatter The formatter to use for y axis values. x_range: Range1d A range to use for the X-axis. y_range: Range1d A range to use for the Y-axis. """ if kwargs.has_key('number_of_categories'): revenue_table = analysis.select_top_n_columns( table, kwargs['number_of_categories']) else: revenue_table = table revenue_stacked = revenue_table.stack().reset_index() revenue_stacked.columns = ['year', 'commodity_desc', 'value'] revenue_plot = Bar( revenue_stacked, label='year', stack='commodity_desc', values='value', **_remove_custom_keys(kwargs) ) if kwargs.has_key('x_range'): revenue_plot.x_range = kwargs['x_range'] if kwargs.has_key('y_range'): revenue_plot.y_range = kwargs['y_range'] else: revenue_plot.y_range = Range1d(0, revenue_table.max().sum()) if kwargs.has_key('xaxis_formatter'): revenue_plot._xaxis.formatter = kwargs['xaxis_formatter'] if kwargs.has_key('yaxis_formatter'): revenue_plot._yaxis.formatter = kwargs['yaxis_formatter'] return revenue_plot
def bar_plot_table(table, **kwargs): """Plot a tabular DataFrame with an index and multiple columns representing categories using a Bokeh bar chart. In addition to the keyword parameters accepted by bokeh.charts.Bar, this function accepts the following additional keyword arguments: number_of_categories: integer The number of categories ranked by total value to use. xaxis_formatter: TickFormatter The formatter to use for x axis values. yaxis_formatter: TickFormatter The formatter to use for y axis values. x_range: Range1d A range to use for the X-axis. y_range: Range1d A range to use for the Y-axis. """ if kwargs.has_key('number_of_categories'): revenue_table = analysis.select_top_n_columns( table, kwargs['number_of_categories']) else: revenue_table = table revenue_stacked = revenue_table.stack().reset_index() revenue_stacked.columns = ['year', 'commodity_desc', 'value'] revenue_plot = Bar(revenue_stacked, label='year', stack='commodity_desc', values='value', **_remove_custom_keys(kwargs)) if kwargs.has_key('x_range'): revenue_plot.x_range = kwargs['x_range'] if kwargs.has_key('y_range'): revenue_plot.y_range = kwargs['y_range'] else: revenue_plot.y_range = Range1d(0, revenue_table.max().sum()) if kwargs.has_key('xaxis_formatter'): revenue_plot._xaxis.formatter = kwargs['xaxis_formatter'] if kwargs.has_key('yaxis_formatter'): revenue_plot._yaxis.formatter = kwargs['yaxis_formatter'] return revenue_plot
def make_bar_plot(labels, values): data = {'labels': labels, 'values': values} bar = Bar(data, values='values', label=CatAttr(columns=['labels'], sort=False), legend=False, agg='mean', bar_width=0.6, plot_width=500, plot_height=350) bar.y_range = Range1d(0.0, 1.0) script, div = components(bar) return script, div
def generate_chart(event): data = get_data(TIMED_EVENTS[event]) # 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, values='percentage_dnf', label='year', color="FireBrick", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan DNFs - " + event 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 = "Percentage DNF" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart._yaxis.formatter = NumeralTickFormatter(format="0%") barchart.y_range = Range1d(0, 1) barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['year'] num_dnf = data['dnfs'].loc[data['year'] == year] num_entries = data['entries'].loc[data['year'] == year] percent_dnf = data['percentage_dnf'].loc[data['year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[ ("# DNFs", '%d' % num_dnf.values[0]), ("# Entries", '%d' % num_entries.values[0]), ("% DNF", '%.2f%%' % (100 * percent_dnf.values[0])) ]) barchart.add_tools(hover) return barchart
def generate_chart(event): data = get_data(TIMED_EVENTS[event]) # 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, values='percentage_dnf', label='year', color="FireBrick", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan DNFs - " + event 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 = "Percentage DNF" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart._yaxis.formatter = NumeralTickFormatter(format="0%") barchart.y_range = Range1d(0, 1) barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['year'] num_dnf = data['dnfs'].loc[data['year'] == year] num_entries = data['entries'].loc[data['year'] == year] percent_dnf = data['percentage_dnf'].loc[data['year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[("# DNFs", '%d' % num_dnf.values[0]), ("# Entries", '%d' % num_entries.values[0]), ("% DNF", '%.2f%%' % (100 * percent_dnf.values[0]))]) barchart.add_tools(hover) return barchart
hover.tooltips = [(c, '@' + c) for c in cols] hover.tooltips.append(('index', '$index')) # Finally add/enable the tool fig.add_tools(hover) return fig p2 = scatter_with_hover(test2, 'abs_third_less_adv_simple', 'abs_third_less_adv', marker='circle', fig_width=800, fig_height=800, cols=['bea_code', 'date', 'abs_third_less_adv_simple', 'abs_third_less_adv'], size=20) p2.title ="Offsetting revisions" p2.xaxis.axis_label="Absolute (third less adv)" p2.yaxis.axis_label="Aggregate absolute (third less adv)" p2.x_range= Range1d(0,test2['abs_third_less_adv_simple'].max()*1.1) p2.y_range= Range1d(0,test2['abs_third_less_adv'].max()*1.1) sum_rev = test2['abs_third_less_adv'] simple = test2['abs_third_less_adv_simple'] regression = np.polyfit(simple, sum_rev, 1) r_x, r_y = zip(*((i, i*regression[0] + regression[1]) for i in range(len(test2['abs_third_less_adv'])))) p2.line(r_x, r_y, color="red", line_width=6) output_file("regression.html") show(p2)
# Make the chart TOOLS = '' plt2 = Bar(portions_df, label="Name", values="Portion", stack="SaleStatus", legend="bottom_right", color=['Green', 'Red'], xlabel="Listing feature", ylabel="Portion sold or unsold", title="Impact of listing features on likelihood of sale", tools=TOOLS) plt2.logo = None plt2.toolbar_location = None plt2.y_range = Range1d(start=0, end=1) # Save the plot output_file("./templates/plot3_new.html") # show(plt2) ####################################################### # Look at dependence of sale outcome on seller feedback score ####################################################### # hist = Histogram(data_sold, values='feedbackScore', color='listingType', # title="Distribution of feedback scores for sold items", legend='top_left') # # # Save the plot # output_file("./templates/data_exploration.html") # show(hist)
# Make the chart TOOLS = '' plt2 = Bar(portions_df, label="Name", values="Portion", stack = "SaleStatus", legend="bottom_right", color=['Green','Red'], xlabel="Listing feature", ylabel="Portion sold or unsold", title="Impact of listing features on likelihood of sale", tools=TOOLS) plt2.logo = None plt2.toolbar_location = None plt2.y_range = Range1d(start=0,end=1) # Save the plot output_file("./templates/plot3_new.html") # show(plt2) ####################################################### # Look at dependence of sale outcome on seller feedback score ####################################################### # hist = Histogram(data_sold, values='feedbackScore', color='listingType', # title="Distribution of feedback scores for sold items", legend='top_left') # # # Save the plot # output_file("./templates/data_exploration.html")