Dash Plotly px.line 图表:简化 Hover 信息显示
在 Dash Plotly 的 px.line 图表中,使用 hovertemplate 参数可以简化多条线的某些值不在 Hover 信息中显示。给定一组数据,如果不希望在 Hover 信息中显示某个值,可以将其设置为 ' %{text} ' 或 ''(空字符串)。
例如,以下代码将在 Hover 信息中仅显示 x 和 y 的值:
import plotly.express as px
import pandas as pd
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y1': [1, 3, 2, 4, 3],
'y2': [2, 4, 3, 5, 4],
'y3': [3, 5, 4, 6, 5]
})
fig = px.line(df, x='x', y=['y1', 'y2', 'y3'], hover_name='x', hover_data={'y1':True, 'y2':True, 'y3':False},
hovertemplate='x: %{x}<br>y1: %{y1}<br>y2: %{y2}<br>y3: %{text}')
fig.update_traces(hovertemplate='%{x}<br>%{y}')
fig.show()
在这个例子中,我们使用 hover_data 参数来指定哪些值应该在 Hover 信息中显示,然后使用 hovertemplate 参数来设置显示哪些值。我们将不想在 Hover 信息中显示的 y3 的值设置为 ' %{text} ',因此它不会在 Hover 信息中显示。最后,我们使用 update_traces 方法来设置所有线都应该在 Hover 信息中显示的值。
原文地址: https://www.cveoy.top/t/topic/nic9 著作权归作者所有。请勿转载和采集!