0

it's not showing in bottom right corner too. Data is coming from table in database. It plots the graph well but coordinates of x is missing on mouse hover over graph. Please help. I am using mplcursors for mouse hover.

import matplotlib.pyplot as plt
import mplcursors
from datetime import datetime

ax1 = plt.subplot(111)
time = ['2017-01-01 09:00:00.000', '2017-01-01 09:00:01.000', '2017-01-01 09:00:02.000', '2017-01-01 09:00:03.000', '2017-01-01 09:00:04.000', '2017-01-01 09:00:05.000', '2017-01-01 09:00:06.000', '2017-01-01 09:00:07.000', '2017-01-01 09:00:08.000', '2017-01-01 09:00:09.000', '2017-01-01 09:00:10.000']
lstDateTime = [str(datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f').isoformat(sep=' ', timespec='milliseconds')) for x in
               time]
print(f'lstDateTime: {", ".join(lstDateTime)}')
bet = [60.01, 60.01, 62.01, 61.01, 63.79, 69.28, 63.51, 62.24, 60.53, 61.53, 60.53]
prob = [61.1, 61.2, 63.03, 62.03, 64.02, 70.28, 64.51, 63.24, 61.53, 62.53, 61.53]
plt.plot_date(lstDateTime, bet, "b-", label="bet")
plt.plot_date(lstDateTime, prob, "g-", label="porb")
plt.tick_params(axis='x', rotation=90)
# ax1.plot(lstDateTime, bet, "b-", label="bet")
# ax1.plot(lstDateTime, prob, "g-", label="porb")
# ax1.tick_params(axis='x', rotation=90)
mplcursors.cursor(hover=True)
plt.show()

what's bugging me is that from start I cannot see x coordinate even in bottom right corner of matplotlib card.

enter image description here

it's something I don't understand. x axis has list of timestamp.

'2017-01-01 11:43:07.000', '2017-01-01 11:43:23.000', '2017-01-01 11:42:45.000' 

like this but it does not show up. Why and how to correct it I need to know.

Issue is somewhere here:- issue is in time conversion.

lstDateTime = [str(datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f').isoformat(sep=' ', timespec='milliseconds')) for x in time] print(f'lstDateTime: {", ".join(lstDateTime)}')

but it gives correct format issue is x coordinates stops showing.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Rahul
  • 41
  • 1
  • 7
  • Does this answer your question? [Python matplotlib doesn't show full date on mouse hover](https://stackoverflow.com/questions/46853259/python-matplotlib-doesnt-show-full-date-on-mouse-hover) – Joe Apr 14 '20 at 09:55
  • no, in that example it's showing something to begin with. – Rahul Apr 14 '20 at 09:56
  • There is a specific function for plotting date series: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.plot_date.html – Joe Apr 14 '20 at 11:23
  • https://stackoverflow.com/questions/1574088/plotting-time-in-python-with-matplotlib – Joe Apr 14 '20 at 11:24
  • Changed situation is still the same. – Rahul Apr 14 '20 at 11:34
  • It's probably not related to mplcursors. Please use this example https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/date.html or maybe this https://kite.com/python/answers/how-to-plot-dates-on-the-x-axis-of-a-matplotlib-plot-in-python or this one https://blog.mafr.de/2012/03/11/time-series-data-with-matplotlib/ and replace it bit by bit with your data, your dates and test at which point it fails. Do the examples show the date on hover? – Joe Apr 14 '20 at 12:02
  • @Joe evry time stamp has it's corresponding bid and prob value. No these references don't help. add to that they can be random timestamps too with no fixed interval between them. – Rahul Apr 14 '20 at 12:21
  • You need to figure out where the source of the problem is. Your timestaps, matplotlib, something else? So you need to rule out piece by piece. One way is to do what I mentioned above. – Joe Apr 14 '20 at 15:20
  • issue is in time conversion. lstDateTime = [str(datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f').isoformat(sep=' ', timespec='milliseconds')) for x in time] print(f'lstDateTime: {", ".join(lstDateTime)}') – Rahul Apr 15 '20 at 08:07
  • Please add an example with two example dates in the answer above so we can execute it. Is there a message? – Joe Apr 15 '20 at 08:08
  • if I use pandas it gives full plot but date is not complete – Rahul Apr 15 '20 at 08:23
  • What does the example show? It is converting from string to string. – Joe Apr 15 '20 at 09:40
  • I found no solution to this for basically millisecond has no directive. It goes with seconds or microsecond but not millisecond. That's my dead end. – Rahul Apr 18 '20 at 12:07
  • I don't get it. `2017-01-01 09:00:01.500` is 500 milliseconds, but also 500000 microseconds. The keyword `timespec` in`isoformat` can be `seconds`, `milliseconds` or `microseconds`. https://docs.python.org/3/library/datetime.html#datetime.datetime.isoformat – Joe Apr 18 '20 at 13:46
  • Joe it can be in iso format but iso format millisecond coordinates does not show up as y in actual matplotlib graph. And you are right it works if we convert to microseconds but not as milliseconds. – Rahul Apr 18 '20 at 14:16
  • Could you please post a screenshot? – Joe Apr 18 '20 at 15:13

1 Answers1

0

How about that. You probably have to use matplotlib dates.

But there seems to be a problem with the accuracy, see the hover tag. There is a timestamp that is not in the data.

import matplotlib.pyplot as plt
import mplcursors
from datetime import datetime

import matplotlib.dates as mdates
mdates.date2num

fig, ax = plt.subplots()
time = ['2017-01-01 09:00:00.000', '2017-01-01 09:00:01.000', '2017-01-01 09:00:02.000', '2017-01-01 09:00:03.000', '2017-01-01 09:00:04.000', '2017-01-01 09:00:05.000', '2017-01-01 09:00:06.000', '2017-01-01 09:00:07.000', '2017-01-01 09:00:08.000', '2017-01-01 09:00:09.000', '2017-01-01 09:00:10.000']

# to not use isoformat, use datetime objects
lstDateTime = [datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f') for x in
               time]

# convert to matplotlib date format
lstDateTime = mdates.date2num(lstDateTime)

#print(f'lstDateTime: {", ".join(lstDateTime)}')
bet = [60.01, 60.01, 62.01, 61.01, 63.79, 69.28, 63.51, 62.24, 60.53, 61.53, 60.53]
prob = [61.1, 61.2, 63.03, 62.03, 64.02, 70.28, 64.51, 63.24, 61.53, 62.53, 61.53]
ax.plot_date(lstDateTime, bet, "b-", label="bet")
ax.plot_date(lstDateTime, prob, "g-", label="porb")
plt.tick_params(axis='x', rotation=90)

ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S.%f'))

fig.autofmt_xdate()
mplcursors.cursor(hover=True)

plt.show()

enter image description here

Joe
  • 6,758
  • 2
  • 26
  • 47