如何使用 matplotlib 在多轴图中突出显示选定的线
How to highlight selected line in a multiple axes figure using matplotlib
我正在尝试编写代码以将选定的行(通过单击或鼠标悬停)突出显示为红线。
我尝试了在 , , https://github.com/joferkington/mpldatacursor, https://mplcursors.readthedocs.io/en/stable/
中找到的一些解决方案
但大多数情况都适用于一个子图中的单个轴或两个轴。就我而言,我需要在多个子图中处理多行。
我试过:
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()))
我想知道我是否可以使用相同的库或类似的库来实现我的目标来突出显示给定 axes/subplot 中的选定行。
下面是我的图例(多轴多线)和代码:
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))
但是,一旦我选择了另一条线,突出显示的曲线就无法恢复到原来的颜色。
请注意,这是我案例中的一个简化示例。
import mplcursors
mplcursors.cursor(highlight=True).connect("add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))
一行解决所有问题!
我正在尝试编写代码以将选定的行(通过单击或鼠标悬停)突出显示为红线。
我尝试了在
但大多数情况都适用于一个子图中的单个轴或两个轴。就我而言,我需要在多个子图中处理多行。
我试过:
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()))
我想知道我是否可以使用相同的库或类似的库来实现我的目标来突出显示给定 axes/subplot 中的选定行。
下面是我的图例(多轴多线)和代码:
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))
但是,一旦我选择了另一条线,突出显示的曲线就无法恢复到原来的颜色。
请注意,这是我案例中的一个简化示例。
import mplcursors
mplcursors.cursor(highlight=True).connect("add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))
一行解决所有问题!