为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕仙森 2023-07-05 18:03:00
这是我计划用于创建条形图的代码。忽略下一行。我编写此代码只是为了平衡代码和细节。import numpy as npimport pandas as pdimport matplotlib.pyplot as pltdef 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 : 各个国家在冬季参加的次数
查看完整描述

1 回答

?
森林海

TA贡献2011条经验 获得超2个赞

只是改变

x=np.arange(11)

to 

x = np.arange(len(df))

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

to

plt.xticks(x,Countries,rotation=30)


查看完整回答
反对 回复 2023-07-05
  • 1 回答
  • 0 关注
  • 109 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信