您的位置:首页 > 编程语言 > Python开发

python 从文件导入数据画图

2017-08-04 09:41 330 查看
今天早晨花了三个小时解决了一个bug,症结在于忽略了turtle库中colormode的限制。在这里做一个复盘,希望其他初学者能够避免这个错误。

"""
Created on Fri Aug  4 06:27:45 2017

@author: suqi791776
"""

import turtle #inport Turtle
def main():
# firstly setup the window
turtle.title("plot the graph according file data")
turtle.setup(800, 600, 0, 0)

# secondly setup the parameters of pen
p = turtle.Turtle()
p.color = ("red")
p.pensize(5)
p.speed(2)

#thirly read the file
result = []
infile = open("data.txt","r")
for line in infile:
result.append(list(map(float,line.split(',')))) #put datas
#in a list; list(map(function,list)) is correct usage, not
#map(function,list)
print(result)

#plot the graph
for i in range(len(result)):
p.pencolor((result[i][3],result[i][4],result[i][5]))
#if turtle.colormode(1), the parameter in
#p.pencolor((r,g,b)) must between 0 and 1.
p.forward(result[i][0])
if result[i][1]:
p.left(result[i][2])
else:
p.right(result[i][2])
p.goto(0,0) #return the original point

main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python