def bar_color_by_year(): df = odo(g.ds, pd.DataFrame) df1 = df.groupby(['production_year', 'color' ]).size().reset_index().rename(columns={0: 'count'}) sums = df.groupby( ['production_year', 'color']).agg('size').reset_index().groupby('production_year').sum() df1['prc'] = df1.apply(lambda x: x[2] * 100 / sums.loc[x[0]], axis=1) form = CarsRelativeValuesForm(request.args, csrf_enabled=False) field = 'prc' if 'submit' in request.args and form.validate( ) and form.is_relative.data else 'count' p = Bar( df1, values=field, label='production_year', stack='color', legend=False, #'top_right', title='Colors by Year of production', xlabel="Year", ylabel="Count", ) p.background_fill_color = "beige" return render(p, dict(form=form))
def fnCreate_Chart_Bar1(df,strS): pd.options.html.border=1 label1=CatAttr(columns=['Airline'], sort=False) plot = Bar(df, label=label1, values='Ticket Price',group='Airline', title="Average of Ticket Price- Airline wise" + strS, legend=False, bar_width=100, xlabel='Airlines',ylabel='Average Ticket Price') plot.logo=None script, div = components(plot,CDN) return script, div
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 make_datasources_for_comparison(idd_dataframe, years, measure_codes): # filter out SHAx measures calculated with METH2012 row_filter = (idd_dataframe.methodo == '0') & (idd_dataframe.measure_code.isin(measure_codes)) bar_charts = [] countries = set() sources = {} for year in years: chart_data = idd_dataframe[row_filter & (idd_dataframe.year == year)] [countries.add(x) for x in chart_data.location_name.unique()] bar = Bar(data=chart_data, stack='measure_name', values='observation', label='location_name', legend='top_right', bar_width=1) bar_charts.append(bar) base_source_df = \ pd.concat([x.to_df() for x in bar.select(dict(type=ColumnDataSource))]) sources['_' + year] = ColumnDataSource(base_source_df) # return the dictionary of ColumnDataSources and 1 bar chart -- # we want to keep a bar chart so we can reconstruct the legend, later return sources, bar_charts[0], countries
def calculate_average_accuracy(self, confusion_matrix): if confusion_matrix is None: return None scores = [] n = np.shape(confusion_matrix)[0] mean_acc = 0 for index, r in enumerate(confusion_matrix): ss = sum(r) if ss != 0: scores.append(float(r[index]) / ss) scores = np.hstack((scores,np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"accuracy": scores} s = Bar(data, cat=class_labels, title="Per Class Accuracy", xlabel='categories', ylabel='accuracy', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#ec5d5e"]) hover = s.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('accuracy', '@accuracy'), ]) p = hplot(s) script, div = components(p) return (script, div)
def fnCreate_Chart_Bar(df): pd.options.html.border=0 plot = Bar(df, 'Sub_Attbt', values='FY2013', title="Total DISPL by Attribute", bar_width=0.4) plot.logo=None script, div = components(plot,CDN) return script, div
def plot3(): p = figure(title='something', x_axis_label='date', x_axis_type='datetime') df = pd.read_csv('chart.csv') df3 = pd.read_csv('df3d.csv') output_file("stacked_bar.html") p = Bar(df3, label='Country', values='Population wo access water', stack='category', color=color(columns='category', palette=['Orange', 'Red'], sort=False), legend='top_right') script, div = components(p) df4 = df[df['status'] == 'water supplier'] output_file("bar.html") q = Bar(df4, label='Country', values='Possible water provider', legend=False, color="Green") script2, div2 = components(q) return render_template('plot3.html', script=script, div=div, script2=script2, div2=div2)
def calculate_average_accuracy(self, confusion_matrix): if confusion_matrix is None: return None scores = [] n = np.shape(confusion_matrix)[0] mean_acc = 0 for index, r in enumerate(confusion_matrix): ss = sum(r) if ss != 0: scores.append(float(r[index]) / ss) scores = np.hstack((scores, np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"accuracy": scores} s = Bar(data, cat=class_labels, title="Per Class Accuracy", xlabel='categories', ylabel='accuracy', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#ec5d5e"]) hover = s.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('accuracy', '@accuracy'), ]) p = hplot(s) script, div = components(p) return (script, div)
def plot_scores2(self): if self.scores is None: return None scores = np.vstack(self.scores) data = OrderedDict() for i in xrange(len(self.class_labels)): data[self.class_labels[i]] = scores[:2, i] s1 = Bar(data, cat=['precision', 'recall'], title="Per Class Scores", width=500, height=500, legend=True, tools="pan,resize,box_zoom,hover,save,reset") data = OrderedDict() for i in xrange(len(self.class_labels)): data[self.class_labels[i]] = scores[3:4, i] s2 = Bar(data, cat=['support'], title="Support", width=500, height=500, legend=True, tools="pan,resize,box_zoom,hover,save,reset") p = hplot(s1, s2) script, div = components(p) return (script, div)
def bar_response(results_list, output_path): output_dir = os.path.join(output_path, "charts") if os.path.isdir(output_dir) is False: os.mkdir(output_dir) tools = "pan,wheel_zoom,box_zoom,reset,hover,save" for df in results_list: print(df) p = Bar(df, label='hh_type', values='perc_res', stack='digital', title="a_title", legend='top_right', tools=tools) hover = p.select_one(HoverTool) hover.point_policy = "follow_mouse" hover.tooltips = [ ("count", "@height"), ] output_file_path = os.path.join(output_dir, 'test bar.html') output_file(output_file_path) show(p)
def report(): cursor = get_hive_cursor() if cursor is None: return render_template('/main/bi_connection_issue.html') # FIXME we probably want to create aggregates on hadoop # and cache them rather than returning the whole data # set here # we need to ignore monitoring pings which have rating user_id = -1 # and movie_id = -1 try: cursor.execute( "select * from movie_ratings where customer_id <> '-1' and movie_id <> '-1'", configuration={ 'hive.mapred.supports.subdirectories': 'true', 'mapred.input.dir.recursive': 'true' }) except: return render_template('/main/bi_connection_issue.html') df = as_pandas(cursor) count = df.shape[0] if count == 0: return render_template('/main/bi_no_records.html') from bokeh.charts import Bar, output_file, show fig = Bar( df, label='movie_ratings.rating', values='movie_ratings.rating', agg='count', title='Distribution of movie ratings', legend=False ) fig.plot_height = 400 fig.xaxis.axis_label = 'Rating' fig.yaxis.axis_label = 'Count ( Rating )' js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig) html = flask.render_template( '/main/embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def fnCreate_Chart_Bar(df): pd.options.html.border=1 # pd.options.display.html.border=1 label1=CatAttr(columns=['Routes'], sort=False) plot = Bar(df, label=label1, values='Ticket Price',group='Routes', title="Average of Ticket Price- Routes wise", legend=False, bar_width=50, xlabel='Routes',ylabel='Average Ticket Price') plot.logo=None script, div = components(plot,CDN) return script, div
def fnCreate_Chart_Bar_Full(df): pd.options.html.border=0 plot = Bar(df, label='Attbt', values='FY2013', agg='sum', group='Attbt', title="Total Sum, grouped by Attribute", legend='top_left') # plot = Bar(df, 'Sub_Attbt', values='FY2013', title="Total DISPL by Attribute", bar_width=0.4) plot.logo=None script, div = components(plot,CDN) return script, div
def goChart(label, stack_or_group, values, ylabel=None, color=None): convertPDFDate(workingPDF, keyFields[0]) if ylabel is None: ylabel=values label=label if isinstance(label, (list, tuple)) else [label] if stacked: charts.append( Bar(workingPDF, label=CatAttr(columns=label, sort=False), stack=stack_or_group, color=color, values=values, legend=self.showLegend(), ylabel=ylabel)) else: charts.append( Bar(workingPDF, label=CatAttr(columns=label, sort=False), group=stack_or_group, color=color, values=values, legend=self.showLegend(), ylabel=ylabel))
def withbokeh(self,dfMergeWithInfo): from bokeh.charts import Bar, output_file, show details = dfMergeWithInfo["videoSid"].values.tolist() amount = list(dfMergeWithInfo["playCount"].astype(float).values) print amount bar = Bar(dfMergeWithInfo, filename="bar.html") bar.title("MN Capital Budget - 2014").xlabel("Detail").ylabel("Amount") show(bar)
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 fnCreate_Chart_Bar2(df): output_file("line_bar.html") pd.options.html.border=0 plot = Bar(df, label='Attbt', values='FY2013', agg='sum', group='Attbt', title="Total Sum, grouped by Attribute", legend='top_left') # plot = Bar(df, 'Sub_Attbt', values='FY2013', title="Total DISPL by Attribute", bar_width=0.4) plot.logo=None show(plot) return True
def get_user_type(): subscriber_count = 0 customer_count = 0 unknown_count = 0 start_date_range = datetime.datetime.strptime(request.form['start_date'], "%Y-%m-%d").date() end_date_jinja2 = datetime.datetime.strptime(request.form['stop_date'], "%Y-%m-%d").date() end_date_range = datetime.datetime.strptime(request.form['stop_date'], "%Y-%m-%d").date() date_start = str(start_date_range) date_stop = str(end_date_range) date_stop_jinja2 = str(end_date_jinja2) #df = get_dataframe(start_date_range, end_date_range) if 'submitDateFilter' in request.form: df = date_form(df_init, date_start, date_stop) if 'submit_station' in request.form: df = station_form(df_init, date_start, date_stop) if 'submit_gender' in request.form: df = gender_form(df_init, date_start, date_stop) if 'submit_age' in request.form: df = age_form(df_init, date_start, date_stop) if 'submit_time' in request.form: df = time_form(df_init, date_start, date_stop) df = df_init user_type_series = df['usertype'].values for x in user_type_series: if x == 'Subscriber': subscriber_count += 1 elif x == 'Customer': customer_count += 1 else: unknown_count += 1 if unknown_count == 0: #customer_type = [subscriber_count, customer_count] df = pd.DataFrame({'User Type': ['Subscriber', 'Customer'], 'Type': [subscriber_count, customer_count]}) else: #customer_type = [subscriber_count, customer_count, unknown_count] df = pd.DataFrame({'User Type': ['Subscriber', 'Customer', 'Null'], 'Type': [subscriber_count, customer_count, unknown_count]}) #b = Bar(customer_type, title="Bar example", label='categories', ylabel='values', width=400, height=400) b = Bar(df, title="User Types", label='User Type', values='Type') b.select(dict(type=GlyphRenderer)) b.left[0].formatter.use_scientific = False scriptb, divb = components(b) return render_template('userType.html', s=subscriber_count, c=customer_count, u=unknown_count, d1=date_start, d2=date_stop_jinja2, scriptb=scriptb, divb=divb)
def plotbar(events, userid): """ Arch: I tried to install bokeh 0.10.0 ver and that was giving a lot of errors in the Bar(method args)- StopIteration and also while trying to load the latest bokeh.min.js from CDN So I am going back to the bokeh 0.8.1 ver- at least it works """ xr = [x.title for x in events] y1values = [y1.prefearfactor for y1 in events] y2values = [y2.postfearfactor for y2 in events] yr = OrderedDict(PredictedFear=y1values, ActualFear=y2values) """ height=400 width=600 bar = figure(plot_width=width, plot_height=height, title="Imagined vs real") # use the `rect` renderer to display stacked bars of the medal results. Note # that we set y_range explicitly on the first renderer bar.rect(xr, y1values, height=0.5, width=0.4, color="red", alpha=0.6) bar.rect(xr, y2values, height=0.5, width=0.4, color="blue", alpha=0.6) """ bar = Bar(yr, xr, xlabel="Events", ylabel="Fear factor", title="Imagined vs real fear", width=800, height=600, legend=True, palette=['#c1ff55', '#b7feea'], stacked=True, tools=None, xgrid=None, ygrid=None) bar.toolbar_location = None #output_file("stacked_bar.html", autosave=True) #show(bar) - commented out so it doesnt try to open display in linux #generating unique filenames by- concatenating userid to a rolledindex- #we dont expect a user to generate more than """ global rollingindex userfilepath = filepath+str(userid)+str(rollingindex.next())+'.js' """ userfilepath = filepath + str(userid) + '.js' js, tag = autoload_static( bar, CDN, '/' + userfilepath + '?id=' + str(randint(1, 1000000))) with open(userfilepath, 'w') as f: f.write(js) return js, tag
def plot(df): print("-" * 10) print(df) print("-" * 10) from bokeh.charts import Bar, show p = Bar(df, label=["model", "dim reduction"], \ values="accuracy", agg="mean", color="model",\ legend=None, bar_width=0.3, plot_width=600, plot_height=600) p.logo = None # will open a browser to show the plot. show(p)
def fnCreate_Chart_Bar(df): plot = Bar(df, label='Aspects', values='Count',group='Polarity', title="Average Sentiment for various Aspects", legend='top_left', bar_width=0.9,color=["green","red"], xlabel='Aspects',ylabel='Count') plot.logo=None plot.legend.label_text_font_size = "7pt" plot.legend.orientation = "horizontal" # plot.legend.click_policy="hide" script, div = components(plot,CDN) return script, div
def fnCreate_Stacked_Bar(df): # label1=CatAttr(columns=['Aspects'], sort=True) tmp=df plot = Bar(tmp, stack=tmp.columns[0], label=tmp.columns[1], values=tmp.columns[2], legend=True,title="Sentiment segmentation based on top 7 Aspects", xlabel='Polarity',ylabel='Count') plot.logo=None plot.legend.label_text_font_size = "7pt" plot.legend.orientation = "horizontal" script, div = components(plot,CDN) return script, div
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 draw_regional_fig_bokeh(make, model, year): """ Returns a bar graph figure rendered in Bokeh; for cars similar to the given car, groups price and mileage according to region in greater Boston """ try: min_auto_year = int(year) - 2 max_auto_year = int(year) + 2 except: min_auto_year = 2012 max_auto_year = 2016 listings = get_listings(make, model, year) df = pd.DataFrame(data=listings) if len(df) == 0: return row() #"No results found, check your spelling" df['mileage'] = df.apply(lambda row: get_mileage(row['description']), axis=1) df['price'] = df.apply(lambda row: get_price(row['price']), axis=1) df['region'] = df['link'].str[1:5] df['year'] = df.apply(lambda row: get_year(row['description']), axis=1) df['full_region'] = df.apply( lambda row: region_dict[row['region'].strip('/')] if row['region'].strip('/') in region_dict else None, axis=1) mileage_count = len(df[df['mileage'].notnull()]) title1 = 'Average Price of Used {0} {1}, {2}-{3}, by region, n={4}'.format( make, model, min_auto_year, max_auto_year, len(df)) title2 = 'Average Mileage of Used {0} {1}, {2}-{3}, by region, n={4}'.format( make, model, min_auto_year, max_auto_year, mileage_count) bar1 = Bar(df, label='full_region', values='price', agg='mean', title=title1, legend=None, xlabel="Region", ylabel="Price", y_range=(0, 20000)) bar2 = Bar(df, label='full_region', values='mileage', agg='mean', title=title2, legend=None, xlabel="Region", ylabel="Mileage", y_range=(0, 150000), color='dodgerblue') return row(bar1, bar2)
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 fnCreate_Chart_Bar_wf(df,stS= "all type reviews"): label1=CatAttr(columns=['word'], sort=False) plot = Bar(df, label=label1, values='freq',group='word', title="Top 10 Frequent Words for " + stS, legend=None, bar_width=0.9,xlabel='word',ylabel='freq') #,plot_width=450, plot_height=450 plot.logo=None plot.legend.label_text_font_size = "7pt" plot.legend.orientation = "horizontal" script, div = components(plot,CDN) return script, div
def systemEvolutionBarPlot(df, yLabel, values): with Timer(key='systemEvolutionBarPlot', verbose=True): p = Bar(df, label='snapshot', values=values, agg='sum', stack='software', legend='bottom_left', bar_width=0.5, xlabel="Snapshots", ylabel=yLabel, responsive=True, height=200,tools='hover') glyph_renderers = p.select(GlyphRenderer) bar_source = [glyph_renderers[i].data_source for i in range(len(glyph_renderers))] hover = p.select(HoverTool) hover.tooltips = [ ('software',' @software'), ('value', '@height'), ] p.xaxis.formatter=FuncTickFormatter.from_py_func(getWeekStringTick) p.axis.minor_tick_line_color = None p.background_fill_color = "#fafafa" p.legend.location = "top_left" p.toolbar.logo = None p.toolbar_location = None legend=p.legend[0].legends p.legend[0].legends=[] l = Legend( location=(0, -30)) l.items=legend p.add_layout(l, 'right') return p
def get_counties_plot(data_frame): plot = Bar(data_frame, label='CTYNAME', values='TOT_POP', agg='max', plot_width=1200, plot_height=500, title='Population by County', legend=False) plot.xaxis.axis_label = 'County' plot.yaxis.axis_label = 'Population' plot.yaxis.formatter = NumeralTickFormatter(format="0a") plot.sizing_mode = 'scale_width' return plot
def main(): # check recalculation request if 'recalculate' in request.args: if request.args.get('recalculate') == 'True': betalyzer.recalculate() # build sector betas bar chart sector_betas = betalyzer.df_tickers.groupby('sector')['beta'].mean() bk_sector_betas = Bar(sector_betas, plot_width=550, plot_height=400, legend=None) bk_sector_betas_script, bk_sector_betas_div = components(bk_sector_betas) # build market cap betas bar chart mktcap_betas = betalyzer.df_tickers.groupby( 'market_cap_decile')['beta'].mean() bk_mc_betas = Bar(mktcap_betas, plot_width=550, plot_height=400, legend=None) bk_mc_betas_script, bk_mc_betas_div = components(bk_mc_betas) # build market cap scatter plot scatter = Scatter(betalyzer.df_tickers, x='market_cap_log', y='beta', plot_width=550, plot_height=400) scatter_script, scatter_div = components(scatter) # build line plot for top three stocks top_tickers = betalyzer.df_tickers['ticker'].head(3) bk_history = Line(betalyzer.df_betas[top_tickers], plot_width=550, plot_height=400) bk_history_script, bk_history_div = components(bk_history) return render_template( 'main.html', dt_tickers=betalyzer.df_tickers.to_dict(orient='records'), bk_sector_betas_script=bk_sector_betas_script, bk_sector_betas_div=bk_sector_betas_div, bk_mc_betas_script=bk_mc_betas_script, bk_mc_betas_div=bk_mc_betas_div, scatter_script=scatter_script, scatter_div=scatter_div, bk_history_script=bk_history_script, bk_history_div=bk_history_div)
def fnCreate_Chart_Bar(df): pd.options.html.border = 1 plot = Bar(df, label='ID', values='Tweet Count', group='ID', title="Top 5 contributors", legend=False, bar_width=1.8, xlabel='Users', ylabel='Tweets Count') plot.logo = None script, div = components(plot, CDN) return script, div
def createBokehChart(self): data = self.getWorkingPandasDataFrame() keyFields = self.getKeyFields() valueFields = self.getValueFields() clusterby = self.options.get("clusterby") stacked = self.options.get("charttype", "grouped") == "stacked" subplots = self.isSubplot() # self.debug("keyF={0}, valueF={1}, cluster={2}, stacked={3}".format(keyFields[0], valueFields[0], clusterby, stacked)) charts = [] params = [] if subplots and len(valueFields) > 1: for val in valueFields: series = clusterby if clusterby is not None else False values = val params.append((values, series)) elif clusterby is not None and len(valueFields) <= 1: params.append((valueFields[0], clusterby)) else: series = '_'.join(valueFields) values = blend(*valueFields, name=series.replace('_', ','), labels_name=series) if clusterby is not None: self.addMessage( "Warning: 'Cluster By' ignored when you have multiple Value Fields but subplots option is not selected" ) params.append((values, series)) for p in params: if stacked: b = Bar(data, label=keyFields[0], values=p[0], stack=p[1], legend=self.showLegend()) else: b = Bar(data, label=keyFields[0], values=p[0], group=p[1], legend=self.showLegend()) charts.append(b) return charts
def plot_top_topic_of_author_by_year(self, df_author_eval, plot=True): '''needs output from plot_author_evolution_plot(a_id) as input Plots the top topic of the author for the given year''' years = [y for y in range(1987, 2017)] dc = df_author_eval.cumsum(axis=1) p = [dc[y].idxmax() for y in years] if (plot): # plt.bar(years,p) # plt.show() df_f = pd.DataFrame({'Years': years, 'TopTopic': p}) output_file("toptopicbyyear.html") pl = Bar(df_f, 'Years', values='TopTopic', title="Bar Plot of top Topic", color='blue', legend=False) show(pl) print("The author changed the topic %d times" % len(np.unique(p))) print("\n Following are the topics:") for i in np.unique(p): print("\nTopic %d" % (i + 1)) print('\n Top Words:') print(self.model.show_topic(i)) print('\n') return p
def plot_crashes_vs_year(air_data, method, save_fig=True): """ 姣忓勾绌洪毦鏁板垎鏋� """ if method == 'sns': # Seaborn 缁樺浘 plt.figure(figsize=(15.0, 10.0)) sns.countplot(x='Year', data=air_data) # 瑙e喅matplotlib鏄剧ず涓枃闂 plt.rcParams['font.sans-serif'] = ['SimHei'] # 鎸囧畾榛樿瀛椾綋 plt.rcParams[ 'axes.unicode_minus'] = False # 瑙e喅淇濆瓨鍥惧儚鏄礋鍙�-'鏄剧ず涓烘柟鍧楃殑闂 plt.title(u'绌洪毦娆℃暟 vs 骞翠唤') plt.xlabel(u'骞翠唤') plt.ylabel(u'绌洪毦娆℃暟') plt.xticks(rotation=90) if save_fig: plt.savefig('crashes_year.png') plt.show() elif method == 'bokeh': # Boken 缁樺浘 p = Bar(air_data, 'Year', title='绌洪毦娆℃暟 vs 骞翠唤', plot_width=1000, legend=False, xlabel='骞翠唤', ylabel='绌洪毦娆℃暟') p.xaxis.major_label_orientation = pi / 2 output_file('crashes_year.html') show(p)
def topic_evolution_by_year(self, topic, sp=True): year_dist = self.year_dist topic_score = [] for i in range(len(year_dist)): topic_score.append(year_dist[i].sum()[topic]) tf = pd.DataFrame({ 'Topic' + str(topic) + 'Score': topic_score, 'Year': [y for y in range(1987, 2017)] }) # ax=tf.plot(kind='bar') # ax.set_xticklabels([y for y in range(1987,2017)], rotation=90) # plt.show() # output_file("topicevolutionbyyear.html") p = Bar(tf, 'Year', values='Topic' + str(topic) + 'Score', title="Bar Plot of Topic %d score for all years " % topic, color='green', legend=False) if (sp): show(p) print('\n Following are the top words in topic %d:' % topic) print(self.model.show_topic(topic)) return topic_score, p
def leagues(): grouped = df_squads.groupby('year') club_country = df_squads.club_country.unique() club_country.sort() df_clb_ctry = pd.DataFrame({"club_country":club_country}) for year in df_squads.year.unique(): country_to_club = grouped.club_country.value_counts()[year] / grouped.team.value_counts()[year] df_clb_ctry = df_clb_ctry.join(country_to_club.to_frame(year)) df_clb_ctry = df_clb_ctry.fillna(0) # If NaN, country league not represented at the world cup df_clb_ctry = df_clb_ctry.T plot_nat_leagues = figure(tools=TOOLS, x_axis_label='World Cup Year', toolbar_location=None ) # take the top 11 countries and plot for num, country in enumerate(df_clb_ctry.sum().sort_values(ascending=False).head(11).index): plot_nat_leagues.scatter(df_clb_ctry.index, df_clb_ctry[country],legend=country,color=RdBu11[num],size=8, alpha=0.5) plot_nat_leagues.legend.location = "top_left" df_expats = df_league_data[df_league_data.nation!=df_league_data.nationality] seasons = df_expats.season.unique() min = int(seasons[0].split('-')[0]) max = int(seasons[-1].split('-')[0]) wc_year_slider = Slider(start=min, end=max, value=max, step=1, title="Soccer Season") for season in seasons: nationality_expats = df_expats.groupby('season').nationality.value_counts()[season].head(5) nation_expats = df_expats.groupby('season').nation.value_counts()[season] plot_expats = Bar(nationality_expats, toolbar_location=None) plot_expats.x_range = FactorRange(factors=nationality_expats.index.tolist()) plot_dict = {"nations":plot_nat_leagues, "expats":plot_expats} script, div = components(plot_dict) return render_template('league.html', js_resources=js_resources, script=script, div=div)
def plot_average_dts(df, hours): lasti = 0 tmp = 0 tmplist = [] ### Currently using a fixed method timeset, if there is a better way to do a log loop this could be changed. ### lasti = 0 tmp = 0 tmplist = [] xlist = [] for i in range(1,10): tmp = 0 for item in df['noao_time']: if item > lasti and item < i: tmp = tmp + 1 tmplist.append(tmp) xlist.append(i) lasti = i for i in range(10,100,10): tmp = 0 for item in df['noao_time']: if item > lasti and item < i: tmp = tmp + 1 tmplist.append(tmp) xlist.append(i) lasti = i for i in range(100,1000,100): tmp = 0 for item in df['noao_time']: if item > lasti and item < i: tmp = tmp + 1 tmplist.append(tmp) xlist.append(i) lasti = i graph_info = {} graph_info['values'] = tmplist graph_info['Time in minutes'] = xlist p = Bar(graph_info, values='values', label='Time in minutes', ylabel='Number of transfers', color='navy', title='DTS time plot') p.plot_height = 500 p.plot_width = 1000 return p
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 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(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 MonthGraph(request): if request.method =='GET' : if request.user.is_authenticated(): now = datetime.now() now = formats.date_format(now,"SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0] print "Don't select data" head="" plot = Bar(y,title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render(request, "monthgraph.html", {"the_script": script, "the_div": div,'devices': DeviceProfile.objects.filter(owner=request.user),"date":str(now)}) else: return redirect('/authen/') elif request.method == 'POST': now = datetime.now() now = formats.date_format(now,"SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0] head="" if request.POST.get('deviceid') is not None: id = request.POST.get('deviceid') if request.POST.get('month') is not None: month = request.POST.get('month') do = DeviceProfile.objects.get(device_id=id) data = DeviceUsage.objects.filter(device_id=do,date__month=month).values('date').annotate(sumusage=Sum('usage'),sumtime=Sum('time')) #print data # data[0].get('date').strftime("%d") get only date from year-month-date for d in data: hr = (float(d.get('sumtime'))/3600) kw = float(d.get('sumusage')) y[int(d.get('date').strftime("%d"))] = kw * hr head = "usage of device name: "+ do.device_name +" at "+request.POST.get('month')+"th Month" else: print "Don't select data" head="" plot = Bar(y,title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render(request, "monthgraph.html", {"the_script": script, "the_div": div,'devices': DeviceProfile.objects.filter(owner=request.user),"date":str(now)})
def fun(): plot_transfer_fee = figure(tools=TOOLS, title='World Record Soccer Transfer Fee', x_axis_label='Year', y_axis_label=u"Fee (\u00A3)", toolbar_location=None ) plot_transfer_fee.line(df_tranfer['year'], df_tranfer['fee_pounds'],color=RdBu11[0],line_width=4) month_map = {k: v for k,v in enumerate(calendar.month_abbr)} df_mth = df_squads.birth_month.value_counts().sort_index() df_mth.index = df_mth.index.map(lambda x: month_map[x]) plot_mth_of_birth = Bar(df_mth, toolbar_location=None) plot_mth_of_birth.x_range = FactorRange(factors=df_mth.index.tolist()) plots = {"plt_trans_fee": plot_transfer_fee, "plt_mth_birth": plot_mth_of_birth} script, div = components(plots) return render_template('interesting.html', js_resources=js_resources, script=script, div=div)
def generate_chart(year): data = get_data(year) plot_data = {'country': data.index.tolist(), 'count': [float(i) for i in data.values.tolist()]} barchart = Bar(plot_data, label='country', values='count', color="red", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + year + " Countries" barchart.x_range = FactorRange(factors=data.index.tolist()) barchart._xaxis.axis_label = "Country" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Number of Teams" 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=[("Country", '@x'), ("# Teams", '@height')]) barchart.add_tools(hover) return barchart
def plot_scores(self, all_scores): if all_scores is None: return None scores = all_scores[0][:] scores = np.hstack((scores,np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"precision": scores} s1 = Bar(data, cat=class_labels, title="Per Class Precision", xlabel='categories', ylabel='precision', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#b2df8a"]) hover = s1.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('precision', '@precision'), ]) scores = all_scores[1][:] scores = np.hstack((scores,np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"recall": scores} s2 = Bar(data, cat=class_labels, title="Per Class Recall", xlabel='categories', ylabel='recall', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#a6cee3"]) hover = s2.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('recall', '@recall'), ]) data = {"support": all_scores[3]} s3 = Bar(data, cat=self.class_labels, title="Per Class Support", xlabel='categories', ylabel='support', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#6a3d9a"]) hover = s3.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('support', '@support'), ]) p = hplot(s1, s2, s3) script, div = components(p) return (script, div)
def output_chart(issues_df,output_mode='static'): import datetime import bokeh from bokeh.models import HoverTool # Add timestamp to title issues_chart = Bar(issues_df, label='value_delivered', values='status', agg='count', stack='status', title=ISSUES_TITLE+" (Updated "+datetime.datetime.now().strftime('%m/%d/%Y')+")", xlabel="Value Delivered",ylabel="Number of Use Cases", legend='top_right', tools='hover', color=brewer["GnBu"][3] ) issues_chart.plot_width = DESTINATION_FRAME_WIDTH - (HTML_BODY_MARGIN * 2) issues_chart.plot_height = DESTINATION_FRAME_HEIGHT - (HTML_BODY_MARGIN * 2) issues_chart.logo = None issues_chart.toolbar_location = None hover = issues_chart.select(dict(type=HoverTool)) hover.tooltips = [ ("Value Delivered", "$x")] #--- Configure output --- reset_output() if output_mode == 'static': # Static file. CDN is most space efficient output_file(ISSUES_FILE, title=ISSUES_TITLE, autosave=False, mode='cdn', root_dir=None ) # Generate file save(issues_chart,filename=ISSUES_FILE) elif output_mode == 'notebook': output_notebook() # Show inline show(issues_chart) else: # Server (using internal server IP, rather than localhost or external) session = bokeh.session.Session(root_url = BOKEH_SERVER_IP, load_from_config=False) output_server("ddod_chart", session=session) show(issues_chart)
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
for x in portions_sold: portions_unsold.append((x[0],1-x[1],x[2],'Unsold',x[4])) portions = portions_sold + portions_unsold # Put into a df portions_df = pd.DataFrame.from_records(portions, columns=["Name", "Portion","HasFeature","SaleStatus", "Feature"] ) # 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
''' import pandas as pd from bokeh.charts import Bar, output_file, show, defaults import bokeh.plotting as bp from bokeh.models import FixedTicker, LinearAxis from bokeh.io import output_file, show, vplot crapsArray(100, 5, 300) csv=pd.read_csv('test.csv') output_file("craps.html") #Plot sizes for charts defaults.width = 1200 defaults.height = 300 action = Bar(csv, 'roll', values = 'score', title = "Craps Outcomes", bar_width = 1, color = 'score') money = Bar(csv, 'roll', values = 'bank', title = "Craps Bankroll", bar_width = 1, color = 'score') winLose = Bar(csv, 'roll', values = 'value', title = "Craps Roll Win/Loss", bar_width = 1, color = 'score') xaxis=action.select({'type' : LinearAxis}) xaxis.ticker=FixedTicker(ticks=[0,25,50,75,100]) yaxis=action.select({'type' : LinearAxis}) yaxis.ticker=FixedTicker(ticks=[2,3,4,5,6,7,8,9,10,11,12]) p = vplot(action, money,winLose) show(p) print("Cashing Out")
output_file('data_table.html') show(data_table) test3.sort_values('abs_third_less_adv', inplace=True, ascending=False) test3.reset_index(inplace=True) source = ColumnDataSource(test3) p2 = Bar(test3, 'category', values='third_less_adv', title="GDP revisions for 2011 - 2015: Simple", plot_width=1000, plot_height=1000) output_file('2011_2015_change.html') show(p2) p3 = Bar(test3, 'category', values='abs_third_less_adv', title="GDP revisions for 2011 - 2015: absoulte value", plot_width=1000, plot_height=1000) output_file('2011_2015_abs_change.html') show(p3)
def index(): if request.method == 'GET': return render_template('index.html') else: # request was a POST # store the city code # all processing is based on filtering summary tables on city code app.vars['citycode'] = request.form['city'] citycode = app.vars['citycode'] # app.vars['features'] = request.form.getlist('features') ####################### # Summary table loads # ####################### # coll_inj = pd.read_csv('1_coll_inj_ann.csv', encoding='utf-8') # top_10 = pd.read_csv('2_top_10.csv', encoding='utf-8') # surf_vs_high = pd.read_csv('3_surf_vs_high.csv', encoding='utf-8') # inj_causes = pd.read_csv('4_inj_causes.csv', encoding='utf-8') # weather = pd.read_csv('5_weather.csv', encoding='utf-8') # hour = pd.read_csv('6_hour.csv', encoding='utf-8') # city_stats14 = pd.read_csv('7_2014_city_stats.csv', encoding='utf-8') # ins_est13 = pd.read_csv('8_2013_ins_est.csv', encoding='utf-8') # kfinal = pd.read_csv('9_kmeans_and_pop.csv', encoding='utf-8') ################################ # Hardcoded summary statistics # ################################ # California collision stats capop2014 = 38357121 cacol2014 = 384646 caco1k = np.round(1000.0 * cacol2014 / capop2014, 1) cainj2014 = 225044 cainj1k = np.round(1000.0 * cainj2014 / capop2014, 1) cakill2014 = 3068 cak1m = np.round(1000000.0 * cakill2014 / capop2014, 1) # Califonia insurance stats human_claims_2013 = 4207773612 prop_claims_2013 = 7422643259 inj_2013 = 223128 sev_inj_2013 = 10664 kill_2013 = 3104 parties_2013 = 763086 # cost per X insurance estimates # crude! cost_per_inj = np.round(human_claims_2013 / \ (1.0 * inj_2013 + 9.0 * sev_inj_2013 + 19.0 * kill_2013), 1) cost_per_party = np.round(prop_claims_2013 / (1.0 * parties_2013), 1) cacpc = np.round(1.0 * (human_claims_2013 + prop_claims_2013) / 37965755, 2) # City collision stats citystats = city_stats14[city_stats14['cccode'] == int(citycode)] cityname = citystats['COUNTY/CITY'].values[0] collper1k = np.round(citystats['collper1k'].values[0],1) injper1k = np.round(citystats['injper1k'].values[0],1) killper1m = np.round(citystats['killper1m'].values[0],1) # City insurance Stats insstats = ins_est13[ins_est13['cccode'] == int(citycode)] citycpc = np.round(insstats['estpercapita'].values[0], 2) tools='save' # Other City Stats citypop = kfinal[kfinal['cccode'] == int(citycode)]['pop2014'].values[0] citypop = locale.format("%d", citypop, grouping=True) cluster = kfinal[kfinal['cccode'] == int(citycode)]['cluster'].values[0] ################ # Annual Trend # ################ df = coll_inj[coll_inj['cccode'] == int(citycode)] print 'before bar definition' p1 = Bar(df, label='year', values='count', title="Collisions & Injuries by Year", group='type', xlabel='Year', ylabel='Collisions/Injuries', height=400, # width=600, # bar_width=0.1, tools=tools, legend='bottom_left' ) p1.toolbar_location=None p1.responsive=True print 'end p1 definition' ############################## # Top 10 Worst Intersections # ############################## columns = ['inter', 'numcoll', 'SUM(NUMBER_INJURED)', 'SUM(NUMBER_KILLED)'] temp = top_10[top_10['CNTY_CITY_LOC'] == int(citycode)][columns] newcolumns = ['Intersection', 'Collisions', 'Injuries', 'Fatalities'] temp.columns = newcolumns interhtml = temp.to_html(index=False).replace('border="1"', 'border="0"') interhtml = interhtml.replace('\n', '') ############################### # Surface Streets vs Highways # ############################### df3 = surf_vs_high[surf_vs_high['cccode'] == int(citycode)] df3 = df3[['type', 'count', 'dtype']] p2 = Bar(df3, label='dtype', values='count', stack='type', title="Surface Streets vs Freeways and Highways", xlabel='', ylabel='Proportion', height=400, # width=600, bar_width=0.9, tools=tools, legend='bottom_right' ) p2.toolbar_location=None p2.responsive=True print 'end p2 definition' ############################################## # Primary Causes of Collisions with Injuries # ############################################## df4 = inj_causes[inj_causes['CNTY_CITY_LOC'] == int(citycode)] p3 = Bar(df4, label='pcf', values='SUM(NUMBER_INJURED)', title="Primary Violations Leading to Injuries", xlabel='', ylabel='Injuries', height=400, # width=600, bar_width=0.9, tools=tools, color='black' ) p3.toolbar_location=None p3.responsive=True print 'end p3 definition' ################################ # Weather at time of collision # ################################ df5 = weather[weather['CNTY_CITY_LOC'] == int(citycode)] p4 = Bar(df5, label='weather', values='numcoll', title="Weather Conditions for Collisions", xlabel='', ylabel='Collisions', height=400, # width=600, bar_width=0.9, tools=tools, color='yellow' ) p4.toolbar_location=None p4.responsive=True print 'end p4 definition' ############### # Time of Day # ############### df6 = hour[hour['CNTY_CITY_LOC'] == int(citycode)] p5 = Bar(df6, label='hour', values='numcoll', title="Time of Day", xlabel='Hour', ylabel='Collisions', height=400, # width=600, bar_width=0.9, tools=tools, color='black' ) p5.toolbar_location=None p5.responsive=True ################# # Plot encoding # ################# plots = {'trend': p1, 'streettypes': p2, 'injurycauses': p3, 'weather': p4, 'hour': p5} script, div = components(plots, INLINE) # script1 = encode_utf8(script1) ############# # Rendering # ############# print 'before render' html = render_template('index_post.html', citycode=citycode, cityname=cityname, collper1k=collper1k, caco1k=caco1k, injper1k=injper1k, cainj1k=cainj1k, killper1m=killper1m, cak1m=cak1m, citycpc=citycpc, cacpc=cacpc, plot_scripts=script, div=div, interhtml=interhtml, citypop=citypop, cluster=cluster) print 'after render' return encode_utf8(html)
def make_bar(counter, keywords, output_filename): data = pd.Series(filter_counter(counter, keywords)) bar = Bar(data, title='Word Counts') output_file(output_filename) bar.show()
ch1TotalDislikes = channel1_data["total_dislikes"] ch2TotalDislikes = channel2_data["total_dislikes"] ch1TotalSubscribers = channel1_data["total_subscribers"] ch2TotalSubscribers = channel2_data["total_subscribers"] # Data to be used for graphone ordered into groups data = { 'Numbers': ['Dislikes', 'Likes', 'Subscribers', 'Total Views', 'Dislikes', 'Likes', 'Subscribers', 'Total Views'], 'Channels': [ch1, ch1, ch1, ch1, ch2, ch2, ch2, ch2], 'Total': [ch1TotalDislikes, ch1TotalLikes, ch1TotalSubscribers, ch1TotalViews, ch2TotalDislikes, ch2TotalLikes, ch2TotalSubscribers, ch2TotalViews] } # Puts all the data into a format which may be graphed with correct parameters: bar = Bar(data, values='Total', label=['Channels', 'Numbers'],agg = 'sum', title="Comparing two Youtube Channels", width=500, height = 1000, group = 'Numbers', tools=['hover', 'resize', 'box_zoom', 'wheel_zoom', 'pan']) # Allows the hover tool to function: hover = bar.select(dict(type=HoverTool)) hover.tooltips = [('Value of Channel',' $x'),('Value of Total',' @height')] # outputs a file with the data for the graph: output_file("stacked_bar.html") # Shows the graph: show(hplot(bar)) # #End of graphing functions code! #**************************************************************************************
def predict(): def weighted_choice(choices): total = sum(w for c, w in choices) r = random.uniform(0, total) upto = 0 for c, w in choices: if upto + w >= r: return c upto += w assert False, "Shouldn't get here" df_fifa_latest_rank z = np.polyfit(df_prob_win_fifa_rank.rank_diff, df_prob_win_fifa_rank.prob, 4) p_win = np.poly1d(z) # p_win(_some_rank) z = np.polyfit(df_prob_draw_fifa_rank.rank_diff.values, df_prob_draw_fifa_rank.prob.values, 3) p_draw = np.poly1d(z) country_codes = { "Albania": "ALB", "Austria": "AUT", "Belgium": "BEL", "Croatia": "CRO", "Czech Republic": "CZE", "England": "ENG", "France": "FRA", "Germany": "GER", "Hungary": "HUN", "Iceland": "ISL", "Italy": "ITA", "Northern Ireland": "NIR", "Poland": "POL", "Portugal": "POR", "Republic of Ireland": "IRL", "Romania": "ROU", "Russia": "RUS", "Slovakia": "SVK", "Spain": "ESP", "Sweden": "SWE", "Switzerland": "SUI", "Turkey": "TUR", "Ukraine": "UKR", "Wales": "WAL", } country_from_codes = { "ALB": "Albania", "AUT": "Austria", "BEL": "Belgium", "CRO": "Croatia", "CZE": "Czech Republic", "ENG": "England", "FRA": "France", "GER": "Germany", "HUN": "Hungary", "ISL": "Iceland", "ITA": "Italy", "NIR": "Northern Ireland", "POL": "Poland", "POR": "Portugal", "IRL": "Republic of Ireland", "ROU": "Romania", "RUS": "Russia", "SVK": "Slovakia", "ESP": "Spain", "SWE": "Sweden", "SUI": "Switzerland", "TUR": "Turkey", "UKR": "Ukraine", "WAL": "Wales", } grp_A_matches = [ ['2016-06-10', 'France', 'Romania'], ['2016-06-11', 'Albania', 'Switzerland'], ['2016-06-15', 'Romania', 'Switzerland'], ['2016-06-15', 'France', 'Albania'], ['2016-06-19', 'Switzerland', 'France'], ['2016-06-19', 'Romania', 'Albania'], ] grp_B_matches = [ ['2016-06-11', 'Wales', 'Slovakia'], ['2016-06-11', 'England', 'Russia'], ['2016-06-15', 'Russia', 'Slovakia'], ['2016-06-16', 'England', 'Wales'], ['2016-06-20', 'Slovakia', 'England'], ['2016-06-20', 'Russia', 'Wales'], ] grp_C_matches = [ ['2016-06-12', 'Poland', 'Northern Ireland'], ['2016-06-12', 'Germany', 'Ukraine'], ['2016-06-16', 'Ukraine', 'Northern Ireland'], ['2016-06-16', 'Germany', 'Poland'], ['2016-06-21', 'Northern Ireland', 'Germany'], ['2016-06-21', 'Ukraine', 'Poland'], ] grp_D_matches = [ ['2016-06-12', 'Turkey', 'Croatia'], ['2016-06-13', 'Spain', 'Czech Republic'], ['2016-06-17', 'Czech Republic', 'Croatia'], ['2016-06-17', 'Spain', 'Turkey'], ['2016-06-21', 'Croatia', 'Spain'], ['2016-06-21', 'Czech Republic', 'Turkey'], ] grp_E_matches = [ ['2016-06-13', 'Republic of Ireland', 'Sweden'], ['2016-06-13', 'Belgium', 'Italy'], ['2016-06-17', 'Italy', 'Sweden'], ['2016-06-18', 'Belgium', 'Republic of Ireland'], ['2016-06-22', 'Sweden', 'Belgium'], ['2016-06-22', 'Italy', 'Republic of Ireland'], ] grp_F_matches = [ ['2016-06-14', 'Austria', 'Hungary'], ['2016-06-14', 'Portugal', 'Iceland'], ['2016-06-18', 'Iceland', 'Hungary'], ['2016-06-18', 'Portugal', 'Austria'], ['2016-06-22', 'Iceland', 'Austria'], ['2016-06-22', 'Hungary', 'Portugal'], ] grp_matchs = grp_A_matches + grp_B_matches + grp_C_matches + grp_D_matches + grp_E_matches + grp_F_matches df_ctry_codes = df_fifa_latest_rank.country_code probailities = [] plot_dict = {} prediction_dict = {} results = {} for k, match in enumerate(grp_matchs,1): rank_diff = df_fifa_latest_rank[df_ctry_codes==country_codes[match[1]]].ranking.values - df_fifa_latest_rank[df_ctry_codes==country_codes[match[2]]].ranking.values probs = [p_win(rank_diff*-1)[0], p_draw(abs(rank_diff))[0], p_win(rank_diff)[0]] probs = [float(i)/sum(probs) for i in probs] probailities = [country_codes[match[1]], 'DRAW', country_codes[match[2]]] + probs choices = [(country_codes[match[1]], probs[0]), ('DRAW', probs[1]), (country_codes[match[2]], probs[2])] prediction = weighted_choice(choices) if prediction=='DRAW': prediction_dict["match_{0}".format(k)] = prediction else: prediction_dict["match_{0}".format(k)] = country_from_codes[prediction] df = pd.DataFrame(probailities[3:], probailities[:3]) df.columns = ['probability'] plot_probs = Bar(df, toolbar_location=None, plot_width=200, plot_height=200, agg='mean') plot_probs.x_range = FactorRange(factors=probailities[:3]) plot_dict["match_{0}".format(k)] = plot_probs gp_A = dict.fromkeys(["France","Romania","Albania","Switzerland"], 0) gp_B = dict.fromkeys(["England","Russia","Wales","Slovakia"], 0) gp_C = dict.fromkeys(["Germany","Ukraine","Poland","Northern Ireland"], 0) gp_D = dict.fromkeys(["Spain","Czech Republic","Turkey","Croatia"], 0) gp_E = dict.fromkeys(["Belgium","Italy","Republic of Ireland","Sweden"], 0) gp_F = dict.fromkeys(["Portugal","Iceland","Austria","Hungary"], 0) for k, match in enumerate(grp_A_matches,1): result = prediction_dict["match_{0}".format(k)] if result == 'DRAW': gp_A[match[1]] += 1 gp_A[match[2]] += 1 else: gp_A[result] += 3 for k, match in enumerate(grp_B_matches,7): result = prediction_dict["match_{0}".format(k)] if result == 'DRAW': gp_B[match[1]] += 1 gp_B[match[2]] += 1 else: gp_B[result] += 3 for k, match in enumerate(grp_C_matches,13): result = prediction_dict["match_{0}".format(k)] if result == 'DRAW': gp_C[match[1]] += 1 gp_C[match[2]] += 1 else: gp_C[result] += 3 for k, match in enumerate(grp_D_matches,19): result = prediction_dict["match_{0}".format(k)] if result == 'DRAW': gp_D[match[1]] += 1 gp_D[match[2]] += 1 else: gp_D[result] += 3 for k, match in enumerate(grp_E_matches,25): result = prediction_dict["match_{0}".format(k)] if result == 'DRAW': gp_E[match[1]] += 1 gp_E[match[2]] += 1 else: gp_E[result] += 3 for k, match in enumerate(grp_F_matches,31): result = prediction_dict["match_{0}".format(k)] if result == 'DRAW': gp_F[match[1]] += 1 gp_F[match[2]] += 1 else: gp_F[result] += 3 all_groups = [gp_A,gp_B,gp_C,gp_D,gp_E,gp_F] gp_labels = {1:"A",2:"B",3:"C",4:"D",5:"E",6:"F"} table_html = {} for k, group in enumerate(all_groups,1): table_string = [' <table style="width:100%">'] group_standings = {'team':[], 'points':[]} for j in xrange(1,len(group)+1): table_string.append(' <tr>') top = max(group, key=group.get) points = group.get(top) group_standings['team'].append(top) group_standings['points'].append(points) group.pop(top) table_string.append(' <td>{0}</td>'.format(j)) table_string.append(' <td>{0}</td>'.format(top)) table_string.append(' <td>{0}</td>'.format(points)) table_string.append(' </tr>') table_string.append(' </table>') table_html["gp_{0}".format(gp_labels[k])] = '\n'.join(table_string) script, div = components(plot_dict) return render_template('euro.html', js_resources=js_resources, script=script, div=div, pred=prediction_dict, table=table_html)
def plot_lcoe(top): # all this can probably be done smarter with a Pandas DataFrame?! Any takers? aep = top.fin_a.net_aep fcr = top.fin_a.fixed_charge_rate turbine_lcoe = OrderedDict(Rotor=(fcr / aep * top.tcc_a.tcc.blade_cost * top.tcc_a.tcc.blade_number + fcr / aep * top.tcc_a.tcc.hub_system_cost) * top.turbine_number, Tower=fcr / aep * top.tcc_a.tcc.tower_cost * top.turbine_number, Nacelle=fcr / aep * top.tcc_a.tcc.nacelle_cost * top.turbine_number) infra_lcoe = OrderedDict(Assembly=fcr / aep * top.bos_breakdown.assembly_and_installation_costs, Development=fcr / aep * top.bos_breakdown.development_costs, Electrical=fcr / aep * top.bos_breakdown.electrical_costs, Substructure=fcr / aep * top.bos_breakdown.foundation_and_substructure_costs, Other=fcr / aep * top.bos_breakdown.foundation_and_substructure_costs, Preparation=fcr / aep * top.bos_breakdown.preparation_and_staging_costs, Soft=fcr / aep * top.bos_breakdown.soft_costs, Transportation=fcr / aep * top.bos_breakdown.transportation_costs) opex_lcoe = OrderedDict(Opex=top.opex_a.avg_annual_opex / aep) turbine_sum = np.sum(turbine_lcoe.values()) infra_sum = np.sum(infra_lcoe.values()) opex_sum = np.sum(opex_lcoe.values()) total_lcoe = np.array([turbine_sum, infra_sum, opex_sum]) # array containing Turbine, BOS, and OPEX lcoe costs lcoe = total_lcoe.sum() total_lcoe = OrderedDict(Total=lcoe) everything_lcoe = turbine_lcoe everything_lcoe.update(infra_lcoe) everything_lcoe.update(opex_lcoe) cumSum = 0 invisibleBlock = OrderedDict() for key in everything_lcoe.keys(): invisibleBlock[key] = cumSum cumSum += everything_lcoe[key] everything_lcoe.update(total_lcoe) invisibleBlock['Total'] = 0 Combined = invisibleBlock.values() colors = ['white' for i in range(len(Combined))] next_color = palette.next() for i in range(3): colors.append(next_color) next_color = palette.next() for i in range(8): colors.append(next_color) colors.append(palette.next()) colors.append('black') myGroup=[] for x in range(2): for i in range(3): myGroup.append('turbine') for i in range(8): myGroup.append('infrastructure') myGroup.append('opex') myGroup.append('total') for stuff in everything_lcoe.values(): Combined.append(stuff) myKeys = OrderedDict(turbine=turbine_lcoe.keys(), infra=infra_lcoe.keys(), myOpex=opex_lcoe.keys()) myNames = myKeys['turbine'] for stuff in list(myKeys['turbine']): myNames.append(stuff) myStack = [] for i in range(13): myStack.append(1) for i in range(13): myStack.append(2) myDict = dict(Amount=Combined, Group=myGroup, Names=myNames, stack=myStack, color=colors) myDF = df(data=myDict) # print myDF myBar = Bar(myDF, values='Amount', label=CatAttr(columns=['Names'], sort=False), stack="stack", toolbar_location="above", color="color", ygrid=False, legend=None, title='LCOE Costs Breakdown', ylabel="LCOE ($/kWh)", xlabel="Component", tools="crosshair,pan,wheel_zoom,box_zoom,reset,hover,previewsave", ) hover = myBar.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("Component","@Names"), # ("Group", "@Group"), # maybe one day bokeh will fix this and it will work. It should show "turbine, infra, or opex" ]) return myBar
from bokeh.charts import Bar, output_file, show, vplot, hplot, defaults from bokeh.sampledata.autompg import autompg as df df['neg_mpg'] = 0 - df['mpg'] defaults.plot_width = 400 defaults.plot_height = 400 bar_plot = Bar(df, label='cyl', title="label='cyl'") bar_plot.title_text_font_size = '10pt' bar_plot2 = Bar(df, label='cyl', bar_width=0.4, title="label='cyl' bar_width=0.4") bar_plot2.title_text_font_size = '10pt' bar_plot3 = Bar(df, label='cyl', values='mpg', agg='mean', title="label='cyl' values='mpg' agg='mean'") bar_plot3.title_text_font_size = '10pt' bar_plot4 = Bar(df, label='cyl', title="label='cyl' color='DimGray'", color='dimgray') bar_plot4.title_text_font_size = '10pt' # multiple columns bar_plot5 = Bar(df, label=['cyl', 'origin'], values='mpg', agg='mean', title="label=['cyl', 'origin'] values='mpg' agg='mean'") bar_plot5.title_text_font_size = '10pt' bar_plot6 = Bar(df, label='origin', values='mpg', agg='mean', stack='cyl', title="label='origin' values='mpg' agg='mean' stack='cyl'", legend='top_right') bar_plot6.title_text_font_size = '10pt'
def summarize_plans(state, age, npi): todaysdate = str(datetime.now()) age = str(age) if age > '65': age = '65' elif age < '20': age = '20' # filter based on what plans are current (not expired) and age filteredplans = pd.read_hdf('webapp/data/plan-rates.h5', state, where=['(Age==age) & (RateExpirationDate > todaysdate) & (RateEffectiveDate < todaysdate)'], columns = ['IndividualRate', 'MetalLevel', 'Age','URLForSummaryofBenefitsCoverage','PlanMarketingName']) stateave = filteredplans.IndividualRate.mean() myave = stateave # plot it #filteredplans.groupby('MetalLevel').IndividualRate.mean().plot(kind='bar') #statebardf = filteredplans.groupby('MetalLevel').IndividualRate.mean() statebardf = filteredplans.groupby('MetalLevel', as_index=False).mean() #p = mpl.to_bokeh() if npi == '': p = Bar(filteredplans.groupby('MetalLevel').IndividualRate.mean(), values='IndividualRate', xlabel="", ylabel="Montly Premium ($)") else: print 'input npi is ' + npi # read hdf5 of provider-plan pairings provnplanpd = pd.read_hdf('webapp/data/plan_providers.h5', state) # make a list of planids for the input npi planlst = provnplanpd[provnplanpd.npi == npi].plan_id.values print planlst # read in hdf5 of plan info planinfo = pd.read_hdf('webapp/data/plan-rates.h5', state, where=['(Age==age) & (RateExpirationDate > todaysdate) & (RateEffectiveDate < todaysdate)']) # for each planid, get the first entry and concatenate them together. filteredplans = planinfo[planinfo.PlanId==planlst[0]].groupby('PlanId', as_index=False).first() filteredplans.head() #pd.concat([filteredplans, filteredplans]) for ii in range(1, len(planlst)): filteredplans = pd.concat([filteredplans, planinfo[planinfo.PlanId==planlst[ii]].groupby('PlanId', as_index=False).first()]) provbardf = filteredplans.groupby('MetalLevel', as_index=False).mean() provbardf['average'] = ['provider rates']*len(provbardf.MetalLevel.values) statebardf['average'] = ['state average']*len(provbardf.MetalLevel.values) myave = statebardf.IndividualRate.mean() #provbardf['average'] = [1, 1] #statebardf['average'] = [2, 2] #plotdf = pd.merge(statebardf, provbardf, on='MetalLevel') plotdf = pd.concat([provbardf, statebardf]) #plotdf['state average'] = statebardf['IndividualRate'] #plotdf['provider rates'] = provbardf['IndividualRate'] print plotdf p = Bar(plotdf, label='MetalLevel', values='IndividualRate', group='average', legend='top_right', xlabel="", ylabel="Montly Premium ($)") p.logo = None p.plot_height=400 p.toolbar_location = None script,div = components(p) print filteredplans.to_dict(orient='records') return {'num_plans':len(filteredplans), 'script': script, 'plot_div': div, 'national_comp': format_price_comp(float(age2nationalAverage(age))-myave), 'state_comp': format_price_comp(float(stateave)-myave), 'plans': render_template('plans.html', plans=filteredplans.sort_values(by='IndividualRate').to_dict(orient='records'))}
from bokeh.charts import Bar, output_file, show, vplot from numpy.random import rand from pandas import DataFrame N = 10 data = DataFrame({'A': rand(N), 'B': rand(N), 'C': rand(N)}) # Stack columns A,B,C and convert the multiindices to columns sdata = data.stack().reset_index() sdata.columns = ['labels', 'stack', 'values'] bar = Bar(sdata, values='values', label='labels', stack='stack', legend='top_right') bar2 = Bar(sdata, values='values', label='labels', stack='stack', legend='top_right') bar2.x_range = bar.x_range # Link the x axes output_file("stacked_bar.html") show(vplot(bar, bar2))
def comparison(request, stock_id1, stock_id2): stock1 = get_object_or_404(Stock, pk=stock_id1) stock2 = get_object_or_404(Stock, pk=stock_id2) #update(stock1) #update(stock2) # Graph # Last known weekday current_day = weekday().isoformat() # Retrieve live data YYYY-MM-DD historical_price1 = ystockquote.get_historical_prices(stock1, '2013-01-24', current_day) correct_order1 = sorted(historical_price1) stock_prices1 = [] dates1 = [] for values in correct_order1: stock_prices1.append(historical_price1[values]['Adj Close']) dates1.append(values) # Convert to Float for p in range(len(stock_prices1)): stock_prices1[p] = float(stock_prices1[p]) # Convert to Datetime Format dates_objects1 = [] for d in dates1: dates_objects1.append(datetime.strptime(d, '%Y-%m-%d')) # Retrieve live data YYYY-MM-DD historical_price2 = ystockquote.get_historical_prices(stock2, '2013-01-24', current_day) correct_order2 = sorted(historical_price2) stock_prices2 = [] dates2 = [] for values in correct_order2: stock_prices2.append(historical_price2[values]['Adj Close']) dates2.append(values) # Convert to Float for p in range(len(stock_prices2)): stock_prices2[p] = float(stock_prices2[p]) # Convert to Datetime Format dates_objects2 = [] for d in dates2: dates_objects2.append(datetime.strptime(d, '%Y-%m-%d')) #Tools hover = HoverTool(mode='vline') crosshair = CrosshairTool(dimensions=['height']) TOOLS = [BoxZoomTool(), crosshair, PreviewSaveTool(), ResetTool()] p1 = figure(x_axis_type="datetime", responsive=True, plot_height=250,tools=TOOLS) p1.border_fill = "whitesmoke" # Multiple Axises min1 = min(stock_prices1) max1 = max(stock_prices1) min2 = min(stock_prices2) max2 = max(stock_prices2) p1.y_range = Range1d(start= min1- (min1/10),end=max1+ (min1/10)) p1.extra_y_ranges = {'range2':Range1d(start= min2- (min2/10),end=max2+ (min2/10))} p1.add_layout(LinearAxis(y_range_name="range2"), 'right') p1.line(np.array(dates_objects1, dtype=np.datetime64), stock_prices1, color='#b41f2e', legend=stock1.ticker) p1.line(np.array(dates_objects2, dtype=np.datetime64), stock_prices2, y_range_name='range2', color='#1F78B4', legend=stock2.ticker) p1.grid.grid_line_alpha = 0.3 p1.xaxis.axis_label = 'Date' p1.yaxis.axis_label = 'Price' script, div = components(p1) # Bar Charts mc1 = float((stock1.market_cap[:-1])) mc2 = float((stock2.market_cap[:-1])) pe1 = float(stock1.pe_ratio) pe2 = float(stock2.pe_ratio) pb1 =float(stock1.pb_ratio) pb2 = float(stock2.pb_ratio) vol1 = float(stock1.volume) vol2 = float(stock2.volume) rev1 = float((stock1.revenue[:-1])) rev2 = float((stock2.revenue[:-1])) peg1 = float(stock1.peg) peg2 = float(stock2.peg) mc_dict= {'tickers':[stock1.ticker,stock2.ticker], 'market cap':[mc1,mc2], 'PB Ratio':[pb1,pb2], 'PE Ratio':[pe1,pe2], 'Volume': [vol1,vol2], 'Revenue': [rev1,rev2], 'PE Growth': [peg1,peg2] } #Hover Tools - Not working #hover_mc = HoverTool(tooltips=[('Market Cap', '@height'), ('Ticker', '@cat'), ]) mc_bar = Bar(mc_dict,width=250, height=250,values='market cap',label='tickers', color=color(columns='market cap',palette=['#b41f2e','#1F78B4']), title="Market Cap", ylabel='Billions') vol_bar = Bar(mc_dict, width=250, height=250,values='Volume', label='tickers', color=color(columns='Volume',palette=['#b41f2e','#1F78B4']), title="Volume",ylabel='Volume') pb_bar = Bar(mc_dict, width=250, height=250,values='PB Ratio', label='tickers', color=color(columns='PB Ratio',palette=['#b41f2e','#1F78B4']),title="PB Ratio",ylabel='Ratio') pe_bar = Bar(mc_dict, width=250, height=250,values='PE Ratio', label='tickers', color=color(columns='PE Ratio',palette=['#b41f2e','#1F78B4']), title="PE Ratio",ylabel='Ratio') peg_bar = Bar(mc_dict, width=250, height=250, values='PE Growth', label='tickers', color=color(columns='PE Growth', palette=['#b41f2e', '#1F78B4']), title="PE Growth", ylabel='Percentage(%)') rev_bar = Bar(mc_dict, width=250, height=250, values='Revenue', label='tickers', color=color(columns='Revenue', palette=['#b41f2e', '#1F78B4']), title="Revenue", ylabel='Billions') mc_bar.border_fill_color = "whitesmoke" vol_bar.border_fill_color = "whitesmoke" pb_bar.border_fill_color = "whitesmoke" pe_bar.border_fill_color = "whitesmoke" peg_bar.border_fill_color = "whitesmoke" rev_bar.border_fill_color = "whitesmoke" grid = gridplot([[mc_bar,rev_bar, pe_bar],[vol_bar, pb_bar, peg_bar]]) mscript, mdiv = components(grid) context = {'stock1': stock1, 'stock2': stock2, 'the_script': script, 'the_div': div, 'mc1': mscript, 'mc2': mdiv, } return render(request, 'stocktracker/comparison.html', context)
for elem in list_of_elems: first = elem[0] second = elem[1] list_of_words.append(first) list_of_num.append(second) return list_of_words, list_of_num a = [(u'colada', 1.06465), (u'mmmmmmmmmmmmmmmmmmmmm', 0.85172), (u'brunched', 0.85172), (u'combinacion', 0.85172), (u'altar', 0.85172), (u'heckled', 0.70977), (u'guitarist', 0.70977), (u'benched', 0.60837), (u'suprisingly', 0.60837), (u'jombot', 0.60837), (u'cpt', 0.53232), (u'pubgrub', 0.53232), (u'prepaired', 0.53232), (u'bestpapersonaltrainercom', 0.53232), (u'touristic', 0.47318), (u'delucas', 0.47318), (u'stickler', 0.47318), (u'parfaits', 0.47318), (u'mochas', 0.45543), (u'bearclaw', 0.42586), (u'isntead', 0.42586), (u'portapottys', 0.42586), (u'centerpieces', 0.42586), (u'newcastle', 0.42586), (u'anf', 0.42586), (u'hollywoodlanes', 0.42586), (u'hideaway', 0.38715), (u'haiku', 0.38715), (u'muffaletta', 0.38715), (u'anazing', 0.38715),(u'balboa', 0.06347), (u'novak', 0.06342), (u'aamco', 0.06337), (u'faded', 0.06317), (u'jojo', 0.06312), (u'unos', 0.06309), (u'proves', 0.06309), (u'refurbish', 0.06309), (u'fleet', 0.06309), (u'koh', 0.06309)] list_of_words, list_of_num = parse_elements(a) model = {'TF-IDF': list_of_num, 'Words': list_of_words} df1 = df(data = model, columns = ['TF-IDF','Words']) graph1 = Bar(df1, 'Words', values = 'TF-IDF', title = "TF-IDF of top positive words", ylabel = 'TF-IDF scores', width = 800, height = 400, color = 'blue') graph1.left[0].formatter.use_scientific= False graph1.x_range = FactorRange(factors=df1['Words'].tolist()) c = [(u'hoofah', 4.2586), (u'ewwww', 4.2586), (u'disconnected', 0.70977), (u'precooked', 0.70977), (u'crimes', 0.60837), (u'greasiest', 0.60837), (u'redevelopment', 0.60837), (u'sloooooow', 0.53232), (u'yucky', 0.53232), (u'horrifying', 0.53232), (u'rancho', 0.47318), (u'crisco', 0.47318), (u'grabn', 0.42586), (u'gnocci', 0.42586), (u'strictest', 0.38715), (u'clout', 0.38715), (u'unenjoyable', 0.38715), (u't3', 0.36502), (u'terriable', 0.32758), (u'foe', 0.32758), (u'pararell', 0.32758), (u'thrifters', 0.32758), (u'society', 0.30419), (u'cremation', 0.28391), (u'fabricated', 0.28391), (u'yiengling', 0.28391), (u'delish', 0.27475), (u'uncover', 0.26616), (u'm8n', 0.26616), (u'pepparoni', 0.26616),(u'drops', 0.03097), (u'scheduling', 0.03097), (u'towed', 0.03089), (u'stark', 0.03086), (u'bjork', 0.03086), (u'arrogance', 0.03086), (u'maitre', 0.03086), (u'electronica', 0.03086), (u'basing', 0.03086), (u'caters', 0.03086)] list_of_words2, list_of_num2 = parse_elements(c) model2 = {'TF-IDF': list_of_num2, 'Words': list_of_words2} df2 = df(data = model2, columns = ['TF-IDF','Words']) graph2 = Bar(df2, 'Words', values = 'TF-IDF', title = "TF-IDF of top negative words", ylabel = 'TF-IDF scores', width = 800, height = 400, color = 'red') graph2.left[0].formatter.use_scientific= False graph2.x_range = FactorRange(factors=df2['Words'].tolist()) output_file("swag1.html")
dstat[dname] = grp # later, we build a dict containing the grouped data ["negative","neutral","positive"] posavglist = [] negavglist = [] namelist = [] for k,v in dstat.items(): posavglist.append(list(v.poscount)) negavglist.append(list(v.negcount)) namelist.append([k+"_neg",k+"_neu",k+"_pos"]) counts = OrderedDict(poscount=posavglist, negcount=negavglist) bar = Bar(counts, namelist , filename="counts.html") bar.title("Occurrence of positive and negative counts").xlabel("class").ylabel("count") bar.legend(True).width(900).height(400).stacked(True) bar.show()