Matplotlib Stackplot图例错误

 众大文化_724 发布于 2023-02-13 13:04

我试图为我的stackplot包含一个图例.我知道你不能以正常的方式做到这一点所以我按照这个类似的帖子的说明,但仍然有一个错误.

x=data[:,-1]
y1=map(int,data[:,1])
y2=map(int,data[:,2])
y3=map(int,data[:,3])
y4=map(int,data[:,4])
y5=map(int,data[:,5])
y6=map(int,data[:,6])
y7=map(int,data[:,7])
y8=map(int,data[:,8])
y9=map(int,data[:,9])
y10=map(int,data[:,0])

xnew=np.linspace(0,len(x),50)

smooth_y1=spline(np.arange(len(x)),y1,xnew)
smooth_y2=spline(np.arange(len(x)),y2,xnew)
smooth_y3=spline(np.arange(len(x)),y3,xnew)
smooth_y4=spline(np.arange(len(x)),y4,xnew)
smooth_y5=spline(np.arange(len(x)),y5,xnew)
smooth_y6=spline(np.arange(len(x)),y6,xnew)
smooth_y7=spline(np.arange(len(x)),y7,xnew)
smooth_y8=spline(np.arange(len(x)),y8,xnew)
smooth_y9=spline(np.arange(len(x)),y9,xnew)
smooth_y10=spline(np.arange(len(x)),y10,xnew)

plt.stackplot(np.arange(50),smooth_y1,smooth_y2,smooth_y3,smooth_y4,smooth_y5,smooth_y6,smooth_y7,smooth_y8,smooth_y9,smooth_y10)


plt.ylim([0,30])
plt.legend([smooth_y1,smooth_y2,smooth_y3,smooth_y4,smooth_y5,smooth_y6,smooth_y7,smooth_y8,smooth_y9,smooth_y10],['hfsdkjfhs','sldjfhsdkj','sdrtryf','sdfsd','sdkjf','sdfsd','sdrtdf','sfsd','sdaaafs','sdffghs'])
plt.show()

但是,与图例的行发生错误.它说

File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3381, in legend
    ret = gca().legend(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4778, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\legend.py", line 366, in __init__
    self._init_legend_box(handles, labels)
  File "C:\Python27\lib\site-packages\matplotlib\legend.py", line 606, in _init_legend_box
    handler = self.get_legend_handler(legend_handler_map, orig_handle)
  File "C:\Python27\lib\site-packages\matplotlib\legend.py", line 546, in get_legend_handler
    if orig_handle in legend_handler_keys:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

有谁知道如何解决这个问题?

1 个回答
  • from matplotlib.patches import Rectangle
    
    label_list = ['hfsdkjfhs','sldjfhsdkj','sdrtryf','sdfsd','sdkjf','sdfsd','sdrtdf','sfsd','sdaaafs','sdffghs']
    
    x = data[:, -1]
    xnew=np.linspace(0,len(x),50)
    
    smoothed_data = [spline(np.arange(len(x)),data[:, j%10],xnew) 
                     for j in range(1, 11)]
    
    # get a figure and axes
    fig, ax = plt.subplots()
    # make the stack plot
    stack_coll = ax.stackplot(xnew, smoothed_data)
    # set the ylim
    ax.set_ylim([0,30])
    # make proxy artists
    proxy_rects = [Rectangle((0, 0), 1, 1, fc=pc.get_facecolor()[0]) for pc in stack_coll]
    # make the legend
    ax.legend(proxy_rects, label_list)
    # re-draw the canvas
    plt.draw()
    

    2023-02-13 13:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有