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

2024-04-26 23:35:01 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,问题很简单:

我试着画一个简单的散点图:

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

根据这些数据:

^{pr2}$

这将产生一个运行良好的图形,直到我们得到值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但我不知道该怎么办。在


Tags: csvimport图形timeplotrangecolzip
1条回答
网友
1楼 · 发布于 2024-04-26 23:35:01

好的,你可以试试这个:

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

相关问题 更多 >