Aman Rathore
Aman Rathore

Reputation: 183

How can I edit hovertext labels in Plotly Python?

When I'm hovering on a plot in Heatmap it is showing axis as x, y, z.

But I wanted it to be like Year, Month, No. of Passengers respectively

Heatmap: See the image of Heatmap here

Here is my code:

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

Upvotes: 2

Views: 453

Answers (1)

Henry Ecker
Henry Ecker

Reputation: 35686

Add a hovertemplate and specify the format:

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

Variables are referenced by %{x}, %{y}, and %{z} respectively, otherwise html can be used to format the display.

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='')
]

hover text

name is set to the empty string (name=''), otherwise trace 0 will appear as the label:

hover text with name

Upvotes: 1

Related Questions