Show non-numerical variables / columns in hover text
Question:
I have this code to create a plot and this dataframe
df2.dtypes
Out[464]:
Device category
time datetime64[ns]
Path category
variable object
value float64
path category
dtype: object
df3 = df2.query('variable==@signals').groupby('path').first()
fig_general = px.scatter(df3
, x = "time"
, y = 'value'
, custom_data = ['Path']
, color = 'Device'
, symbol = 'variable'
, hover_name = "Path"
, opacity = 0.6
, template = 'plotly_dark'
, marginal_y = "rug"
)
fig_general.update_layout(
transition_duration = 500
, autosize = True
, height = 700
, hovermode = 'closest'
)
fig_general.update_traces(marker = dict(
size = 14
)
, hovertemplate="<br>".join([
"Device: %{Device}"
, "variable': {symbol}"
, "time: %{x}"
, "value': %{y}"
])
)
And I would like to show variable
and Device
in the hover text but it is only shown as text itself. time
and value
is working properly. How do I do that?
Answers:
All you need is to add Device data to the customdata
like so:
fig_general.update_traces(marker=dict(size=14),
customdata=df3[['variable', 'Device']].values,
hovertemplate="<br>".join([
"Device: %{customdata[1]}",
"variable: %{customdata[0]}",
"time: %{x}",
"value: %{y}",
])
)
I have this code to create a plot and this dataframe
df2.dtypes
Out[464]:
Device category
time datetime64[ns]
Path category
variable object
value float64
path category
dtype: object
df3 = df2.query('variable==@signals').groupby('path').first()
fig_general = px.scatter(df3
, x = "time"
, y = 'value'
, custom_data = ['Path']
, color = 'Device'
, symbol = 'variable'
, hover_name = "Path"
, opacity = 0.6
, template = 'plotly_dark'
, marginal_y = "rug"
)
fig_general.update_layout(
transition_duration = 500
, autosize = True
, height = 700
, hovermode = 'closest'
)
fig_general.update_traces(marker = dict(
size = 14
)
, hovertemplate="<br>".join([
"Device: %{Device}"
, "variable': {symbol}"
, "time: %{x}"
, "value': %{y}"
])
)
And I would like to show variable
and Device
in the hover text but it is only shown as text itself. time
and value
is working properly. How do I do that?
All you need is to add Device data to the customdata
like so:
fig_general.update_traces(marker=dict(size=14),
customdata=df3[['variable', 'Device']].values,
hovertemplate="<br>".join([
"Device: %{customdata[1]}",
"variable: %{customdata[0]}",
"time: %{x}",
"value: %{y}",
])
)