2013-02-26 57 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