【问题标题】:ValueError shape mismatch objects cannot be broadcast to a single shapeValueError 形状不匹配对象无法广播到单个形状
【发布时间】:2021-05-22 07:11:19
【问题描述】:

我尝试在 matplotlib 中绘制第三个堆叠条,之后是 stackoverflow 上的这篇文章:A third stacked bar in matplotlib

但是我有一个标题中提到的值错误,我不明白为什么我遵循了与帖子中完全相同的过程...

代码如下:

    count_faithfull = [2806862, 2798370, 2793310, 2786216, 2780627, 2773589, 2769471, 2764706, 2758180, 2753428, 2745208, 2745208]
    count_coward = [15088, 16216, 11432, 11658, 11862, 14706, 12864, 14006, 15327, 12614, 15002, 0]
    count_recruit = [0, 6372, 4564, 6273, 7668, 8746, 9241, 8801, 7862, 6782, 6463, 6463] 
    width = 0.35
    year = list(range(202001, 202013)) #We take the last year
    p1 = plt.bar(year, count_recruit, width, color='#d62728', )
    p2 = plt.bar(year, count_coward, width,  bottom=count_recruit)
    p3 = plt.bar(year, count_faithfull, width,  bottom=count_recruit+count_coward)
    
    plt.ylabel('Scores')
    plt.title('Scores by group and gender')
    plt.gca().ticklabel_format(useOffset=False)
    plt.yticks(np.arange(0, 81, 10))
    plt.legend((p1[0], p2[0], p3[0]), ('Faithfull', 'Coward', "Recruit"))
    
    plt.show()

有人可以帮助我吗?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    这是因为count_recruit+count_coward 创建了一个大列表,而不是像您链接的示例中那样将它们相加。在示例中,他们使用了 numpy 数组,这些数组由 + 符号相加。

    您可以先按元素添加列表,然后使用它们:

    from matplotlib import pyplot as plt
    import numpy as np
    count_faithfull = [2806862, 2798370, 2793310, 2786216, 2780627, 2773589, 2769471, 2764706, 2758180, 2753428, 2745208,
                       2745208]
    count_coward = [15088, 16216, 11432, 11658, 11862, 14706, 12864, 14006, 15327, 12614, 15002, 0]
    count_recruit = [0, 6372, 4564, 6273, 7668, 8746, 9241, 8801, 7862, 6782, 6463, 6463]
    width = 0.35
    year = list(range(202001, 202013))  # We take the last year
    p1 = plt.bar(year, count_recruit, width, color='#d62728', )
    p2 = plt.bar(year, count_coward, width, bottom=count_recruit)
    L=[i+j for i,j in zip(count_coward,count_recruit)]
    p3 = plt.bar(year,count_recruit, width, bottom=L)
    
    plt.ylabel('Scores')
    plt.title('Scores by group and gender')
    plt.gca().ticklabel_format(useOffset=False)
    plt.yticks(np.arange(0, 81, 10))
    plt.legend((p1[0], p2[0], p3[0]), ('Faithfull', 'Coward', "Recruit"))
    plt.show()
    
    

    【讨论】:

      【解决方案2】:

      问题如下:count_cowardcount_recruitlists,所以当你使用 + 时,它不是在做元素方面的 sum,相反,它只是创建一个包含所有元素的更大列表在两个列表中:

      print(count_recruit + count_coward)
      
      >>> [0, 6372, 4564, 6273, 7668, 8746, 9241, 8801, 7862, 6782, 6463, 6463, 15088, 16216, 11432, 11658, 11862, 14706, 12864, 14006, 15327, 12614, 15002, 0]
      

      因此,您的大小不匹配,因为 yearcount_faithfull 的长度等于 12。 因此,您需要对 2 个等长列表进行元素总和:

      选项 1:使用 numpy

      import numpy as np
      
      
      p3 = plt.bar(
          year, count_faithfull, width,  
          bottom=np.array(count_recruit)+np.array(count_coward)
      )
      

      选项 2:使用列表推导

      p3 = plt.bar(
          year, count_faithfull, width,  
          bottom=[x + y for x, y in zip(count_recruit, count_coward)]
      )
      

      【讨论】:

        猜你喜欢
        • 2013-06-01
        • 1970-01-01
        • 2021-05-13
        • 2021-02-27
        • 1970-01-01
        • 2019-02-16
        • 2023-02-25
        • 2020-04-28
        相关资源
        最近更新 更多