Sage从csv导入并绘制大于10的数字

时间:2013-04-19 16:31:11

标签: python csv scatter-plot sage

Okey,问题很简单:

我尝试绘制一个简单的散点图:

import csv

a = csv.reader(open(DATA+'testi1.csv'))

G = Graphics()

  for col in a:  
  time = col[0]  
  conversion = col[2]  
  x_series = time  
  y_series = conversion  
  plot = scatter_plot (zip(x_series,y_series))  
  G += plot 

G.set_axes_range(0, 20, 0, 20)

G

从这些数据:

1,2,3  
2,4,6  
3,6,9  
4,8,12  
5,10,15  
6,12,18  

这导致图表工作正常,直到我们得到值12 15 18
它是这样的:

1,3  
2,6  
3,9  
4,1  
5,1  
6,1

我尝试直接输入值:

G = Graphics()

x_series = (1,2,3,4,5,6)  
y_series = (3,6,9,12,15,18)  
plot = scatter_plot(zip(x_series,y_series))  
G += plot 

G.set_axes_range(0, 20, 0, 20)

G

这导致图表工作正常,它被绘制没有问题。 我认为问题出在csv.reader上,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

好的,你可以试试这个:

import csv

a = csv.reader(open(DATA+'testi1.csv'))
G = Graphics()

# create 2 lists so as to save the desired column fields
x_series=[]
y_series=[]

# iterate the csv file
for x,y,z in a:
  # append the first and third columns to
  # x_series and y_series list respectively
  x_series.append( int(x) )
  y_series.append( int(z) )

# then make the scatter plot
plot = scatter_plot(zip(x_series,y_series))  
G += plot 

G.set_axes_range(0, 20, 0, 20)

G