我正在尝试编写代码以将所选行(通过单击或鼠标悬停)突出显示为红线。
我已经尝试了一些解决方案,我发现如何在 matplotlib 中突出显示线条集合,在 matplotlib中更改标记颜色,https : //github.com/joferkington/mpldatacursor,https: //mplcursors.readthedocs.io/en/稳定的/
但大多数情况下在一个子图中的单个轴或两个轴上工作。就我而言,我需要在多个子图中处理多行。
我试过了:
def on_pick (event, fig, axes):
for ax in axes:
for line in ax.lines:
if event.artist is line:
ind = event.ind[0]
line.set_color('red')
fig.canvas.draw_idle()
fig1.canvas.mpl_connect('pick_event', lambda event: on_pick(event,fig1, axes))
当我点击我的身影时,什么也没有发生。所以,我不知道如何实现它。
为了使类似的功能起作用,显示所选行的标签。我只是使用:
import mplcursors
mplcursors.cursor().connect("add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))
我想知道我是否可以使用相同的库或类似的库来实现我的目标,以突出显示给定轴/子图中的选定线。
下面是我的图形示例(多轴多线)和代码:
x1 = np.linspace(0, 10, num=6, endpoint=True)
y11 = abs(x1**2)
y12 = abs(x1/2)
x2 = np.linspace(1, 100, num=10, endpoint=True)
y21 = abs(x2/10)
y22 = abs(np.sqrt(x2))
x3 = np.linspace(50, 100, num=20, endpoint=True)
y31 = abs(x3**2)
y32 = abs(x3/5)
fig, axes = plt.subplots(3,1)
axes = axes.reshape(-1)
ax1 = axes[0]
ax2 = axes[1]
ax3 = axes[2]
ax1.plot(x1, y11, y12, picker = True)
ax2.plot(x2, y21, y22, picker = True)
ax3.plot(x3, y31, y32, picker = True)
def on_pick (event, fig, axes):
for ax in axes:
for line in ax.lines:
if event.artist is line:
ind = event.ind[0]
line.set_color('red')
fig.canvas.draw_idle()
fig.canvas.mpl_connect('pick_event', lambda event: on_pick(event,fig, axes))
但是,一旦我选择另一条线,突出显示的曲线就无法恢复到原始颜色。
请注意,这是我案例中的一个简化示例。