将垂直线添加到悬停框(见图片)
Add the vertical line to the hoverbox (see pictures)
我正在制作一个程序来快速分析电池充电器等的测试曲线。我想结合 hoverbox,它捕捉到每条曲线和一条垂直线以便于比较。如果我激活这两个代码,它们会发生碰撞,并且在移动鼠标时我会得到一条线,当我停止它时它会消失并且悬停框不会捕捉到曲线。
悬停框由 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 instaid。这可能吗?
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()
我正在制作一个程序来快速分析电池充电器等的测试曲线。我想结合 hoverbox,它捕捉到每条曲线和一条垂直线以便于比较。如果我激活这两个代码,它们会发生碰撞,并且在移动鼠标时我会得到一条线,当我停止它时它会消失并且悬停框不会捕捉到曲线。
悬停框由 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 instaid。这可能吗?
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()