为什么图例选择仅适用于"ax.twinx()"而不适用于"ax"?

编程入门 行业动态 更新时间:2024-10-28 21:21:14
本文介绍了为什么图例选择仅适用于"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"?

本文发布于:2023-08-02 03:51:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1274384.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   而不   图例   quot   twinx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!