【问题标题】:Python Matplotlib Multicursor: Show value under cursor in legendPython Matplotlib Multicursor:在图例中显示光标下的值
【发布时间】:2020-06-18 17:13:32
【问题描述】:

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

其实我一直以为这是matplotlib分别Multicursor的一个标准特性,但好像不是。有人已经这样做了还是我必须自己实现它。

我已经找到了这个帖子matplotlib multiple values under cursor,但这可能只是我想要的实现的开始。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我已经开发了一个解决方案。

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.widgets import MultiCursor
    from bisect import bisect_left
    
    
    fig = plt.figure(figsize=(15, 8))
    
    # create random graph with 60 datapoints, 0 till 59
    x = list(range(0,60))
    
    axes_list = [] 
    
    def createRandomGraph(ax,x):
        y = np.random.randint(low=0, high=15, size=60)
        data.append(y)
        ax.plot(x,y, marker='.')
    
    
    def take_closest(myList, myNumber):
        """
        Assumes myList is sorted. Returns closest value to myNumber.
    
        If two numbers are equally close, return the smallest number.
        """
        pos = bisect_left(myList, myNumber)
        if pos == 0:
            return myList[0]
        if pos == len(myList):
            return myList[-1]
        before = myList[pos - 1]
        after = myList[pos]
        if after - myNumber < myNumber - before:
           return after, pos
        else:
           return before, pos-1
    
    
    
    def show_Legend(event): 
        #get mouse coordinates
        mouseXdata = event.xdata    
    
        # the value of the closest data point to the current mouse position shall be shown
        closestXValue, posClosestXvalue = take_closest(data[0], mouseXdata)
    
        i = 1
        for ax in axes_list:
            datalegend = ax.text(1.05, 0.5, data[i][posClosestXvalue],  fontsize=7,
                                          verticalalignment='top', bbox=props, transform=ax.transAxes)               
            ax.draw_artist(datalegend) 
    
            # this remove is required because otherwise after a resizing of the window there is 
            # an artifact of the last label, which lies behind the new one
            datalegend.remove()
            i +=1
    
        fig.canvas.update()
    
    
    # store the x value of the graph in the first element of the list
    data = [x]
    
    # properties of the legend labels
    props = dict(boxstyle='round', edgecolor='black', facecolor='wheat', alpha=1.5)
    
    for i in range(5):
        if(i>0):        
            # all plots share the same x axes, thus during zooming and panning 
            # we will see always the same x section of each graph
            ax = plt.subplot(5, 1, i+1, sharex=ax)             
        else:
            ax = plt.subplot(5, 1, i+1)        
    
        axes_list.append(ax)    
        createRandomGraph(ax,x)  
    
    multi = MultiCursor(fig.canvas, axes_list, color='r', lw=1)   
    
    # function show_Legend is called while hovering over the graphs 
    fig.canvas.mpl_connect('motion_notify_event', show_Legend)
    
    plt.show()
    

    输出如下所示

    也许你喜欢它并觉得它有用

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2020-10-11
      • 1970-01-01
      • 2021-03-27
      • 2023-03-22
      • 1970-01-01
      • 2018-05-12
      • 2014-08-01
      • 2017-03-03
      相关资源
      最近更新 更多