Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plotly hover data and hover text

My input:

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']}))

My code:

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()

My output:
enter image description here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As you can see, I create some custom plot within bar chart. And now I try make some "glitter", but a little bit confused. What I want&expect: I try hide in popup (hover text) any x=1, variable=quality_value (i.e values from x and y axis) and just leave
only labels from hover_data within custom name not like now (hover_data_0 and label)

>Solution :

You can use fig.update_traces(hoverdata) with the following setup:

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

Plot 1:

enter image description here

Complete code:

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading