itsaMeMathi0x
itsaMeMathi0x

Reputation: 172

How to display Date (X Axis) info with mpldatacursor?

I'm working on a telemetry system, and right now I would like to see each scatter point in my plot with each pair of coordinates through clicks. My plot is a time series, so I'm having a hard time to display each date with datacursor. I'm currently using this line

plt.gca().fmt_xdata = matplotlib.dates.DateFormatter('%H:%M:%S')

Which certifies me that my X axis is date-based.

I have already tried like this:

datacursor(ax1, formatter = 'Valor medido : {y:.6f} às {x:.6f}'.format)

The output is ok for Y, but the date come out as a "epoch number", like "57990.011454".

After a little research, I can convert this number with:

matplotlib.dates.num2date(d).strftime('%H:%M:%S')

but I'm failing to put it all together to display in my cursor.

Thanks in advance!

Upvotes: 0

Views: 63

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

formatter= accepts any function that returns a string. You could therefore write (code untested because you did not provide a Minimal, Complete, and Verifiable example )

def print_coords(**kwargs):
    return 'Valor medido : {y:.6f} às {x:s}'.format(y=kwargs['y'],
                                                    x=matplotlib.dates.num2date(kwargs['x']).strftime('%H:%M:%S'))

datacursor(ax1, formatter=print_coords)

Upvotes: 1

Related Questions