如何使用Pandas散布绘图系列

时间:2015-09-25 21:46:03

标签: python pandas

我有这个系列:

$sql .= implode(',', $valuesArr);

我正在尝试制作散点图,但我无法访问该系列中的索引和值。

任何指针都将受到赞赏。

4 个答案:

答案 0 :(得分:16)

我相信如果在一个系列中调用t0调用.plot(),pandas系列不支持kind ='scatter'。

我相信Lev的答案是最好的,适合与熊猫一起使用。我使用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

答案 1 :(得分:8)

我认为最简单的是

series.plot(style='.')

答案 2 :(得分:7)

喜欢这个吗?

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

答案 3 :(得分:0)

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

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

df = series.reset_index()
df.plot(x="x_col", y="y_col", kind="scatter")