Closed
Description
Hello!
I didn't find this issue reported yet. When I add columns from a dataframe of type category to the hover_data of a plotly express figure, codes instead of the categories are shown.
It took me a while to figure this out, because I had numeric categories close to their codes. This is an easy pitfall. Once you remove a category, the numbers aren't as expected anymore, because the codes changed.
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'x': [1,2,1,2,1,2],
'y': [1,1,2,2,3,3],
'user': [1,1,2,2,3,3],
'color': ['color1', 'color1', 'color2', 'color2', 'color3', 'color3'],
'foo': ['a','b','a','b','a','b'],
'bar': [2,2,2,3,3,3]})
df[['user', 'color', 'foo', 'bar']] = df[['user', 'color', 'foo', 'bar']].astype('category')
# Fails to show foo and bar categories. Shows codes instead.
hover1 = {'x': False, 'foo': True, 'bar': True}
fig1 = px.line(data_frame=df, x='x', y='y', line_group='user', color='color', hover_data=hover1)
fig1.show()
# Workaround.
hover2 = {'x': False, 'foo_': (True, df['foo'].values), 'bar_': (True, df['bar'].values)}
fig2 = px.line(data_frame=df, x='x', y='y', line_group='user', color='color', hover_data=hover2, labels={'foo_': 'foo', 'bar_': 'bar'})
fig2.show()
plotly v4.9.0
Metadata
Metadata
Assignees
Labels
No labels