def polynomial(): """ Very simple embedding of a polynomial chart """ # Grab the inputs arguments from the URL args = flask.request.args # Get all the form arguments in the url with defaults color = getitem(args, 'color', 'Black') _from = int(getitem(args, '_from', 0)) to = int(getitem(args, 'to', 10)) # Create a polynomial line graph with those arguments x = list(range(_from, to + 1)) fig = figure(title="Polynomial") fig.line(x, [i ** 2 for i in x], color=colors[color], line_width=2) resources = INLINE.render() script, div = components(fig) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, resources=resources, color=color, _from=_from, to=to ) return encode_utf8(html)
{{ script }} <style> .embed-wrapper { display: flex; justify-content: space-evenly; } </style> </head> <body> <div class="embed-wrapper"> {% for key in div.keys() %} {{ div[key] }} {% endfor %} </div> </body> </html> ''') resources = INLINE.render() filename = 'embed_multiple.html' html = template.render(resources=resources, script=script, div=div) with io.open(filename, mode='w', encoding='utf-8') as f: f.write(html) view(filename)
<title>Bokeh Scatter Plots</title> {{ resources }} {{ script }} <style> .embed-wrapper { display: flex; justify-content: space-evenly; } </style> </head> <body> <div class="embed-wrapper"> {% for key in div.keys() %} {{ div[key] }} {% endfor %} </div> </body> </html> ''') resources = INLINE.render() filename = 'embed_multiple.html' html = template.render(resources=resources, script=script, div=div) with io.open(filename, mode='w', encoding='utf-8') as f: f.write(html) view(filename)
def usptoPlot(uspto_final, orgName): uspto_dict = dict() uspto_dict['application'] = 0 uspto_dict['grant'] = 1 uspto_final = uspto_final.drop_duplicates([ '_version_', 'applicationDate', 'applicationNumber', 'applicationType', 'documentId', 'documentType', 'productionDate', 'publicationDate', 'title', 'year' ]) uspto_final['year'] = uspto_final['year'].astype('int') uspto_final['type_ind'] = uspto_final['documentType'].apply( lambda x: uspto_dict[x]) source_2 = ColumnDataSource(data=uspto_final) tooltips_2 = [("Title", "<strong>@title</strong>"), ("Application Date", "@applicationDate"), ("Application Type", "@applicationType"), ("Document Type", "@documentType"), ("Patent Number", "@patentNumber"), ("Publication Date", "@publicationDate"), ("year", "@year")] grant_types = ['application', 'grant'] p = figure(title=orgName + ' PATENTS', y_axis_label='YEAR', x_axis_label='PATENT', y_range=(int(uspto_final.year.min()) - 1, int(uspto_final.year.max()) + 1), x_range=grant_types, tooltips=tooltips_2, tools=['hover', 'tap'], plot_width=500, plot_height=550) url = "@pdfPath" taptool = p.select(type=TapTool) taptool.callback = OpenURL(url=url) color_mapper = CategoricalColorMapper(factors=list( uspto_final['title'].unique()), palette=all_palettes['Set1'][9]) p.scatter(x='documentType', y=jitter('year', 0.5), size=10, source=source_2, alpha=0.5, color={ 'field': 'title', 'transform': color_mapper }) p.title.text_font_size = "18pt" p.xaxis.axis_label_text_font_size = "16pt" p.yaxis.axis_label_text_font_size = "16pt" p.xaxis.major_label_text_font_size = "12pt" p.yaxis.major_label_text_font_size = "12pt" p.toolbar.logo = None p.toolbar_location = None source, div = components(p) resources = INLINE.render() return p
def plot_tweets(tweets, sentiment_scores): """ Create a histogram-style barplot of tweets and their sentiment. Return a bokeh plot object, expressed as a tuple of (resources, script, div). Where : resources: some CSS, etc. that goes in the head of the webpage for styling the plot. script: javascript for the plot to function. expressed as string. div: html div container for the plot. expressed as string. """ # Sort tweets from negative to positive. # This step is not strictly necessary, but makes it easier to see the overall shape of the data. sorted_indices = np.argsort(sentiment_scores) sentiment_scores = np.array(sentiment_scores)[sorted_indices] tweets = np.array(tweets)[sorted_indices] # Express the data as a bokeh data source object. source = ColumnDataSource(data={ "text": tweets, "sentiment": sentiment_scores, "x": np.arange(len(tweets)), }) """ Create plot. """ # Create plot object. width = 0.9 p = figure(x_axis_label="Tweet", y_axis_label="Sentiment (0 = Neutral)") p.vbar(source=source, x="x", top="sentiment", width=width) # Add hover tool, allowing mouseover to view text and sentiment. hover = HoverTool( tooltips=[ ("text", "@text"), ("sentiment", "@sentiment") ], formatters={ "text": "printf", "sentiment": "printf" }, mode="vline" ) p.add_tools(hover) """ Format plot. """ # axis font size p.xaxis.axis_label_text_font_size = "15pt" p.yaxis.axis_label_text_font_size = "15pt" # remove tick marks from axes p.xaxis.major_tick_line_color = None p.xaxis.minor_tick_line_color = None p.yaxis.major_tick_line_color = None p.yaxis.minor_tick_line_color = None # adjust plot width, height scale = 1.5 p.plot_height = int(250 * scale) p.plot_width = int(450 * scale) # remove toolbar (e.g. move, resize, etc) from right of plot. p.toolbar.logo = None p.toolbar_location = None # remove gridlines p.xgrid.visible = False p.ygrid.visible = False # remove x axis tick labels (done by setting label fontsize to 0 pt) p.xaxis.major_label_text_font_size = '0pt' """ Export plot """ # Create resources string, which is CSS, etc. that goes in the head of resources = INLINE.render() # Get javascript (script) and HTML div (div) for the plot. script, div = components(p) return (resources, script, div)
def modify_doc(cls): TOOLS = "pan,wheel_zoom,box_zoom,reset,save" season = Season() seasons_1990_on = season.seasons_1990_on data_dict = seasons_1990_on.to_dict('list') source = ColumnDataSource(data=data_dict) slider = Slider(title="Year", start=1990, end=2017, step=1, value=2006) menu_options_list = ["ALL"] + seasons_1990_on["Tm"].unique().tolist() team_menu = Select(options=menu_options_list, value="ALL", title="Team") columns = list(seasons_1990_on.columns) x_axis_menu = Select(options=columns, value="3PA", title="X Axis") y_axis_menu = Select(options=columns, value="3P", title="Y Axis") palette = season.get_palette() color_mapper = CategoricalColorMapper( factors=seasons_1990_on["Tm"].unique().tolist(), palette=palette) TOOLS = "pan,wheel_zoom,box_zoom,reset,save" p1 = figure(x_axis_label="3 Points Attempted", y_axis_label="3 Points Made", tools=TOOLS) ################### p1.circle( "X", "Y", source=source, alpha=0.8, nonselection_alpha=0.1, ) p1.legend.location = "bottom_right" #################### #################### ####################### cls.add_tooltips(p1) column1 = column(widgetbox(team_menu), widgetbox(slider), widgetbox(x_axis_menu), widgetbox(y_axis_menu)) layout = row(column1, p1) args = { 'source': source, 'data_dict': data_dict, 'team_menu': team_menu, 'slider': slider, 'x_axis_menu': x_axis_menu, 'y_axis_menu': y_axis_menu } callback = CustomJS(args=args, code=""" data_copy = JSON.parse(JSON.stringify(data_dict)) console.log(data_copy, "<-- Here is data copy"); if (team_menu.value === "ALL") { console.log("What's up. I am ALL"); } source.change.emit(); """) x_axis_menu.js_on_change('value', callback) resources = INLINE.render() script, div = components({'p': layout}) return {'script': script, 'div': div, 'resources': resources} def callback(attr, old, new): if menu.value == "ALL": new_df = seasons_1990_on[seasons_1990_on['Year'] == slider.value] new_x = new_df[x_axis_menu.value] new_y = new_df[y_axis_menu.value] else: new_df = seasons_1990_on[ (seasons_1990_on['Year'] == slider.value) & (seasons_1990_on['Tm'] == menu.value)] new_x = new_df[x_axis_menu.value] new_y = new_df[y_axis_menu.value] source.data['X'] = new_x source.data['Y'] = new_y new_df['X'] = new_x new_df['Y'] = new_y tooltips = [("Players", "@Player"), (x_axis_menu.value, "@{}".format(x_axis_menu.value))( y_axis_menu.value, "@{}".format(y_axis_menu.value))] source.data = new_df cls.add_tooltips(plot=p1, tooltips=tooltips) print("Here is x", new_x) print("Here is y", new_y) print("Here is y name", new_y.name) p1.xaxis.axis_label = new_x.name p1.yaxis.axis_label = new_y.name slider.on_change("value", callback) menu.on_change("value", callback) x_axis_menu.on_change("value", callback) y_axis_menu.on_change("value", callback)