辅助 y 轴上的 Matplotlib 选择器事件 [英] Matplotlib picker event on secondary y-axis

查看:38
本文介绍了辅助 y 轴上的 Matplotlib 选择器事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用选择器事件(单击点并打印其坐标),但是要在具有次要y轴的图上启用

I want to enable picker event (clicking on a dot and its coordinates are printed), but on a plot with a secondary y-axis.

例如,这是从 twinx 示例.选择器事件以某种方式仅在第二个轴(正弦线)上启用:

For example, this is adopted from the twinx example. The picker event is somehow only enabled on the second axis (sine line):

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()

推荐答案

这是 twinx 的标准行为.从文档:

对于那些在使用twinx时挑选"艺术家的人,仅在最顶部的轴上调用挑选事件.

For those who are 'picking' artists while using twinx, pick events are only called for the artists in the top-most axes.

这篇关于辅助 y 轴上的 Matplotlib 选择器事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆