修改列表中保存的对象的属性

时间:2019-02-10 01:08:42

标签: python matplotlib

我正在尝试编写一个程序来绘制数据。文件将具有不同的数据通道。我希望能够根据要查看的数据手动显示/隐藏每个频道。

我能够加载数据并精细绘制通道,并能够控制其初始可见性属性。我无法在代码中显示/隐藏代码,但是正在引用我喜欢且有效的示例。我认为问题在于将数据通道对象保存到列表中,原因是将使用的不同文件之间的数据通道数量未知。

fig, ax = plt.subplots()
plt.title(str(fileName))

#   List of the line objects about to be made
lines = []

#   Create a line for each channel of data
for i in range(0, len(channelList)):
    print(channelList[i])

    print("")
    print("Channel Data")

    channelObject = tdmsFile.object('stream', channelList[i])

    channelData = channelObject.data
    print(channelData)

    print("")
    print("Channel Time")

    channelTime = channelObject.time_track()
    print(channelTime)
    print("")

    #   Put the line object in the list, to edit attributes later
    lines.append(ax.plot(channelTime, channelData, label = str(channelList[i])))
    print(lines[i])

leg = ax.legend(loc='upper left', fancybox=True, shadow=True)
leg.get_frame().set_alpha(0.4)

lined = dict()
for legline, origline in zip(leg.get_lines(), lines):
    legline.set_picker(5)  # 5 pts tolerance
    lined[legline] = origline
    print(lined[legline])

def onpick(event):
    # on the pick event, find the orig line corresponding to the
    # legend proxy line, and toggle the visibility
    legline = event.artist
    print(legline)
    origline = lined[legline]
    print(origline)
    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()

弹出我的图,当我单击图例中的标签时,代码正在响应,但这是发生错误的地方:

Line2D(A001)

[<matplotlib.lines.Line2D object at 0x0000024EC606E278>]

Traceback (most recent call last):
  File "C:\Users\camkn\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cbook\__init__.py", line 215, in process
    func(*args, **kwargs)
  File "C:\Users\camkn\Documents\Python\VisualizeTDMS\VisualizeTDMS.py", line 85, in onpick
    vis = not origline.get_visible()
AttributeError: 'list' object has no attribute 'get_visible'

试图更改列表的属性而不是列表中的对象,但是我不知道如何修复代码以修改对象而不是列表的属性。

我要引用的代码是对实际对象进行硬编码,以获取固定数量的行,而不是使用列表来容纳数量未知的行对象。

谢谢您的帮助!

0 个答案:

没有答案