首页 > 解决方案 > 错误代码:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (7,) 和 (1, 7)

问题描述

我正在尝试做一个 plt 图。我不断收到 x 和 y 第一维不同的错误。我不明白为什么。我究竟做错了什么?

我在代码中添加了更多细节。

非常感谢!

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

dico_dtype={'Country Code':str,'Indicator Name' : str}

liste_annee=['1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978','1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987',      '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996','1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005','2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014','2015', '2016', '2017', '2020', '2025', '2030', '2035', '2040', '2045','2050', '2055', '2060', '2065', '2070', '2075', '2080', '2085', '2090','2095', '2100']

for annee in liste_annee: 
    dico_dtype[annee] = np.float

print (dico_dtype)


data = pd.read_csv ('C:/Users/s.guerin/Desktop/OPC/projet edtech/StatsCSV/EdStatsData.csv', dtype=dico_dtype)

datanew=data.dropna(axis=0, thresh=25)
datadrop= data.drop(['1990', '1995','2000', '2005', '2006', '2007','2008','1970','1988','2001','2002','2003','2004' ,'1971','1972','1973','1974','1975','1976','1977','1978','1979','1980','1985','1987', '1989','1991', '1981','1982','1983','1984','1986','1992','1993','1994', '1996','1997', '1998','1999', '2016', '2017', '2020', '2025', '2030', '2035', '2040', '2045','2050', '2055', '2060', '2065', '2070', '2075', '2080', '2085', '2090','2095', '2100'], axis=1) 

internet =datadrop.loc[(datadrop['Indicator Name']=="Internet users (per 100 people)"),:]

internet_ok=internet.loc[ (internet['moyenne'] > 61),:]

internet_ok=internet_ok.sort_values(by = '2009')

#courbe 进化 => 进化图

plt.figure(10, figsize=(50, 20))

plt.xlabel('Année') ; xtitre.set_fontsize(200)
plt.ylabel('%'); ytitre.set_fontsize(200)
titre = plt.title('Evolution par année des utilisateurs Internet en %'); titre.set_fontsize(80)
x=np.arange(2009,2016)

liste_country=['BHS', 'PRI', 'HRV', 'GUM', 'KWT', 'BHR', 'QAT', 'MAC','KNA', 'MYS']

for countrycode in liste_country:
    y = internet_ok[internet_ok['Country Code'] == countrycode]
    y = y[['2009', '2010','2011','2012', '2013','2014','2015'] ]
    y = np.reshape(y.values, (7,))  
    plt.plot(x,y, label = countrycode)

plt.grid(True)
plt.tick_params(axis = 'both', labelsize = 50)
plt.legend(loc=1, prop={'size': 50})

标签: matplotlibgraphdimension

解决方案


推荐阅读