0

How do I prevent the text values passed as text=df.petal_width.values in the example below from showing up in the hover tooltip? They should only display as annotations directly on the plot.

plot

import plotly.express as px

df = px.data.iris()

fig = px.scatter(
    df,
    x="sepal_length",
    y="sepal_width",
    color="species",
    text=df.petal_width.values,
)

fig.show()

To be clear, I know I could pass text=df.petal_width and the tooltip value would not be called text but petal_width. This is not what I want. I want it gone entirely. Only way I found so far is ugly:

hov_temp = [
    x for x in fig.data[0].hovertemplate.split("<br>") if not x.startswith("text")
]
fig.data[0].hovertemplate = "<br>".join(hov_temp)
Janosh
  • 3,392
  • 2
  • 27
  • 35

1 Answers1

1

You can pass a dict with labels to hover_data and text=df.petal_width:

labels = {'species':True,'sepal_length':True,'sepal_width':True,'petal_length':False,'petal_width':False}
fig = px.scatter(df,x='sepal_length',y='sepal_width',color='species',text=df.petal_width,hover_data=labels)
fig.show()

enter image description here

gmolnar
  • 108
  • 1
  • 6
  • Perfect, looks like just `labels = {'petal_width': False}` is enough. – Janosh Dec 02 '21 at 16:59
  • `text` can also be a column name. So `col='petal_width'` and `px.scatter(...,text=col,hover_data={col:False})` also works – gmolnar Dec 02 '21 at 18:46