为什么图例选择仅适用于"ax.twinx()"而不适用于"ax"? [英] Why does legend-picking only works for `ax.twinx()` and not `ax`?

查看:109
本文介绍了为什么图例选择仅适用于"ax.twinx()"而不适用于"ax"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天.下面提供的最少代码允许用户单击图例元素以隐藏/显示相关数据集.出于某种原因,尽管代码高度常规"并且没有以不同的方式处理最后一个 ax,但它仅适用于其中一个 axes.第一个斧头似乎没有选择 pick_event s.如何解决这个问题?

Good day. The minimal code provided below allows the user to click on a legend element to hide/show the associated data set. For some reason, it only works on one of the axes despite the code being highly "regular" and not treating the last ax in a different way. The first ax does not seem to pick pick_events. How to fix that?

点击气泡进行测试:

import numpy as np
import matplotlib.pyplot as plt

# Create dummy data.
fig = plt.gcf()
ax1 = plt.gca()
ax2 = ax1.twinx()
X = np.arange(-5, +5.01, 0.5)
Y1 = -X**2
Y2 = -0.5*X**2
ax1.scatter(X, Y1, color="red", label="1")
ax2.scatter(X, Y2, color="blue", label="2")
ax1.legend(loc="upper left")
ax2.legend(loc="upper right")
ax1.set_ybound(+5, -30)
ax2.set_ybound(+5, -30)

# Enable the pickable legend elements.
for ax in (ax1, ax2):
    for legend_item in ax.get_legend().legendHandles:
        legend_item.set_gid("1" if ax is ax1 else "2")
        legend_item.set_picker(10)

# Connect the pick event to a function.
def hide_or_show_data(event):
    """Upon clicking on a legend element, hide/show the associated data."""

    artist = event.artist
    gid = artist.get_gid()

    if gid == "1":
        scatter = ax1.collections[0]
    elif gid == "2":
        scatter = ax2.collections[0]

    scatter.set_visible(not scatter.get_visible())
    plt.draw()        

fig.canvas.mpl_connect("pick_event", hide_or_show_data)

我的直觉是 ax1 会忽略事件,因为如果有意义的话,它位于" ax2 以下".

My gut feeling is that ax1 ignores events because it's "below" ax2 if that makes any sense.

推荐答案

您可以为上轴中的下轴创建图例.然后选择事件将只在上轴触发.

You can create the legend for the lower axes in the upper axes. Then pick-events will only be fired in the upper axes.

import numpy as np
import matplotlib.pyplot as plt

# Create dummy data.
fig = plt.gcf()
ax1 = plt.gca()
ax2 = ax1.twinx()
X = np.arange(-5, +5.01, 0.5)
Y1 = -X**2
Y2 = -0.5*X**2
ax1.scatter(X, Y1, color="red", label="1")
ax2.scatter(X, Y2, color="blue", label="2")
ax1.set_ybound(+5, -30)
ax2.set_ybound(+5, -30)


h,l=ax1.get_legend_handles_labels()
leg1 = ax2.legend(h,l,loc="upper left")
leg2 = ax2.legend(loc="upper right")
ax2.add_artist(leg1)


# Enable the pickable legend elements.
for leg in [leg1, leg2]:
    for legend_item in leg.legendHandles:
        legend_item.set_gid("1" if leg is leg1 else "2")
        legend_item.set_picker(10)

# Connect the pick event to a function.
def hide_or_show_data(event):
    """Upon clicking on a legend element, hide/show the associated data."""

    artist = event.artist
    gid = artist.get_gid()

    if gid == "1":
        scatter = ax1.collections[0]
    elif gid == "2":
        scatter = ax2.collections[0]

    scatter.set_visible(not scatter.get_visible())
    plt.draw()        

fig.canvas.mpl_connect("pick_event", hide_or_show_data)

plt.show()

这篇关于为什么图例选择仅适用于"ax.twinx()"而不适用于"ax"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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