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.
Asked
Active
Viewed 133 times
0

Trenton McKinney
- 56,955
- 33
- 144
- 158

Orlando
- 120
- 9
-
1You would really highly increase your chances of getting a helpful answer if you'd provide some minimal reproducible code (including test data). – JohanC Aug 27 '21 at 15:32
-
1I'm guessing you can use [tag: `mplcursors`](https://stackoverflow.com/tags/mplcursors/info) – Trenton McKinney Aug 27 '21 at 15:33
-
@TrentonMcKinney thank you so much didnt know about mplcursors easy to use!!!! – Orlando Aug 27 '21 at 15:55
-
@TrentonMcKinney how can set your comment as answer?? or maybe answer again :) thanks again – Orlando Aug 27 '21 at 16:01
-
Once you have it set up, you can post an answer. I wouldn't post a link as an answer. – Trenton McKinney Aug 27 '21 at 16:06
-
Please provide enough code so others can better understand or reproduce the problem. – Community Aug 27 '21 at 20:10
1 Answers
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