在Python中绘制年份

 收藏

我的excel文件中有数据,如下所示,我已将其导入python并尝试绘制它们,

Year    Andhra Pradesh
1973-74 50.67
1977-78 69.66
1983    115.4
1993-94 288.7
1999-00 453.61
2004-05 603.82
2009-10 1233.76
2011-12 1753.96

但是,每当我运行命令时,都会得到一个空白图。

import pandas as pd
import matplotlib.pyplot as plt
data1= pd.read_excel('C:\\Users\\3004\\Desktop\\Variables.xlsx',2)
plt.plot(data1)

我想要x轴上的年份以及直方图或任何其他绘图上显示的值。但是,我收到一个空白图。

回复
  • dsit 回复

    You can use the DataFrame's own plotting methods: data1.plot()

  • 心碎谁买单 回复

    plt.plot(data1)
    

    退货

    TypeError:无法散列的类型:'numpy.ndarray'

    You need to pass x and y separatly in plt.plot(x,y):

    import pandas as pd
    import matplotlib.pyplot as plt
    
    data1 = pd.DataFrame([
        ['1973-74', 50.67],
        ['1977-78', 69.66],
        ['1983   ',115.4],
        ['1993-94',288.7],
        ['1999-00',453.61],
        ['2004-05',603.82],
        ['2009-10',1233.76],
        ['2011-12',1753.96]], 
        columns=['Year','Andhra Pradesh'])
    
    #data1= pd.read_excel('C:\\Users\\3004\\Desktop\\Variables.xlsx',2)
    
    plt.plot(data1['Year'], data1['Andhra Pradesh'])
    

    enter image description here