簡體   English   中英

輔助 y 軸上的 Matplotlib 選擇器事件

[英]Matplotlib picker event on secondary y-axis

我想啟用選擇器事件(單擊一個點並打印其坐標),但是在帶有輔助 y 軸的繪圖上。

例如,這是從twinx 示例中采用的。 選擇器事件以某種方式僅在第二個軸(正弦線)上啟用:

import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b.',picker=5)
ax1.set_xlabel('time (s)')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
ax1.tick_params('y', colors='b')

ax2 = ax1.twinx()
s2 = np.sin(2 * np.pi * t)
ax2.plot(t, s2, 'r.',picker=5)
ax2.set_ylabel('sin', color='r')
ax2.tick_params('y', colors='r')


def onpick1(event):
    thisline = event.artist
    xdata = thisline.get_xdata()
    ydata = thisline.get_ydata()
    ind = event.ind
    print('Point: ', zip(np.take(xdata, ind), np.take(ydata, ind)))
fig.canvas.mpl_connect('pick_event', onpick1)
fig.tight_layout()
plt.show()

您只需要更改 zorder:

ax.set_zorder(axTwin.get_zorder() + 1)

這是twinx的標准行為。 文檔

對於那些在使用雙胞胎時“挑選”藝術家的人來說,挑選活動僅針對最頂層的藝術家。

暫無
暫無

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

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