Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading