1

我正在使用袖扣来绘制熊猫数据框。如何在悬停模板中包含恍惚名称(列名)?

我使用 name=df.columns.to_list() 然后在 hovertemplate 中使用 %{name} 并且它不起作用。感谢帮助。

 fig=df.iplot(xTitle='Time',yTitle='Temperature(degC)',title=title,name=df.columns.to_list(),
                          size=10,width=4,asFigure=True)
    
    fig.update_traces(hovertemplate='Location: %{name}<br>'+'Date: %{x|%b %d %H:%M,%Y} ,<br>Value:%{y}')

df.head().to_dict()   

{499.95: {Timestamp('2021-10-30 21:08:00'): 224.09,
  Timestamp('2021-10-30 21:17:00'): 224.78,
  Timestamp('2021-10-30 21:27:00'): 223.12,
  Timestamp('2021-10-30 21:36:00'): 224.3,
  Timestamp('2021-10-30 21:45:00'): 225.06},
 627.62: {Timestamp('2021-10-30 21:08:00'): 229.74,
  Timestamp('2021-10-30 21:17:00'): 230.66,
  Timestamp('2021-10-30 21:27:00'): 228.76,
  Timestamp('2021-10-30 21:36:00'): 230.0,
  Timestamp('2021-10-30 21:45:00'): 230.32},
 755.29: {Timestamp('2021-10-30 21:08:00'): 230.42,
  Timestamp('2021-10-30 21:17:00'): 231.13,
  Timestamp('2021-10-30 21:27:00'): 229.0,
  Timestamp('2021-10-30 21:36:00'): 230.28,
  Timestamp('2021-10-30 21:45:00'): 231.21},
 882.96: {Timestamp('2021-10-30 21:08:00'): 188.76,
  Timestamp('2021-10-30 21:17:00'): 190.16,
  Timestamp('2021-10-30 21:27:00'): 189.01,
  Timestamp('2021-10-30 21:36:00'): 191.25,
  Timestamp('2021-10-30 21:45:00'): 191.52},
 1010.63: {Timestamp('2021-10-30 21:08:00'): 196.5,
  Timestamp('2021-10-30 21:17:00'): 197.54,
  Timestamp('2021-10-30 21:27:00'): 196.08,
  Timestamp('2021-10-30 21:36:00'): 198.45,
  Timestamp('2021-10-30 21:45:00'): 198.66},
 1138.3: {Timestamp('2021-10-30 21:08:00'): 206.63,
  Timestamp('2021-10-30 21:17:00'): 206.79,
  Timestamp('2021-10-30 21:27:00'): 205.9,
  Timestamp('2021-10-30 21:36:00'): 207.4,
  Timestamp('2021-10-30 21:45:00'): 208.54},
 1265.97: {Timestamp('2021-10-30 21:08:00'): 213.16,
  Timestamp('2021-10-30 21:17:00'): 214.1,
  Timestamp('2021-10-30 21:27:00'): 212.56,
  Timestamp('2021-10-30 21:36:00'): 214.97,
  Timestamp('2021-10-30 21:45:00'): 215.12},
 1393.64: {Timestamp('2021-10-30 21:08:00'): 202.74,
  Timestamp('2021-10-30 21:17:00'): 205.55,
  Timestamp('2021-10-30 21:27:00'): 203.71,
  Timestamp('2021-10-30 21:36:00'): 207.18,
  Timestamp('2021-10-30 21:45:00'): 203.78},
 1521.31: {Timestamp('2021-10-30 21:08:00'): 199.74,
  Timestamp('2021-10-30 21:17:00'): 200.83,
  Timestamp('2021-10-30 21:27:00'): 199.52,
  Timestamp('2021-10-30 21:36:00'): 201.96,
  Timestamp('2021-10-30 21:45:00'): 203.11},
 1648.98: {Timestamp('2021-10-30 21:08:00'): 127.69,
  Timestamp('2021-10-30 21:17:00'): 127.94,
  Timestamp('2021-10-30 21:27:00'): 127.1,
  Timestamp('2021-10-30 21:36:00'): 128.06,
  Timestamp('2021-10-30 21:45:00'): 128.75}}
4

1 回答 1

1

在 plotly 中,默认情况下hovertemplate唯一可以访问变量xy值(因此不会理解name所指的内容)。通常,您会customdata在定义图形时使用该参数,然后按照此处%{customdata}文档中的说明在悬停模板字符串中使用。

但是,由于您正在使用df.iplot(...)创建图形,并且您似乎无法使用传递 customdata 作为参数,因此我们需要进入图形对象并为每个跟踪手动定义 customdata,然后我们可以%{customdata}在悬停模板中使用显示列名的字符串。

例如:

fig=df.iplot(xTitle='Time',yTitle='Temperature(degC)',title="title", name=df.columns.tolist(),
                          size=10,width=4,asFigure=True)

## manually set customdata in each trace
for fig_scatter_data in fig.data:
    fig_scatter_data['customdata'] = [fig_scatter_data['name']] * len(fig_scatter_data['x'])

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

这将产生下图:

在此处输入图像描述

于 2022-02-14T23:49:41.350 回答