Search code examples
pythondataframeplotlyplotly-pythoncufflinks

how to customize hover date info in cufflink iplot?


I am using cufflink iplot. the default hover date info only include month and year. How to include day as well? like (Jan 24 2022, value) instead of (Jan 2022, value) by default? and there is not hovermode keyword. Thanks

import cufflinks as cf
from plotly.offline import iplot  #, init_notebook_mode

df.iplot(asFigure=True, xTitle='Date')


Solution

  • I tried a simple example and the day appears for me in the hovertemplate. Are the datetimes in your df of type datetime64[ns]?

    import cufflinks as cf
    import pandas as pd
    from plotly.offline import iplot
    
    df = pd.DataFrame({'date':['2022-01-01','2022-01-02'],'value':[1,2]})
    df['date'] = pd.to_datetime(df['date'])
    fig = df.iplot(asFigure=True, x='date', xTitle='Date')
    fig.show()
    

    enter image description here

    Another thing you could try is modifying the hovertemplate itself. Something like the following (assuming that the x value is the datetime and the y value is the value):

    fig.update_traces(hovertemplate='Date: %{x|%b %d, %Y} <br>Value: %{y}')
    

    enter image description here