如何与 plotly.figure_factory 悬停交互?
Posted
技术标签:
【中文标题】如何与 plotly.figure_factory 悬停交互?【英文标题】:How to interact with plotly.figure_factory hover? 【发布时间】:2021-05-05 17:47:58 【问题描述】:我尝试了以下代码:
import plotly.io as pio
import plotly.express as px
import json
import pandas as pd
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
df = px.data.carshare()
fig = go.Figure()
app = dash.Dash()
#fac figurile
fig = ff.create_hexbin_mapbox(df,lat = 'centroid_lat', lon = 'centroid_lon',nx_hexagon = 10,color = 'car_hours',
labels = 'color':'Point Count ',
opacity = 0.5)
fig.update_layout(mapbox_style="carto-darkmatter")
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.show()
它显示:
我想修改悬停,以便它只显示浮点值,悬停时只有第一个小数,我还希望能够在显示值后显示一些东西。例如,悬停时的值应为“点数 = 1019.9 辆汽车/小时。不幸的是,文档并没有太大帮助。
【问题讨论】:
【参考方案1】:在我看来,ff.create_hexbin_mapbox
的最佳选择是直接通过以下方式进行配置:
fig.data[0].hovertemplate = 'Point Count =%z:,.1f<extra>Cars per hour</extra>'
这会变成这样:
...进入这个:
完整代码
import plotly.io as pio
import plotly.express as px
import json
import pandas as pd
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
df = px.data.carshare()
fig = go.Figure()
# app = dash.Dash()
#fac figurile
fig = ff.create_hexbin_mapbox(df,lat = 'centroid_lat', lon = 'centroid_lon',nx_hexagon = 10,color = 'car_hours',
labels = 'color':'Point Count ',
opacity = 0.5)
fig.update_layout(mapbox_style="carto-darkmatter")
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.data[0].hovertemplate = 'Point Count =%z:,.1f<extra>Cars per hour</extra>'
fig.show()
【讨论】:
以上是关于如何与 plotly.figure_factory 悬停交互?的主要内容,如果未能解决你的问题,请参考以下文章