好吧,我知道这不是答案,但评论不允许我进行这种头脑风暴。我尝试了几件事,并注意到以下内容。当您打印axes
of the legendHandles
艺术家在你的 for 循环中,它返回None
对于散点图的两个图例 /PathCollection
艺术家。然而,在“正常”情节的情况下/Line2D
艺术家,它返回轴对象!甚至还不止于此;即使在终端中它们的表示似乎是相同的(AxesSubplot(0.125,0.11;0.775x0.77)
),如果你检查它们是否是== ax2
, 为了legendHandles
的艺术家legend1
它返回False
,而对于其中之一legend2
,它返回True
。这里发生了什么?
所以我不仅尝试删除legend1
from ax1
并再次添加到ax2
但也对legendHandles
目的。但它不允许我这样做:
NotImplementedError: cannot remove artist
在我看来,您似乎发现了一个错误,或者至少发现了不一致的行为。这是我到目前为止尝试过的代码,以防其他人想进一步尝试。
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
import numpy as np
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
X = np.linspace(0, 2*np.pi, 100)
Y1 = X**0.5 * np.sin(X)
Y2 = -np.cos(X)
USE_LINES = True # <--- set this to True or False to test both cases.
if USE_LINES:
ax1.plot(X, Y1, color="green", label="$Y_1$")
ax2.plot(X, Y2, color="red", label="$Y_2$")
else:
ax1.scatter(X, Y1, color="green", label="$Y_1$")
ax2.scatter(X, Y2, color="red", label="$Y_2$")
# Put both legends on ax2 so that pick events also work for ax1's legend.
legend1 = ax1.legend(loc="upper left")
legend2 = ax2.legend(loc="upper right")
legend1.remove()
ax2.add_artist(legend1)
# legend1.legendHandles[0].remove()
# ax2.add_artist(legend1.legendHandles[0])
for n, legend in enumerate((legend1, legend2)):
legend_item = legend.legendHandles[0]
legend_item.set_gid(n+1)
legend_item.set_picker(10)
print(
f'USE_LINES = {USE_LINES}', f'legend{n+1}',
legend_item.axes.__repr__() == legend.axes.__repr__(),
legend_item.axes == legend.axes,
legend_item.axes.__repr__() == ax2.__repr__(),
legend_item.axes == ax2, type(legend_item),
)
# When a legend element is picked, hide/show the associated curve.
def on_graph_pick_event(event):
gid = event.artist.get_gid()
print(f"Picked Y{gid}'s legend.")
ax = {1: ax1, 2: ax2}[gid]
artist = ax.lines[0] if USE_LINES else ax.collections[0]
artist.set_visible(not artist.get_visible())
plt.draw()
fig.canvas.mpl_connect("pick_event", on_graph_pick_event)
plt.show()