Python 如何为数据帧的特定子集绘制图形

Python 如何为数据帧的特定子集绘制图形,python,python-3.x,pandas,matplotlib,jupyter-notebook,Python,Python 3.x,Pandas,Matplotlib,Jupyter Notebook,我一直在尝试为我的数据的特定子集绘制一个图表。我的数据是从1960年到2018年的数据。然而,我只对仅使用特定列的变量和显示1981年以后数据的行绘制直方图感兴趣 到目前为止,我已经尝试使用两个变量进行绘图 x = df1y.index 它返回以下值: Int64Index([1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,

我一直在尝试为我的数据的特定子集绘制一个图表。我的数据是从1960年到2018年的数据。然而,我只对仅使用特定列的变量和显示1981年以后数据的行绘制直方图感兴趣

到目前为止,我已经尝试使用两个变量进行绘图

x = df1y.index
它返回以下值:

Int64Index([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],
       dtype='int64', name=' Time ')

y返回:

Resident Live-births(Number)
Time    
1981    41,000
1982    41,300
1983    39,300
1984    40,200
1985    41,100
1986    37,159
1987    42,362
1988    51,537
1989    46,361
1990    49,787
1991    47,805
1992    47,907
1993    48,739
1994    48,075
1995    46,916
1996    46,707
1997    45,356
1998    41,636
1999    41,327
2000    44,765
2001    39,281
2002    38,555
2003    35,474
2004    35,135
2005    35,528
2006    36,272
2007    37,074
2008    37,277
2009    36,925
2010    35,129
2011    36,178
2012    38,641
2013    35,681
2014    37,967
2015    37,861
2016    36,875
2017    35,444
输入后

x = df1y.index
y = df1.iloc[21:58, [15]]
plt.plot(x, y, 'o-')
我收到一个错误:

TypeError:不可损坏的类型:“numpy.ndarray”

使用

以你尝试走路的方式去做


但是,通常你不想自己计算子集索引,所以考虑一下像

之类的东西。
y = df1.loc[df1.index > 1981, 'name_of_your_column_15_here'].values
获取想要的(y-)值的numpy数组

为了获得更多的便利,只需尝试将
.plot()
直接应用于该系列(也适用于整个数据帧),然后看看会发生什么

idx_slice = df1.index > 1981
df1.loc[idx_slice, 'name_of_your_column_15_here'].plot()
y = df1.loc[df1.index > 1981, 'name_of_your_column_15_here'].values
idx_slice = df1.index > 1981
df1.loc[idx_slice, 'name_of_your_column_15_here'].plot()