Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从子批次元素中获取已打印标签的列表,以避免在图例中出现双条目_Python_Python 3.x_Matplotlib_Legend_Subplot - Fatal编程技术网

Python 从子批次元素中获取已打印标签的列表,以避免在图例中出现双条目

Python 从子批次元素中获取已打印标签的列表,以避免在图例中出现双条目,python,python-3.x,matplotlib,legend,subplot,Python,Python 3.x,Matplotlib,Legend,Subplot,考虑Python中的此函数: def plots(fields, dev_data, plot_dict, width, color='b', offset=0, l=""): ii = 0 for f in fields: ind = np.arange(len(dev_data[ii])) # the x locations for the groups import pdb plot_dict[f][1].bar(ind +

考虑Python中的此函数:

def plots(fields, dev_data, plot_dict, width, color='b', offset=0, l=""):
    ii = 0
    for f in fields:
        ind = np.arange(len(dev_data[ii])) # the x locations for the groups
        import pdb
        plot_dict[f][1].bar(ind + offset, dev_data[ii], width, color=color, label=l, bottom=0)
        if (len(f)>1): #this is used to create a bottom ex. 
            plot_dict[f][1].bar(ind + offset, dev_data[ii+1], width, color='c', label= "free Memory", bottom=dev_data[ii])
            ii += 1
        ii += 1
它获取一个输入数据列表(
dev_data
),并将其绘制为条形图<代码>字段是包含数据信息的元组列表。通常每个元组有一个元素,但如果它有2个,则意味着
devu data
中的2个后续数据必须一个接一个地绘制

该函数在每次运行时传递的
子批次上绘制数据。问题如下:如果我使用相同的
plt.subfigures
列表调用
plt
两次,它将执行此部分:

           if (len(f)>1): #this is used to create a bottom ex. 
                plot_dict[f][1].bar(ind + offset, dev_data[ii+1], width, color='c', label= "free Memory", bottom=dev_data[ii])
                ii += 1
两次使用相同的值打印标签两次,如图所示

为了避免这种行为,我希望获得绘图中的所有现有标签,这样,如果已经存在,我就不会将
label
参数传递给
bar
方法,类似于:

if (len(f)>1): #this is used to create a bottom ex. 
    if "free Memory" not in plot_dict[f][1].get_existing_labels():
        plot_dict[f][1].bar(ind + offset, dev_data[ii+1], width, color='c', label= "free Memory", bottom=dev_data[ii])
    else:
        plot_dict[f][1].bar(ind + offset, dev_data[ii+1], width, color='c', bottom=dev_data[ii])
    ii += 1
matplotlib轴类返回例如
['[0]'、'[1]'、'free memory']
时是否存在类似于
get\u existing\u labels
的内容

希望我的问题清楚。 下面是一个最小的完整且可验证的示例:

import matplotlib.pyplot as plt
import numpy as np
def plots(fields, dev_data, plot_dict, width, color='b', offset=0, l=""):
    ii = 0
    for f in fields:
        ind = np.arange(len(dev_data[ii])) # the x locations for the groups
        import pdb
        plot_dict[f][1].bar(ind + offset, dev_data[ii], width, color=color, label=l, bottom=0)
        if (len(f)>1): #this is used to create a bottom ex. (RAM_used, RAM_free)
            plot_dict[f][1].bar(ind + offset, dev_data[ii+1], width, color='c', label= "free Memory", bottom=dev_data[ii])
            ii += 1
        ii += 1

fields = [('avg_latency',), ('Successful requests',), ('CPU',), ('RAM', 'free RAM')]
dev_data  = [[8.583309839300817, 171.69371585480965, 1094.40896667813, 189.20147247618982], [100.0, 100.0, 100.0, 100.0], [4.8860107086666815, 35.27584414319996, 76.51036722223547, 41.620512010866655], [1416.4132789999996, 1498.8874527999992, 1825.9473847058837, 3796.161298666671], [4585.911099999997, 4399.862112000003, 3348.1968705882373, 521.1009743727773]]
dev_data2  = [[8.583309839300817, 171.69371585480965, 1094.40896667813, 189.20147247618982], [100.0, 100.0, 100.0, 100.0], [4.8860107086666815, 35.27584414319996, 76.51036722223547, 41.620512010866655], [1416.4132789999996, 1498.8874527999992, 1825.9473847058837, 3796.161298666671], [4585.911099999997, 4399.862112000003, 3348.1968705882373, 521.1009743727773]]

d = list()
d.append(dev_data)
d.append(dev_data2)

colors = ['y','b','r','g','y','c']
plot_d = dict(zip(fields,[ plt.subplots()for l in fields]))
width =0.2
l = ['a','b']
for ii, el in enumerate(d):
    plots(fields, d[ii], plot_d, width, colors[ii], width*(ii-1), [ii])
plt.legend()
plt.show()

确实存在这样的函数来获取图例和句柄。它被称为
get\u legend\u handles\u labels()
,并应用于当前轴。这将返回图例句柄和标签。第一个元素(索引
[0]
)表示句柄,第二个元素(索引
[1]
)表示标签


具体来说,请使用以下代码行

import numpy as np
def plots(fields, dev_data, plot_dict, width, color='b', offset=0, l=""):
    ii = 0
    for f in fields:
        ind = np.arange(len(dev_data[ii])) # the x locations for the groups
        import pdb
        plot_dict[f][1].bar(ind + offset, dev_data[ii], width, color=color, label=l, bottom=0)
        if (len(f)>1): #this is used to create a bottom ex. 
            if "free Memory" not in plot_dict[f][1].get_legend_handles_labels()[1]:
                plot_dict[f][1].bar(ind + offset, dev_data[ii+1], width, color='c', label= "free Memory", bottom=dev_data[ii])
            else:
                plot_dict[f][1].bar(ind + offset, dev_data[ii+1], width, color='c', bottom=dev_data[ii])
            ii += 1
        ii += 1