2013-02-26 108 views
1

我試圖把一個散點圖pylab到目前爲止已經悲慘地失敗了。我不是這樣的程序員,所以請耐心等待。散點圖pylab:排列軸和數據

我已經包含在CSV文件與周圍60K線兩列數據組成的數據集。這裏有一個例子:

100000000012640,0.888888888888889 
100000000105442,0.777777777777778 
100000000206866,1.0 
100000000304930,0.777777777777778 
100000000583236,0.888888888888889 
100000000683528,0.777777777777778 
718435316,1.0 
718494043,0.777777777777778 
718602951,0.777777777777778 
718660499,0.777777777777778 
718766852,1.0 
718795104,1.0 
718862926,0.777777777777778 
718927526,0.777777777777778 
718952836,1.0 
719102865,0.777777777777778 
719156726,1.0 
719213511,1.0 
719425334,1.0 
719452158,1.0 
719493947,0.777777777777778 
719566609,1.0 
720090346,0.777777777777778 
720127760,0.777777777777778 
720143948,0.944444444444444 
720221566,1.0 
720256688,0.944444444444444 
720349817,0.777777777777778 
720380601,0.777777777777778 
720446322,1.0 
720524740,1.0 
720560353,1.0 
720594066,0.777777777777778 
720673388,1.0 
720716865,0.777777777777778 
720730249,1.0 
720774433,1.0 

我的目標是繪製這個數據的散點圖,用數據對x軸的第一行和y軸的第二排。 x軸的值按降序排列,從所示的值開始並結束於999963505. y軸的值始終在0和1之間。

這是我試過的(使用「ipython - -pylab「):

data = loadtxt('./data/OD-4322/facebookID.csv', unpack=True, dtype=('float', 'float'), delimiter=',') 
scatter(data[0],data[1]) 

這讓我的東西,類似於散點圖,但並不完全是我正在尋找:

http://content.screencast.com/users/FernandoGarridoVaz/folders/Jing/media/a0df81c5-2dbb-4e93-8e18-3c9db07728f5/00000793.png

(我會直接發佈圖片,但我的在該網站的聲譽不允許它)。

我怎樣才能讓這個讓x軸在同一範圍內的我的價值觀?爲什麼我的情節點都堆積在0和1上,實際上他們分佈在0到1之間的所有地方?

+0

能否請您從整個文件的10-20行的隨機子樣本取代數據樣本,您將得到更好的結果?您提供的範圍太小,無法重現您的問題。如果您可以以'np.array'格式發佈,可以簡單地剪切並粘貼到ipython,會很棒。 – 2013-02-26 22:35:43

回答

1

Pylab使用numpy的,你可以看一下所提供的數據格式here。在第一列中使用非常高的數字,並且不需要浮點雙精度,但是對於較高的整數值。看看您貼上了示例數據:

>>> x = np.loadtxt('./temp.dat', unpack=True, dtype=('float'), delimiter=',')[0] 
>>> x 
array([ 1.00000000e+14, 1.00000000e+14, 1.00000000e+14, 
    1.00000000e+14, 1.00000001e+14, 1.00000001e+14]) 
>>> x = np.loadtxt('./temp.dat', unpack=True, dtype=('uint64'), delimiter=',')[0] 
>>> x 
array([100000000012640, 100000000105442, 100000000206866, 100000000304930, 
    100000000583236, 100000000683528], dtype=uint64) 
>>> y = np.loadtxt('./temp.dat', unpack=True, dtype=('float'), delimiter=',')[1] 
>>> scatter(x,y) 

注意,你在你的行scatter(data[0],data[1])做什麼,只是loadtxt()聲明爲兩列後完成。第一個函數以float形式讀取後顯示您的數據。使用讀入的數據作爲`uint64'將幫助你處理散點圖。

好一點,從開始:matplotlib gallery

編輯回答您的意見,更好地控制讀取輸入的數據:

# create python lists to store the data 
x_vals = [] 
y_vals = [] 
#open file and read in a list containing all lines as string 
f = open("./temp.dat","r") 
lines = f.readlines() 
#Go through the lines 
    #strip() takes away "\n" characters and such 
    #split(",") creates a list of the string line splitted into (here: 2) substrings 
for line in lines: 
    x,y = line.strip().split(",") 
    #append values to their lists and apply the right format 
    x_vals.append(np.uint64(x)) 
    y_vals.append(np.float64(y)) 

scatter(x_vals,y_vals) 
#or just plot the data as points using: 
plot(x_vals,y_vals,"o") 

您的數據的最大值和最小值之間存在非常巨大的範圍, 當你把一組到小和大量

+0

謝謝@schuh。使用UINT64我曾試圖,但我得到以下錯誤:'項= [在拉鍊CONV(VAL)爲(CONV,val)的(轉換器,瓦爾斯)] ValueError異常:無效字面長()與底座10:「0.888888888888889 ' – 2013-02-27 14:31:28