How do I show text from a third dataframe column when hovering over a line chart made from 2 other columns?

So I have a dataframe with 3 columns: date, price, text

import pandas as pd
from datetime import datetime
import random

columns = ('dates','prices','text')
datelist = pd.date_range(datetime.today(), periods=5).tolist()
prices = []
for i in range(0, 5):
    prices.append(random.randint(50, 60))
text =['AAA','BBB','CCC','DDD','EEE']

df = pd.DataFrame({'dates': datelist, 'price':prices, 'text':text})
    dates   price   text
0   2022-11-23 14:11:51.142574  51  AAA
1   2022-11-24 14:11:51.142574  57  BBB
2   2022-11-25 14:11:51.142574  52  CCC
3   2022-11-26 14:11:51.142574  51  DDD
4   2022-11-27 14:11:51.142574  59  EEE

I want to plot date and price on a line chart, but when I hover over the line I want it to show the text from the row corresponding to that date.

eg when I hover over the point corresponding to 2022-11-27 I want the text to show ‘EEE’

ive tried a few things in matplotlib etc but can only get data from the x and y axis to show but I cant figure out how to show data from a different column.

Any help would be greatly appreciated.

Thanks

>Solution :

You could use Plotly.

import plotly.graph_objects as go

fig = go.Figure(data=go.Scatter(x=df['dates'], y=df['price'], mode='lines+markers', text=df['text']))
fig.show()

Leave a Reply