繁体   English   中英

Plotly hover 数据和 hover 文本

[英]Plotly hover data and hover text

我的输入:

df=(pd.DataFrame({'label_color':['white','white','cyan','cyan','cyan','cyan','white','white'],
                  'label_quality':['white','white','red','green','green','red','white','white'],
'label':['foo','foo','foo','foo','foo','foo','foo','foo']}))

我的代码:

df['color_value'] = 1
df['quality_value'] = 1

fig = px.bar(df, y=['color_value','quality_value'],
             x=[1]*len(df),
             orientation='h',
             barmode='group',
             template='plotly_white', hover_data=[df.index.values,df.label])

fig.data[0]['marker']['color'] = df['label_color'].tolist()
fig.data[1]['marker']['color'] = df['label_quality'].tolist()
fig.update_traces(marker_line_color='rgb(8,48,107)')
fig.update_layout(showlegend=False, yaxis_title='foo', xaxis_title='')
fig.show()

我的 output: 在此处输入图像描述 .

如您所见,我在条形图中创建了一些自定义 plot。 现在我尝试制作一些“闪光”,但有点困惑。 想要的和期望的:我尝试在弹出窗口(悬停文本)中隐藏任何x=1variable=quality_value (即来自 x 和 y 轴的值),并且只在自定义名称中保留来自hover_data的标签,而不像现在这样( hover_data_0label

您可以将fig.update_traces(hoverdata)与以下设置一起使用:

fig.update_traces(hovertemplate = 'hover_data_0=%{customdata[0]}<br>label=%{customdata[1]}<extra></extra>')

Plot 1:

在此处输入图像描述

完整代码:

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

df=(pd.DataFrame({'label_color':['white','white','cyan','cyan','cyan','cyan','white','white'],
                  'label_quality':['white','white','red','green','green','red','white','white'],
'label':['foo','foo','foo','foo','foo','foo','foo','foo']}))

df['color_value'] = 1
df['quality_value'] = 1

fig = px.bar(df, y=['color_value','quality_value'],
             x=[1]*len(df),
             orientation='h',
             barmode='group',
             template='plotly_white', hover_data=[df.index.values,df.label])

fig.data[0]['marker']['color'] = df['label_color'].tolist()
fig.data[1]['marker']['color'] = df['label_quality'].tolist()
fig.update_traces(marker_line_color='rgb(8,48,107)')
fig.update_layout(showlegend=False, yaxis_title='foo', xaxis_title='')
f = fig.full_figure_for_development(warn=False)
fig.update_traces(hovertemplate = 'hover_data_0=%{customdata[0]}<br>label=%{customdata[1]}<extra></extra>')
fig.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM