Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding dates into a hovertemplate plotly

Tags:

python

plotly

I am very new to plotly and have run into some trouble formatting the hover info. I am trying to use the hovertemplate in order to provide specific text that I want, as opposed to whatever the graph auto formats to. I want to supply a list of dates, as well as a value and some additional text. However, when trying to supply a date it does not seem to format. I tried doing an exact example from plotly documentation that I will show below.

import plotly.graph_objs as go

fig = go.Figure()
fig.add_trace(
    go.Scatter(x=dict2['time'],
               y=dict2['2m_temp_prod'],
               mode='markers+lines',
               name='Sfc Temp',
               line=dict(color='red', width=4),
               hovertemplate='Day: %{2019-01-01|%A}'))

The hover text does not properly convert the date and instead reads Day: %{2019-01-01|%A}. Any ideas on how to fix this?

Edit: Here is a more workable example.

I load in some data from an xarray dataarray and transform it into a dictionary.

dict2

{'time': 0    2020-08-12 00:00:00
 1    2020-08-12 06:00:00
 2    2020-08-12 12:00:00
 3    2020-08-12 18:00:00
 4    2020-08-13 00:00:00
              ...        
 56   2020-08-26 00:00:00
 57   2020-08-26 06:00:00
 58   2020-08-26 12:00:00
 59   2020-08-26 18:00:00
 60   2020-08-27 00:00:00
 Name: time, Length: 61, dtype: datetime64[ns],
 '2m_temp_prod': 0     84.0
 1     74.0
 2     71.0
 3     82.0
 4     79.0
       ... 
 56    79.0
 57    70.0
 58    67.0
 59    82.0
 60    80.0
 Name: 2m_temp_prod, Length: 61, dtype: float64,
 '2m_temp_area': 0     84.0
 1     74.0
 2     70.0
 3     82.0
 4     80.0
       ... 
 56    79.0
 57    70.0
 58    67.0
 59    82.0
 60    80.0
 Name: 2m_temp_area, Length: 61, dtype: float64}

This data plots fine as a line plot, but the hovertext is wrong, as described above. I tried just copying the example date from the plotly documentation above, but ultimately I want to provide a list of string dates to be applied to the hovertext instead of the autogenerated hovertext. I would like the hovertext to say: Day: Tuesday. The code is supposed to take the date and only output the day of the week. In this case, Jan 1, 2019 was on a Tuesday. Plot for reference.

enter image description here

like image 426
Eli Turasky Avatar asked Aug 11 '20 19:08

Eli Turasky


People also ask

How do I change the date format in plotly?

To customize date format, from 'Axes' under 'Style' menu choose 'Tick Labels' submenu. Next, select the axis you wish to modify, and then set 'advanced(d3-time-format)' for 'Label Format' attribute. A text box will appear where you can enter a custom time format.

What is hover name in plotly?

Hover Labels is the most depectively-power feature for interactive visualization in plotly, for user it is the ability to reveal more information about the data points by moving the cursor (mouse) over the point and having a hover label appear.

What is Hover_data?

hover_data can also be a dictionary. Its keys are existing columns of the dataframe argument, or new labels. For an existing column, the values can be. False to remove the column from the hover data (for example, if one wishes to remove the column of the x argument)


1 Answers

For hovertemplate the variables are x and y respectively

Data

import pandas as pd
import plotly.graph_objs as go
import numpy as np

n = 60
df = pd.DataFrame(
    {"time":pd.date_range(start="2020-08-12", freq="6H", periods=n),
     "2m_temp_prod":np.random.randint(low=65,high=85, size=n)})

Plot

Here I added few more things in hovertemplate as reference

fig = go.Figure()
fig.add_trace(
    go.Scatter(x=df['time'],
               y=df['2m_temp_prod'],
               mode='markers+lines',
               name='Sfc Temp',
               line=dict(color='red', width=4),
               hovertemplate="Date: %{x|%Y-%m-%d}<br>Day: %{x|%A}<br>Temp: %{y:.2f}"
              ))

enter image description here

like image 124
rpanai Avatar answered Sep 25 '22 23:09

rpanai