Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Format hover_data. Is this possible? #115

Closed
indiana-nikel opened this issue Jul 10, 2019 · 4 comments
Closed

Format hover_data. Is this possible? #115

indiana-nikel opened this issue Jul 10, 2019 · 4 comments

Comments

@indiana-nikel
Copy link

indiana-nikel commented Jul 10, 2019

Hello,

I couldn't seem to find this in the docs. Is it possible to format hover_data as a string for display purposes only? My use case comes from displaying dollar amounts, but the original data is float.64 so hovering over a data point does not look appealing.

My current fix requires an extra column to be added to a data frame, then that column is passed to the hover_data argument:

df['hover_text'] = df['revenue'].apply(lambda x :'${0:,.0f}'.format(x))

However, this is not a great solution. My knowledge is pretty limited with plotly so there may be better solutions I haven't been able to find currently. Perhaps with hovertemplate?

Best,
Indiana

@indiana-nikel indiana-nikel changed the title Format hover_data, potential to expose hovertemplate. Format hover_data. Possible? Jul 10, 2019
@indiana-nikel indiana-nikel changed the title Format hover_data. Possible? Format hover_data. Is this possible? Jul 10, 2019
@nicolaskruchten
Copy link
Contributor

Your solution is probably what I would go with, to be honest. The way hover_data is injected into the figure is via hovertemplate, and you can override hovertemplate with .update_trace(hovertemplate="whatever") if you like, but it's likely to be a bit finicky.

@indiana-nikel
Copy link
Author

Okay thank you for your quick response! Are there plans to expose this when creating a plot?

Eg.

px.bar(
   df, 
   x="Revenue", 
   y="Year",
   hover_data={
      "Revenue": "${0:,.0f}", 
      ...
   })

Sidenote: Since discovering this plotting library, it's hard to go back to anything else!

@nicolaskruchten
Copy link
Contributor

I'm glad you like it! The API you suggest above is pretty interesting, I'll think about it. The formatting strings would have to be D3.js-compatible rather than Python-compatible as the formatting happens in Javascript but other than that this seems like a nice extension.

@nicolaskruchten
Copy link
Contributor

Will be completed in plotly/plotly.py#1774

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants