如何在 Plotly Python 中编辑悬停文本标签?

How can I edit hovertext labels in Plotly Python?

当我将鼠标悬停在热图中的某个图上时,它会将轴显示为 x、y、z。

但我希望它分别像年、月、乘客数

热图:

这是我的代码:

import pandas as pd
import plotly.offline as pyo
import plotly.graph_objs as go

flights_data_csv = pd.read_csv("flights.csv")

data = [go.Heatmap(x=flights_data_csv["year"],
                   y=flights_data_csv["month"],
                   z=flights_data_csv["passengers"].values.tolist(),
                   colorbar=dict(title="Passengers traveled <br>by Flights"),
                   colorscale="jet",
                   zmin=100,
                   zmax=650)]

layout = go.Layout(title="Exercise of HeatMaps <br>For Showing Flights details",
                   title_x=0.5)
fig = go.Figure(data, layout)
pyo.plot(fig, filename="Flights Heatmaps.html")

Here is flight dataset

添加一个hovertemplate并指定格式:

Year:%{x}<br>Month:%{y}<br>Passengers:%{z}

变量分别由%{x}%{y}%{z}引用,否则可以用html格式化显示

data = [
    go.Heatmap(x=flights_data_csv["year"],
               y=flights_data_csv["month"],
               z=flights_data_csv["passengers"],
               colorbar=dict(title="Passengers traveled <br>by Flights"),
               colorscale="jet",
               zmin=100,
               zmax=650,
               hovertemplate='Year:%{x}<br>Month:%{y}<br>Passengers:%{z}',
               name='')
]

name设置为空字符串(name=''),否则trace 0将显示为标签: