0

I have this wonderful answer (which is here) working when changing my datetime column to numbers works perfectly, but at this points i really need to change to show datetime in X axis also in label, tried to change text = f'{line.get_label()}: ({posx:.2f},{posy:.2f}) since posx is the current number but can't achieve it.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Orlando
  • 120
  • 9

1 Answers1

0

Thanks to @Trenton McKinney I discover mplcursors, which is a wonderful library, and works for Dates in X axis, even when i set my date column as index

df0=pd.read_excel(filename)
df1=df0.set_index('Date')
###1st plot
fig,ax1 =plt.subplots(1,1)
axsub1 = ax1.twinx()
p1,=ax1.plot(df1[df1.columns[0]],'y-', label='1')
p2,=ax1.plot(df1[df1.columns[1]],'b-', label='2')
p3,=ax1.plot(df1[df1.columns[2]],'g-', label='3')
p4,=ax1.plot(df1[df1.columns[6]],'r-', label='4')
p5,=ax1.plot(df1[df1.columns[8]],'m-', label='DCP')
p6,=ax1.plot(df1[df1.columns[15]],'c-', label='UCP')
p7,=axsub1.plot(df1[df1.columns[7]],'k-', label='6')
ax1.set_title('1, 2, 3, DCP, UCP, 6')
ax1.set_xlabel("Date")
ax1.set_ylabel('1, 3, DCP, UCP')
axsub1.set_ylabel("6")
lns = [p1,p2,p3,p4,p5,p6,p7]
ax1.legend(handles=lns,loc='best', fontsize=10)
mplcursors.cursor(lns)

works amazing, this library saves you some codelines and time Thanks again @Trenton McKinney

Orlando
  • 120
  • 9
  • Be sure to add sample data and all your imports so people can reproduce the example. Provide data with [How to provide a reproducible copy of your DataFrame using `df.head(50).to_clipboard(sep=',')`](https://stackoverflow.com/q/52413246/7758804), then **[edit] your answer**, and paste the clipboard into a code block. – Trenton McKinney Aug 27 '21 at 17:17