Mr. Mikkola
Mr. Mikkola

Reputation: 43

Sage importing from csv and plotting numbers larger than 10

Okey, problem is simple:

I try to draw a simple scatter plot:

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

From this data:

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

This results in graph which works fine, until we came to values 12 15 18
It is graphed like this:

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

I tried the same with entering the values straight in:

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

This results in graph which works just fine, it gets plotted with no problems. I assume the problem is with csv.reader, but I have no idea what to do.

Upvotes: 4

Views: 1548

Answers (1)

Thanasis Petsas
Thanasis Petsas

Reputation: 4448

OK, you can try this:

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

Upvotes: 1

Related Questions