def get_states_plot():
source = AjaxDataSource(
data={'STATE': [], 'STNAME': [], 'STUSAB': [], 'TOT_POP': [], 'TOT_MALE': [], 'TOT_FEMALE': []},
data_url='/api/states/', mode='replace', method='GET')
hover = HoverTool(
tooltips=[
("State", "@STNAME"),
("Population", "@TOT_POP"),
("Female Population", "@TOT_FEMALE"),
("Male Population", "@TOT_MALE"),
]
)
plot = figure(title='Population by State', plot_width=1200, plot_height=500,
x_range=FactorRange(factors=get_state_abbreviations()), y_range=(0, 40000000),
tools=[hover, 'tap','box_zoom','wheel_zoom','save','reset'])
plot.toolbar.active_tap = 'auto'
plot.xaxis.axis_label = 'State'
plot.yaxis.axis_label = 'Population'
plot.yaxis.formatter = NumeralTickFormatter(format="0a")
plot.sizing_mode = 'scale_width'
plot.vbar(bottom=0, top='TOT_POP', x='STUSAB', legend=None, width=0.5, source=source)
url = "/counties/@STATE/"
taptool = plot.select(type=TapTool)
taptool.callback = OpenURL(url=url)
return plot
python类TapTool()的实例源码
def bokeh_scatter_plot(tsne_df):
output_file("results\\Anime_similarity.html")
# Prep plot
plot_anime_sim = bp.figure(plot_width=700, plot_height=600, title="Anime Similarity plotted with tSNE",
tools="pan,wheel_zoom,box_zoom,reset,hover,previewsave,tap",
x_axis_type=None, y_axis_type=None, toolbar_location="below",
toolbar_sticky=False)
# Plotting the anime data
plot_anime_sim.scatter(x='x', y='y', source=tsne_df)
# Handle hover tools
hover = plot_anime_sim.select(dict(type=HoverTool))
hover.tooltips={"Anime":"@anime_name [@rating]"}
# Add ability to click links:
url = "http://www.myanimelist.net/anime/@anime_id"
taptool = plot_anime_sim.select(type=TapTool)
taptool.callback = OpenURL(url=url)
# Show the file:
show(plot_anime_sim)
script, div = components(plot_anime_sim)
with open("results\\sim_script.js", "w") as text_file:
text_file.write(script[37:-10])
with open("results\\sim_html.html", "w") as text_file:
text_file.write(div)
#%% Main code: