ValueError 形状不匹配:对象无法广播到单个形状

这是我计划用于创建条形图的代码。忽略下一行。我编写此代码只是为了平衡代码和细节。


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

def bar1():

    df=pd.read_csv('C:\\Users\Bhuwan Bhatt\Desktop\IP PROJECT\Book1.csv',encoding= 'unicode_escape')

    x=np.arange(11)

    Countries=df['Country']

    STotalMed=df['SummerTotal']

    WTotalMed=df['WinterTotal']

    plt.bar(x-0.25,STotalMed,width=.2, label='Total Medals by Countries in Summer',color='g')

    plt.bar(x+0.25,WTotalMed,width=.2, label='Total Medals by Countries in Winter',color='r')

    plt.xticks(np.arange(11),Countries,rotation=30)

    plt.title('Olympics Data Analysis of Top 10 Countries',color='red',fontsize=10)

    plt.xlabel('Countries')

    plt.ylabel('Total Medals')

    plt.grid()

    plt.legend()

    plt.show()

bar1()

由于某种原因我收到此错误:


Traceback (most recent call last):

  File "C:/Users/Bhuwan Bhatt/Desktop/dsd.py", line 19, in <module>

    bar1()

  File "C:/Users/Bhuwan Bhatt/Desktop/dsd.py", line 10, in bar1

    plt.bar(x-0.25,STotalMed,width=.2, label='Total Medals by Countries in Summer',color='g')

  File "C:\Users\Bhuwan Bhatt\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\pyplot.py", line 2471, in bar

    return gca().bar(

  File "C:\Users\Bhuwan Bhatt\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\__init__.py", line 1438, in inner

    return func(ax, *map(sanitize_sequence, args), **kwargs)


INFO-----> SummerTimesPart : 各个国家在夏季参加的次数 WinterTimesPart : 各个国家在冬季参加的次数


慕仙森
浏览 110回答 1
1回答

森林海

只是改变x=np.arange(11) to&nbsp; x&nbsp;=&nbsp;np.arange(len(df))和plt.xticks(np.arange(11),Countries,rotation=30)&nbsp; to plt.xticks(x,Countries,rotation=30)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python