簡體   English   中英

如何使用Matplotlib突出顯示多軸圖中的選定線

[英]How to highlight selected line in a multiple axes figure using matplotlib

我正在嘗試編寫代碼以將選中的行(通過單擊或鼠標懸停)突出顯示為紅線。

我嘗試了一些解決方案,這些解決方案是我如何在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))

但是,一旦選擇另一條線,突出顯示的曲線將無法恢復為原始顏色。

在此處輸入圖片說明

在此處輸入圖片說明

請注意,這是我的案例的簡化示例。

import mplcursors

mplcursors.cursor(highlight=True).connect("add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))

一線解決了所有問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM