图例选择-在图例上启用选择以打开或关闭原始行

短信

我尝试重现以下示例,但是失败了。因此,我要实现的目标是:我有多个数据框,应该单独绘制这些数据框-为此,我已经创建了一个UI。我想单击图例中的项目以打开或关闭该行。为此,我使用了以下示例

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 0.2, 0.1)
y1 = 2*np.sin(2*np.pi*t)
y2 = 4*np.sin(2*np.pi*2*t)

fig, ax = plt.subplots()
ax.set_title('Click on legend line to toggle line on/off')
line1, = ax.plot(t, y1, lw=2, label='1 HZ')
line2, = ax.plot(t, y2, lw=2, label='2 HZ')
leg = ax.legend(loc='upper left', fancybox=True, shadow=True)
leg.get_frame().set_alpha(0.4)


# we will set up a dict mapping legend line to orig line, and enable
# picking on the legend line
lines = [line1, line2]
lined = dict()
for legline, origline in zip(leg.get_lines(), lines):
    legline.set_picker(5)  # 5 pts tolerance
    lined[legline] = origline


def onpick(event):
    # on the pick event, find the orig line corresponding to the
    # legend proxy line, and toggle the visibility
    legline = event.artist
    origline = lined[legline]
    vis = not origline.get_visible()
    origline.set_visible(vis)
    # Change the alpha on the line in the legend so we can see what lines
    # have been toggled
    if vis:
        legline.set_alpha(1.0)
    else:
        legline.set_alpha(0.2)
    fig.canvas.draw()

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

如果将其复制粘贴有两个图形(我这样做是为了检查我的错误-因为我在生成多个图时出错),它会给我两个图形,但是我无法单击两个图形的图例,只能在最后一个图形上单击,这是我的代码

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 0.2, 0.1)
y1 = 2*np.sin(2*np.pi*t)
y2 = 4*np.sin(2*np.pi*2*t)

fig, ax = plt.subplots()
ax.set_title('Click on legend line to toggle line on/off')
Line1, = ax.plot(t, y1, lw=2, label='1 HZ')
Line2, = ax.plot(t, y2, lw=2, label='2 HZ')
Leg = ax.legend(loc='upper left', fancybox=True, shadow=True)
Leg.get_frame().set_alpha(0.4)


# we will set up a dict mapping legend line to orig line, and enable
# picking on the legend line
Lines = [Line1, Line2]
Lined = dict()
for Legline, Origline in zip(Leg.get_lines(), Lines):
    Legline.set_picker(5)  # 5 pts tolerance
    Lined[Legline] = Origline


def onpick(Event):
    # on the pick event, find the orig line corresponding to the
    # legend proxy line, and toggle the visibility
    Legline = Event.artist
    Origline = Lined[Legline]
    vis = not Origline.get_visible()
    Origline.set_visible(vis)
    # Change the alpha on the line in the legend so we can see what lines
    # have been toggled
    if vis:
        Legline.set_alpha(1.0)
    else:
        Legline.set_alpha(0.2)
    fig.canvas.draw()

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

t = np.arange(0.0, 0.2, 0.1)
y1 = 2*np.sin(2*np.pi*t)
y2 = 4*np.sin(2*np.pi*2*t)

fig, ax = plt.subplots()
ax.set_title('Click on legend line to toggle line on/off')
line1, = ax.plot(t, y1, lw=2, label='1 HZ')
line2, = ax.plot(t, y2, lw=2, label='2 HZ')
leg = ax.legend(loc='upper left', fancybox=True, shadow=True)
leg.get_frame().set_alpha(0.4)


# we will set up a dict mapping legend line to orig line, and enable
# picking on the legend line
lines = [line1, line2]
lined = dict()
for legline, origline in zip(leg.get_lines(), lines):
    legline.set_picker(5)  # 5 pts tolerance
    lined[legline] = origline


def onpick2(event):
    # on the pick event, find the orig line corresponding to the
    # legend proxy line, and toggle the visibility
    legline = event.artist
    origline = lined[legline]
    vis = not origline.get_visible()
    origline.set_visible(vis)
    # Change the alpha on the line in the legend so we can see what lines
    # have been toggled
    if vis:
        legline.set_alpha(1.0)
    else:
        legline.set_alpha(0.2)
    fig.canvas.draw()

fig.canvas.mpl_connect('pick_event', onpick2)

plt.show()

所以基本上我只是复制粘贴它。但这并没有达到我的预期。有任何建议吗?

朝日朝日

您正在使用一些在第一和第二图中具有相同名称的变量。第一个图形的事件处理试图访问在创建第二个图形时已被覆盖的变量,这就是失败的地方。

如果您确定使用唯一的变量名,那么一切都会起作用:

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 0.2, 0.1)
y1 = 2 * np.sin(2 * np.pi * t)
y2 = 4 * np.sin(2 * np.pi * 2 * t)

fig1, ax1 = plt.subplots()
ax1.set_title('Click on legend line to toggle line on/off')
Line11, = ax1.plot(t, y1, lw=2, label='1 HZ')
Line12, = ax1.plot(t, y2, lw=2, label='2 HZ')
Leg1 = ax1.legend(loc='upper left', fancybox=True, shadow=True)
Leg1.get_frame().set_alpha(0.4)

# we will set up a dict mapping legend line to orig line, and enable
# picking on the legend line
Lines1 = [Line11, Line12]
Lined1 = dict()
for Legline, Origline in zip(Leg1.get_lines(), Lines1):
    Legline.set_picker(5)  # 5 pts tolerance
    Lined1[Legline] = Origline


def onpick(Event):
    # on the pick event, find the orig line corresponding to the
    # legend proxy line, and toggle the visibility
    Legline = Event.artist
    Origline = Lined1[Legline]
    vis = not Origline.get_visible()
    Origline.set_visible(vis)
    # Change the alpha on the line in the legend so we can see what lines
    # have been toggled
    if vis:
        Legline.set_alpha(1.0)
    else:
        Legline.set_alpha(0.2)
    fig1.canvas.draw()


fig1.canvas.mpl_connect('pick_event', onpick)

t = np.arange(0.0, 0.2, 0.1)
y1 = 2 * np.sin(2 * np.pi * t)
y2 = 4 * np.sin(2 * np.pi * 2 * t)

fig2, ax2 = plt.subplots()
ax2.set_title('Click on legend line to toggle line on/off')
line21, = ax2.plot(t, y1, lw=2, label='1 HZ')
line22, = ax2.plot(t, y2, lw=2, label='2 HZ')
leg2 = ax2.legend(loc='upper left', fancybox=True, shadow=True)
leg2.get_frame().set_alpha(0.4)

# we will set up a dict mapping legend line to orig line, and enable
# picking on the legend line
lines2 = [line21, line22]
lined2 = dict()
for legline, origline in zip(leg2.get_lines(), lines2):
    legline.set_picker(5)  # 5 pts tolerance
    lined2[legline] = origline


def onpick2(event):
    # on the pick event, find the orig line corresponding to the
    # legend proxy line, and toggle the visibility
    legline = event.artist
    origline = lined2[legline]
    vis = not origline.get_visible()
    origline.set_visible(vis)
    # Change the alpha on the line in the legend so we can see what lines
    # have been toggled
    if vis:
        legline.set_alpha(1.0)
    else:
        legline.set_alpha(0.2)
    fig2.canvas.draw()


fig2.canvas.mpl_connect('pick_event', onpick2)

plt.show()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

关闭 gnuplot 直方图图例中的选择标题

如何链接图例迹线以进行图例和颜色选择?

如何修改图例计数选择标准?

发生多项选择时隐藏图例

是否可以使用 matplotlib 图例选择器选择图例文本区域而不是图例线?

选择图例名称以显示在dc.js条形图中

如何有选择地在图例键周围添加框

在ggplotly中,如何通过代码取消选择图例条目?

选择ggplot图例项目并保留调色板颜色

通过单击图例选择或突出显示地图上的数据

使用ggplot为单个图例值选择选择的颜色(HSV或HCL或RGB)

通过eventListener打开/关闭传单图例

仅选择冗余行,而不是原始行

如何在情节线图中有选择地隐藏图例?

如何将Rshiny select输入带到顶层?目前,宣传单图例妨碍了选择

用熊猫数据框进行matplotlib图例选择不起作用

在栅格堆栈中选择最大值并将图层名称显示为图例

取消选择时如何更改剑道图表图例项目的颜色?

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

如何从子图中的指定子图中选择图例句柄和标签?

在图旁边显示(离散)颜色条,作为(自动选择的)线条颜色的图例

Highcharter-在Rstudio中的代码中取消选择图例中的变量

具有事件选择功能的matplotlib中的可拖动图例

MatLab:可选打开图例

图例覆盖图例-Matplotlib

如何根据图例的颜色在图例上绘制图例?

如何与相应的行一起打开和关闭图例

MySQL仅选择冗余行,而忽略原始行

栏上的Highchart图例