在 Python 中使用 for 循环删除图形标签

问题描述

我有一个图表显示这个https://i.stack.imgur.com/rYzkv.jpg 当一条彩色线(化学元素)被删除时,我希望我的相应注释标签也被删除。我通过将注释标签附加到列表来设置它,如果标签不在任何行中,请删除没有显示该行的标签

注释列表实际上会在删除行后更新以显示剩余的标签,但中间存在大量属性错误。仍然没有删除标签。我如何使用这个想法解决这个问题?

P.S.删除化学线的按钮位于我不想共享的其他文件中。超过 20 个不同类别的文件。所以希望用这个方法解决

def plot_element_peaks(peakClusters,x,hist,peakElementName):

annotation_list = []
minVal = get_bottom_val(hist)
ax = plt.gca()


for name,element in zip(peakElementName,peakClusters):

    color = next(ax._get_lines.prop_cycler)['color']

    biggestLine = element[0]
    for line in element[1:]:
        if line[1] > biggestLine[1]:
            biggestLine = line
    lineAmp = np.interp(biggestLine[0],hist) / biggestLine[1]

    for line in element:
        if lineAmp * line[1] > minVal:
            # this creates the element peak lines
            my_plot = plt.plot([line[0],line[0]],[minVal,lineAmp * line[1]],c=color)

            if line in biggestLine:
                name = ("+".join(map(str,name[:2])))

                my_labels = annotation_list.append(plt.annotate(name,xy=([line[0],lineAmp * line[1]]),textcoords="offset points",# how to position the text
                                   xytext=(0,10),# distance from text to points (x,y)
                                   c=color,size=8))

               
                for i in annotation_list:
                    if my_labels not in biggestline:     
                        try:
                            f.delete(i)
                            # possibly try to delete my_labels and create new plt.annotate with updated annotation list
                            print('removed element')
                        except AttributeError:
                            print('attribute error')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)