如何使用熊猫散布绘图系列

问题描述 投票:15回答:4

我有这个系列:

print series.head()
print type(series)
print series.index

year
1992    36.222222
1993    53.200000
1994    49.400000
1995    34.571429
1996    39.200000
Name: ranking, dtype: float64
<class 'pandas.core.series.Series'>

Int64Index([1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014], dtype='int64', name=u'year')

我正在尝试绘制散点图,但是在访问序列中的索引和值时遇到了麻烦。

任何指针将不胜感激。

python pandas
4个回答
19
投票

[我相信熊猫系列不支持kind ='scatter',如果在系列上看t0调用.plot()。

我相信列夫的答案是最好的,适合与大熊猫一起使用。我使用matplotlib pyplot,它的工作方式与他的示例类似。

import matplotlib.pyplot as plt
plt.scatter(ser.index, ser)
plt.show()

也许尝试一下:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)

year = [1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014]
value = np.random.rand(23)

ser =  pd.Series(index = year,data=value)
df =ser.to_frame()

df.reset_index(inplace=True)
df.columns = ['year','value']
df.plot(kind='scatter',x='year',y='value')
plt.show()

enter image description here


27
投票

我认为最简单的是

series.plot(style='.')

7
投票

喜欢吗?

import pylab
pylab.scatter(series.index, series)

0
投票

我发现的最简单的方法是使用reset_index(),它将返回一个数据序列,其中系列索引为一列。因此,这是从系列转换为数据框的一种非常酷的方法。

一旦使用数据框,就可以使用熊猫图功能:

df = series.reset_index()
df.plot(x="x_col", y="y_col", kind="scatter")
© www.soinside.com 2019 - 2024. All rights reserved.