plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.02k stars 2.54k forks source link

hover_data in PX doesn't accept df.index #2760

Open nicolaskruchten opened 4 years ago

nicolaskruchten commented 4 years ago

In wide-mode I can't do this:

import plotly.express as px
df = px.data.stocks(indexed=True)
fig = px.line(df, hover_data={df.index: "|%B %d, %Y"} )

or find any other way of referring to the index.

emmanuelle commented 4 years ago

The syntax is

import plotly.express as px
df = px.data.stocks()
fig = px.line(df, x=df.date, y=df.columns[1:], hover_data={'index': ("|%B %d, %Y", df.index)} )
fig.show()

Is this what you were asking? The keys of the hover_data dict need to be str or numbers.

nicolaskruchten commented 4 years ago

So that works to add a new entry to the hoverlabel with label "index" but in my use-case is that in wide-form the index of df is used by default (in this case it's called "date") and I can't format that hover entry:

image

nicolaskruchten commented 4 years ago

Ah but you're totally right, this does work:

import plotly.express as px
df = px.data.stocks(indexed=True)
fig = px.line(df, hover_data={"date": ("|%Y", df.index)})
fig.show()
nicolaskruchten commented 4 years ago

Or more generally: hover_data={df.index.name: ("|%Y", df.index)} ... pretty obscure tho! Not sure where to document that one.

imadcat commented 1 year ago

Or more generally: hover_data={df.index.name: ("|%Y", df.index)} ... pretty obscure tho! Not sure where to document that one.

this works for single index, what about multiple index?

rabyj commented 1 year ago

Or more generally: hover_data={df.index.name: ("|%Y", df.index)} ... pretty obscure tho! Not sure where to document that one.

Yes the reference to the general syntax for the formatting part of the tuple would be very appreciated. I finally found that the correct syntax for a literal string was {"index": (df.index)} but I had to fumble a lot.