簡體   English   中英

如何使用 Pandas 散點圖系列

[英]How to Scatter Plot Series using Pandas

我有這個系列:

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')

我正在嘗試繪制散點圖,但無法訪問系列中的索引和值。

任何指針將不勝感激。

我認為最簡單的是:

系列用

series.plot(style='.')

對於數據框

df.plot(x='x_col', y='y_col', style='.')

如果在系列上查看 t0 調用 .plot(),我相信熊貓系列不支持 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()

在此處輸入圖片說明

像這樣?

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

我發現的最簡單的方法是使用reset_index() ,它將返回一個數據幀,以系列索引作為一列。 所以這是從系列轉換為數據幀的一種非常酷的方式。

使用數據框后,您可以使用熊貓繪圖功能:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM