【问题标题】:Add the vertical line to the hoverbox (see pictures)将垂直线添加到悬停框(见图)
【发布时间】:2021-03-09 05:52:27
【问题描述】:

我正在编写一个程序来快速分析电池充电器等的测试曲线。我想结合悬停框,它用一条垂直线捕捉到每条曲线,以便于比较。如果我激活这两个代码,它们会发生碰撞,并且在移动鼠标时会出现一条线,当我停止时它会消失并且悬停框不会与曲线对齐。

悬停框是由 mplcursors 库制作的,而线条是由 matplotlib 中的光标小部件制作的。

cursor = Cursor(
    ax2, useblit=True, horizOn=False, vertOn=True, color="red", linewidth=0.5
)

mplcursors.cursor(hover=True)

完整代码在这里:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
import mplcursors

data = np.loadtxt("test.txt")

x = data[:, 0]
y = data[:, 1]
y2 = data[:, 2]
y3 = data[:, 3]

fig = plt.figure(figsize=(13, 5))

ax = fig.add_subplot(111)

ax.plot(x, y, "--", label="Voltage")
ax.plot(x, y2, "-.", label="Current")


ax2 = ax.twinx()
ax2.plot(x, y3, "g:", label="Temperature")
ax2.set_ylabel("Celsius", color=("LightBlue"))
ax2.set_ylim(18, 100)

fig.legend(
    edgecolor=("DarkBlue"),
    facecolor=("LightBlue"),
    loc="upper right",
    bbox_to_anchor=(1, 1),
    bbox_transform=ax.transAxes,
)

ax.set_title("Test Surveillance", color=("Purple"))
ax.set_xlabel("Milliseconds", color=("LightGreen"))
ax.set_ylabel("Volts and Amps", color=("DarkGrey"))


plt.xlim(0)

# cursor = Cursor(
#     ax2, useblit=True, horizOn=False, vertOn=True, color="red", linewidth=0.5
# )
mplcursors.cursor(hover=True)

plt.show()

作为一个额外的好处:X 值在示例中以秒为单位(我知道它表示毫秒)。我想显示 1:45:24 或图片中 x=5.77e+04 的任何内容。这可能吗?

【问题讨论】:

    标签: python numpy matplotlib mplcursors


    【解决方案1】:

    mplcursors 允许显式设置每次显示注释时都会调用的函数。在那里可以更改显示的文本(以及许多其他属性)。此外,还可以绘制额外的元素,例如线条。当此类元素附加到sel.extras 时,它们将被自动擦除,然后随着光标位置的变化而重新绘制。

    ax.xaxis.set_major_formatter() 将设置格式功能,用于刻度标签和状态栏中的坐标显示。

    import numpy as np
    import matplotlib.pyplot as plt
    import mplcursors
    from math import floor
    
    def hhmmss_formatter(x, pos=None):
        s = floor(x % 60)
        m = floor((x - s) / 60) % 60
        h = floor(x / 3600)
        return f'{h}:{m:02d}' if s == 0 else f'{h}:{m:02d}:{s:02d}'
    
    def show_annotation(sel):
        xi = sel.target[0]
        vertical_line = ax.axvline(xi, color='red', ls=':', lw=1)
        sel.extras.append(vertical_line)
        # print('label:', sel.artist.get_label())
        y1i = np.interp(xi, x, y)
        y2i = np.interp(xi, x, y2)
        y3i = np.interp(xi, x, y3)
        annotation_str = f'Time: {hhmmss_formatter(xi)}\nVoltage: {y1i:.1f}\nCurrent: {y2i:.1f}\nTemperature: {y3i:.1f}'
        sel.annotation.set_text(annotation_str)
    
    x = np.linspace(0, 85000, 500)
    y = np.random.randn(len(x)).cumsum() / 5 + 15
    y2 = np.random.randn(len(x)).cumsum() / 5 + 30
    y3 = np.random.randn(len(x)).cumsum() / 5 + x / 10000 + 25
    
    fig = plt.figure(figsize=(13, 5))
    
    ax = fig.add_subplot(111)
    
    ax.plot(x, y, "--", label="Voltage")
    ax.plot(x, y2, "-.", label="Current")
    
    ax2 = ax.twinx()
    ax2.plot(x, y3, "g:", label="Temperature")
    ax2.set_ylabel("Celsius", color="LightBlue")
    ax2.set_ylim(18, 100)
    
    fig.legend(edgecolor=("DarkBlue"), facecolor=("LightBlue"), loc="upper right", bbox_to_anchor=(1, 1),
               bbox_transform=ax.transAxes, )
    
    ax.set_title("Test Surveillance", color="Purple")
    ax.set_xlabel("Seconds", color="LightGreen")
    ax.set_ylabel("Volts and Amps", color="DarkGrey")
    
    ax.set_xlim(xmin=0)
    ax.xaxis.set_major_formatter(plt.FuncFormatter(hhmmss_formatter))  # show x-axis as hh:mm:ss
    ax.xaxis.set_major_locator(plt.MultipleLocator(2 * 60 * 60))  # set ticks every two hours
    
    cursor = mplcursors.cursor(hover=True)
    cursor.connect('add', show_annotation)
    plt.show()
    

    【讨论】: