【问题标题】:mplcursors: show and highlight coordinates of nearby local extrememplcursors:显示并突出显示附近局部极值的坐标
【发布时间】:2020-06-06 23:29:34
【问题描述】:

我的代码使用mplcursors 显示matplotlib 散点图中每个点的标签,类似于this example。我想知道如何形成一个值列表,使某个点脱颖而出,就好像我有一个点图y=-x^2。当我接近峰值时,它不应该显示 0.001,而是显示 0,而无需找到顶部的确切鼠标位置。我无法解决图中的每个点,因为我没有特定的函数。

【问题讨论】:

    标签: python-3.x matplotlib mplcursors


    【解决方案1】:

    假设散点图中的点是有序的,我们可以调查附近窗口中的极值是否也是稍大窗口中的极值。如果,那么我们可以用它的 x 和 y 坐标报告那个极值。

    下面的代码仅在我们接近局部最大值或最小值时显示注释。它还临时显示一条水平和垂直线以指示确切的位置。该代码可以作为许多变体的起点。

    import matplotlib.pyplot as plt
    import mplcursors
    import numpy as np
    
    near_window = 10 # the width of the nearby window
    far_window = 20 # the width of the far window
    
    def show_annotation(sel):
        ind = sel.target.index
        near_start_index = max(0, ind - near_window)
        y_near = y[near_start_index: min(N, ind + near_window)]
        y_far = y[max(0, ind - far_window): min(N, ind + far_window)]
        near_max = y_near.max()
        far_max = y_far.max()
        annotation_str = ''
        if near_max == far_max:
            near_argmax = y_near.argmax()
            annotation_str = f'local max:\nx:{x[near_start_index + near_argmax]:.3f}\ny:{near_max:.3f}'
            maxline = plt.axhline(near_max, color='crimson', ls=':')
            maxline_x = plt.axvline(x[near_start_index+near_argmax], color='grey', ls=':')
            sel.extras.append(maxline)
            sel.extras.append(maxline_x)
        else:
            near_min = y_near.min()
            far_min = y_far.min()
            if near_min == far_min:
                near_argmin = y_near.argmin()
                annotation_str = f'local min:\nx:{x[near_start_index+near_argmin]:.3f}\ny:{near_min:.3f}'
                minline = plt.axhline(near_min, color='limegreen', ls=':')
                minline_x = plt.axvline(x[near_start_index + near_argmin], color='grey', ls=':')
                sel.extras.append(minline)
                sel.extras.append(minline_x)
        if len(annotation_str) > 0:
            sel.annotation.set_text(annotation_str)
        else:
            sel.annotation.set_visible(False) # hide the annotation
            # sel.annotation.set_text(f'x:{sel.target[0]:.3f}\n y:{sel.target[1]:.3f}')
    
    N = 500
    x = np.linspace(0, 100, 500)
    y = np.cumsum(np.random.normal(0, 0.1, N))
    box = np.ones(20) / 20
    y = np.convolve(y, box, mode='same')
    scat = plt.scatter(x, y, s=1)
    
    cursor = mplcursors.cursor(scat, hover=True)
    cursor.connect('add', show_annotation)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 2011-12-15
      • 1970-01-01
      相关资源
      最近更新 更多