0

Currently I'm doing some data visualization using python, matplotlib and mplcursor that requires to show different parameters and values at the same time in a certain time period.

Sample CSV data that was extracted from a system: https://i.stack.imgur.com/fjd1d.png

My expected output would look like this: https://i.stack.imgur.com/zXGXA.png

Found the same case but they were using numpy functions: Add the vertical line to the hoverbox (see pictures)

Hoping someone will suggest what is the best approach of my problem.

Code below:

import matplotlib.pyplot as plt
import numpy as np
import mplcursors
import pandas as pd


fig, ax=plt.subplots()

y1=ax.twinx()
y2=ax.twinx()

y2.spines.right.set_position(("axes", 1.05))

df=pd.read_csv(r"C:\Users\OneDrive\Desktop\sample.csv")

time=df['Time']
yd1=df['Real Power']
yd2=df['Frequency']
yd3=df['SOC']

l1=ax.plot(time,yd1,color='black', label='Real Power')
l2=y1.plot(time,yd2, color='blue', label='Frequency')
l3=y2.plot(time,yd3, color='orange', label='SOC')

df=pd.DataFrame(df)
arr=df.to_numpy()
print(arr)
def show_annotation(sel):
    x=sel.target[0]
    annotation_str = df['Real Power'][sel.index]
    #sel.annotation.set_text(annotation_str)

fig.autofmt_xdate()
cursor=mplcursors.cursor(hover=True)
cursor.connect('add', show_annotation)
plt.show()```


enzener
  • 1
  • 1
  • Try this: `def show_annotation(sel):x=sel.target[0];vertical_line = ax.axvline(x, color='red', ls=':', lw=1);sel.extras.append(vertical_line);annotation_str = df['Real Power']` Please modify the annotation function to the one above. – r-beginners Feb 22 '22 at 07:27
  • Thank already did what you suggested it seems to work in a single parameter but the problem is when multiple parameters are annotated. – enzener Feb 22 '22 at 08:23
  • As you can see in the code of the referenced answer, we have made a list of strings for each graph value, so we can do the same. – r-beginners Feb 22 '22 at 08:50
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 28 '22 at 02:24

0 Answers0