Matplotlib: Cursor snap to plotted data with datetime axis

匿名 (未验证) 提交于 2019-12-03 02:17:01

问题:

I have a plot of 3 data sets that have datetime objetcs on the x axis. I want to have a cursor that snaps to the data and shows the precise x and y value.

I already have a "snap to cursor", but that only works for scalar x axes. Can anyone help me to modify the snap to cursor so that it works for datetime x axes as well?

Here are my data plots:

import numpy as np import matplotlib.pyplot as plot import matplotlib.ticker as mticker import matplotlib.dates as dates import datetime import Helpers  fig = plot.figure(1) DAU = (  2,  20,  25,  60, 190, 210,  18, 196, 212) WAU = ( 50, 160, 412, 403, 308, 379, 345, 299, 258) MAU = (760, 620, 487, 751, 612, 601, 546, 409, 457)  firstDay = datetime.datetime(2012,1,15)  #create an array with len(DAU) entries from given starting day dayArray = [firstDay + datetime.timedelta(days = i) for i in xrange(len(DAU))]  line1 = plot.plot(dayArray, DAU, 'o-', color = '#336699') line2 = plot.plot(dayArray, WAU, 'o-', color = '#993333') line3 = plot.plot(dayArray, MAU, 'o-', color = '#89a54e')  ax = plot.subplot(111) dateLocator   = mticker.MultipleLocator(2) dateFormatter = dates.DateFormatter('%d.%m.%Y') ax.xaxis.set_major_locator(dateLocator) ax.xaxis.set_major_formatter(dateFormatter) fig.autofmt_xdate(rotation = 90, ha = 'center')  yMax = max(np.max(DAU), np.max(WAU), np.max(MAU)) yLimit = 100 - (yMax % 100) + yMax plot.yticks(np.arange(0, yLimit + 1, 100))  plot.title('Active users', weight = 'bold') plot.grid(True, axis = 'both') plot.subplots_adjust(bottom = 0.2) plot.subplots_adjust(right = 0.82)  legend = plot.legend((line1[0], line2[0], line3[0]),                  ('DAU',                  'WAU',                  'MAU'),                  'upper left',                  bbox_to_anchor = [1, 1],                  shadow = True)  frame = legend.get_frame() frame.set_facecolor('0.80') for t in legend.get_texts():     t.set_fontsize('small')  #THIS DOES NOT WORK cursor = Helpers.SnaptoCursor(ax, dayArray, DAU, 'euro daily') plot.connect('motion_notify_event', cursor.mouse_move)  plot.show() 

And this is my module "Helper" that contains the "SnaptoCursor" class: (I got the basic SnaptoCursor class from somewhere else and modified it a little bit)

Of course this does not work since I am calling the SnaptoCursor with the datetime array "dayArray", which is supposed to be compared to the mouse coordinates later on. And these data types are not comparable.

回答1:

I got it!!!

The problems where these two lines in the init method of the SnaptoCursor class:

self.lx = ax.axhline(color = 'k')  #the horiz line self.ly = ax.axvline(color = 'k')  #the vert line 

They were somehow messing up the datetime x axis (that has ordinals up to 730,000 e.g.), so you just have to initialize the lines' coordinates:

self.lx = ax.axhline(y = min(y), color = 'k')  #the horiz line self.ly = ax.axvline(x = min(x), color = 'k')  #the vert line 

Then it works just fine!

I'll be posting my complete SnaptoCursor class now that I have modified so it accepts individual formatting strings, and it can take up to 3 input data plots - that get snapped to according to your mouse position.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!