Python Matplotlib Multicursor:在图例中显示光标下的值

我正在使用 Multicursor 在每个图形上获取光标。我想在将鼠标悬停在图表上时在图例中显示被光标击中的数据点的值,就像这样

http://img2.mukewang.com/63a162550001647006230446.jpg

实际上我一直认为这是 matplotlib 和 Multicursor 的标准功能,但似乎不是。有人已经这样做了,还是我必须自己实施。

我已经在 cursor 下找到了这篇文章 matplotlib multiple values,但这可能只是我想要的实现的开始。


慕码人2483693
浏览 276回答 1
1回答

米琪卡哇伊

我已经开发了一个解决方案。import matplotlib.pyplot as pltimport numpy as npfrom matplotlib.widgets import MultiCursorfrom bisect import bisect_leftfig = plt.figure(figsize=(15, 8))# create random graph with 60 datapoints, 0 till 59x = list(range(0,60))axes_list = []&nbsp;def createRandomGraph(ax,x):&nbsp; &nbsp; y = np.random.randint(low=0, high=15, size=60)&nbsp; &nbsp; data.append(y)&nbsp; &nbsp; ax.plot(x,y, marker='.')def take_closest(myList, myNumber):&nbsp; &nbsp; """&nbsp; &nbsp; Assumes myList is sorted. Returns closest value to myNumber.&nbsp; &nbsp; If two numbers are equally close, return the smallest number.&nbsp; &nbsp; """&nbsp; &nbsp; pos = bisect_left(myList, myNumber)&nbsp; &nbsp; if pos == 0:&nbsp; &nbsp; &nbsp; &nbsp; return myList[0]&nbsp; &nbsp; if pos == len(myList):&nbsp; &nbsp; &nbsp; &nbsp; return myList[-1]&nbsp; &nbsp; before = myList[pos - 1]&nbsp; &nbsp; after = myList[pos]&nbsp; &nbsp; if after - myNumber < myNumber - before:&nbsp; &nbsp; &nbsp; &nbsp;return after, pos&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp;return before, pos-1def show_Legend(event):&nbsp;&nbsp; &nbsp; #get mouse coordinates&nbsp; &nbsp; mouseXdata = event.xdata&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; # the value of the closest data point to the current mouse position shall be shown&nbsp; &nbsp; closestXValue, posClosestXvalue = take_closest(data[0], mouseXdata)&nbsp; &nbsp; i = 1&nbsp; &nbsp; for ax in axes_list:&nbsp; &nbsp; &nbsp; &nbsp; datalegend = ax.text(1.05, 0.5, data[i][posClosestXvalue],&nbsp; fontsize=7,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; verticalalignment='top', bbox=props, transform=ax.transAxes)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ax.draw_artist(datalegend)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # this remove is required because otherwise after a resizing of the window there is&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # an artifact of the last label, which lies behind the new one&nbsp; &nbsp; &nbsp; &nbsp; datalegend.remove()&nbsp; &nbsp; &nbsp; &nbsp; i +=1&nbsp; &nbsp; fig.canvas.update()# store the x value of the graph in the first element of the listdata = [x]# properties of the legend labelsprops = dict(boxstyle='round', edgecolor='black', facecolor='wheat', alpha=1.5)for i in range(5):&nbsp; &nbsp; if(i>0):&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # all plots share the same x axes, thus during zooming and panning&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # we will see always the same x section of each graph&nbsp; &nbsp; &nbsp; &nbsp; ax = plt.subplot(5, 1, i+1, sharex=ax)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; ax = plt.subplot(5, 1, i+1)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; axes_list.append(ax)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; createRandomGraph(ax,x)&nbsp;&nbsp;multi = MultiCursor(fig.canvas, axes_list, color='r', lw=1)&nbsp; &nbsp;# function show_Legend is called while hovering over the graphs&nbsp;fig.canvas.mpl_connect('motion_notify_event', show_Legend)plt.show()输出看起来像这样https://i.stack.imgur.com/ksxGW.gif
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python