简体   繁体   English

将日期添加到悬停模板 plotly

[英]Adding dates into a hovertemplate plotly

I am very new to plotly and have run into some trouble formatting the hover info.我对 plotly 非常陌生,并且在格式化 hover 信息时遇到了一些麻烦。 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.我正在尝试使用hovertemplate来提供我想要的特定文本,而不是图形自动格式化的任何内容。 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.我尝试从 plotly 文档中做一个确切的例子,我将在下面展示。

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}. hover 文本没有正确转换日期,而是显示为 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.我从 xarray 数据数组中加载一些数据并将其转换为字典。

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.该数据绘制为 plot 行,但悬停文本错误,如上所述。 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.我尝试从上面的 plotly 文档中复制示例日期,但最终我想提供一个字符串日期列表以应用于悬停文本而不是自动生成的悬停文本。 I would like the hovertext to say: Day: Tuesday.我想让 hovertext 说: 日:星期二。 The code is supposed to take the date and only output the day of the week.该代码应该采用日期,并且只有 output 是星期几。 In this case, Jan 1, 2019 was on a Tuesday.在这种情况下,2019 年 1 月 1 日是星期二。 Plot for reference. Plot 供参考。

在此处输入图像描述

For hovertemplate the variables are x and y respectively对于hovertemplate ,变量分别是xy

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 Plot

Here I added few more things in hovertemplate as reference在这里,我在hovertemplate中添加了一些东西作为参考

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}"
              ))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM