4

好的,问题很简单:

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

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 上,但我不知道该怎么做。

4

1 回答 1

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
于 2013-04-19T16:46:36.553 回答