python交互式和文件式区别_Python中的交互式数据可视化与Bokeh(系列五)
使用圖例突出顯示數(shù)據(jù)
這將我們帶到本教程中的最終交互性示例:交互式圖例。
在“ 使用字形繪制數(shù)據(jù)”部分中,您了解了在創(chuàng)建繪圖時(shí)實(shí)現(xiàn)圖例是多么容易。有了這個(gè)傳奇,增加交互性只是分配一個(gè)問(wèn)題click_policy。使用單行代碼,您可以使用圖例快速添加任何一種hide或mute數(shù)據(jù)的功能。
在這個(gè)例子中,你會(huì)看到兩個(gè)相同的散點(diǎn)圖,比較勒布朗詹姆斯和凱文杜蘭特的比賽點(diǎn)數(shù)和籃板數(shù)。唯一的區(qū)別是,一個(gè)將使用a hide作為其click_policy,而另一個(gè)使用mute。
第一步是配置輸出并設(shè)置數(shù)據(jù),從player_statsDataFrame 為每個(gè)玩家創(chuàng)建一個(gè)視圖:
# Bokeh Librariesfrom bokeh.plotting import figure, showfrom bokeh.io import output_filefrom bokeh.models import ColumnDataSource, CDSView, GroupFilterfrom bokeh.layouts import row# Output inline in the notebookoutput_file('lebron-vs-durant.html', title='LeBron James vs. Kevin Durant')# Store the data in a ColumnDataSourceplayer_gm_stats = ColumnDataSource(player_stats)# Create a view for each playerlebron_filters = [GroupFilter(column_name='playFNm', group='LeBron'), GroupFilter(column_name='playLNm', group='James')]lebron_view = CDSView(source=player_gm_stats, filters=lebron_filters)durant_filters = [GroupFilter(column_name='playFNm', group='Kevin'), GroupFilter(column_name='playLNm', group='Durant')]durant_view = CDSView(source=player_gm_stats, filters=durant_filters)在創(chuàng)建圖形之前,可以將圖形,標(biāo)記和數(shù)據(jù)中的公共參數(shù)合并到字典中并重復(fù)使用。這不僅可以在下一步中節(jié)省冗余,而且還可以在以后需要時(shí)提供一種簡(jiǎn)單的方法來(lái)調(diào)整這些參數(shù):
# Consolidate the common keyword arguments in dictscommon_figure_kwargs = { 'plot_width': 400, 'x_axis_label': 'Points', 'toolbar_location': None,}common_circle_kwargs = { 'x': 'playPTS', 'y': 'playTRB', 'source': player_gm_stats, 'size': 12, 'alpha': 0.7,}common_lebron_kwargs = { 'view': lebron_view, 'color': '#002859', 'legend': 'LeBron James'}common_durant_kwargs = { 'view': durant_view, 'color': '#FFC324', 'legend': 'Kevin Durant'}現(xiàn)在已經(jīng)設(shè)置了各種屬性,可以以更簡(jiǎn)潔的方式構(gòu)建兩個(gè)散點(diǎn)圖:
# Create the two figures and draw the datahide_fig = figure(**common_figure_kwargs, title='Click Legend to HIDE Data', y_axis_label='Rebounds')hide_fig.circle(**common_circle_kwargs, **common_lebron_kwargs)hide_fig.circle(**common_circle_kwargs, **common_durant_kwargs)mute_fig = figure(**common_figure_kwargs, title='Click Legend to MUTE Data')mute_fig.circle(**common_circle_kwargs, **common_lebron_kwargs, muted_alpha=0.1)mute_fig.circle(**common_circle_kwargs, **common_durant_kwargs, muted_alpha=0.1)請(qǐng)注意,mute_fig有一個(gè)額外的參數(shù)調(diào)用muted_alpha。當(dāng)mute用作標(biāo)記時(shí),此參數(shù)控制標(biāo)記的不透明度click_policy。
最后,click_policy設(shè)置每個(gè)圖,它們以水平配置顯示:
# Add interactivity to the legendhide_fig.legend.click_policy = 'hide'mute_fig.legend.click_policy = 'mute'# Visualizeshow(row(hide_fig, mute_fig))
一旦傳說(shuō)中的地方,所有你需要做的是分配任一hide或mute到人物的click_policy屬性。這將自動(dòng)將您的基本圖例轉(zhuǎn)換為交互式圖例。
另請(qǐng)注意,特別是mute,LeBron James和Kevin Durant muted_alpha的相應(yīng)circle字形中設(shè)置了附加屬性。這決定了圖例互動(dòng)驅(qū)動(dòng)的視覺(jué)效果。
有關(guān)Bokeh中所有事物交互的更多信息,在Bokeh用戶(hù)指南中添加交互是一個(gè)很好的起點(diǎn)。
總結(jié)和后續(xù)步驟
恭喜!你已經(jīng)完成了本教程的結(jié)尾。
您現(xiàn)在應(yīng)該擁有一套很棒的工具來(lái)開(kāi)始使用Bokeh將數(shù)據(jù)轉(zhuǎn)換為漂亮的交互式可視化。
你學(xué)會(huì)了如何:
配置腳本以呈現(xiàn)為靜態(tài)HTML文件或Jupyter Notebook實(shí)例化和自定義figure()對(duì)象使用字形構(gòu)建可視化使用。訪(fǎng)問(wèn)和過(guò)濾您的數(shù)據(jù) ColumnDataSource在網(wǎng)格和選項(xiàng)卡式布局中組織多個(gè)圖添加不同形式的交互,包括選擇,懸停操作,鏈接和交互式圖例為了更好地探索Bokeh的功能,官方的Bokeh用戶(hù)指南是深入了解更高級(jí)主題的絕佳場(chǎng)所。我還建議您查看Bokeh的畫(huà)廊,了解大量的例子和靈感。
Bokeh 網(wǎng)站文檔https://bokeh.pydata.org/en/latest/docs/reference.html
總結(jié)
以上是生活随笔為你收集整理的python交互式和文件式区别_Python中的交互式数据可视化与Bokeh(系列五)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 登录日志怎么实现_【创新攻关】安全室构建
- 下一篇: css不换行属性_前端 | css di