pythonpandasdateplotlyhover

Display Full Date {DD-MM-YYYY} in Hover Text, python, plotly


I'm plotting a chart that spans multiple years using the python plotly library - import plotly.express as px

When the chart is displayed in full, the date that is shown in the hover text only includes the month and year M-YYYY.

enter image description here

Yet when it is zoomed in the full date is displayed M-DD-YYYY

enter image description here

Are there any settings in or related to hovertemplate or hovermode that will allow the full date to be displayed at all times?

This can be replicated in the following example:

import numpy as np
import pandas as pd
import plotly.express as px

start=pd.to_datetime("2016-01-01")
end=pd.to_datetime("2022-12-31")
length = end - start

df = pd.DataFrame({'Date':pd.date_range(start="2016-01-01",end="2022-12-31"),
                   'Value':np.random.randint(5,30,size=length.days+1)})
fig = px.area(df, x='Date', y="Value", 
              title = "Example Data",)

fig.update_traces(line_color='firebrick')
fig.update_layout(hovermode="x unified")

fig.show()

Solution

  • You need to supply the formatting string to apply to hover data, use hover_data :

    fig = px.area(df, x='Date', y="Value",
                  title="Example Data",
                  hover_data={'Date': '|%B %d, %Y'})
    

    @see