turtle库函数应用
import turtle as t t定义为turtle对象;相当于t=turtle.Turtle()
t.hideturtle() hideturtle()隐藏画笔的turtle形状;showturtle()显示画笔的turtle形状
3.drawLine()函数 drawLine(t,x1,y1,x2,y2) 画一条直线,从起始点(x1,y1)出发,到(x2,y2)结束
4.笔画控住命令 up() 笔画抬起,笔头移动,不划线 down() 笔画落下,划线移动 pencolor()设置画笔颜色 goto(x,y)画笔移动到(x,y)位置
turtle画折线图 import turtle as t import numpy as np fo = open('yiqun') lines = fo.readlines() numberoflines =len(lines) print(numberoflines) A = np.zeros((numberoflines,2), dtype=float) #先创建一个 3x3的全零方阵A,并且数据的类型设置为float浮点型 # 把全部数据文件读到一个列表lines中 A_row = 0 # 表示矩阵的行,从0行开始 for line in lines: # 把lines中的数据逐行读取出来 list = line.strip('\n').split(' ') # 处理逐行数据:strip表示把头尾的'\n'去掉,split表示以空格来分割行数据,然后把处理后的行数据返回到list列表中 A[A_row:] = list[0:2] # 把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行 A_row += 1 # 然后方阵A的下一行接着读 # print(line) print(A) # 打印 方阵A里的数据 # 关闭文件 fo.close() t.screensize(2000,2000,"white") t.setup(1200,800) def main(): # 从(0,0)到(300,0)画直线;相当于画出来x轴 drawLine(t,0,0,900,0) t.write('x',font=("Arial",10)) # 从(0,0)到(0,300)画直线,相当于画出来y轴 drawLine(t,0,0,0,900) t.write('y', font=("Arial", 10)) drawmark(t) # 连线(10,200)到(200,30);在一个for循环里面执行该函数,可以画出来折线图 for _ in range(numberoflines-1): drawwithdot(t,A[_][0]/2,A[_][1]/2,A[_+1][0]/2,A[_+1][1]/2) # 画y轴和x轴间隔 def drawLine(t,x1,y1,x2,y2): t.up() t.goto(x1,y1) t.down() t.pencolor("red") t.goto(x2,y2) def drawwithdot(t,x1,y1,x2,y2): t.pencolor("black") t.up() t.goto(x1,y1) t.dot() t.down() t.goto(x2,y2) t.dot() def drawmark(t): for i in range(30): # 画100个x轴上的间隔。一个间隔长3,间隔距离5. drawLine(t,30*i,3,30*i,0) t.up() t.goto(30*i,-20) t.down() t.write(60 * i, align="center") for i in range(30): # 画100个y轴上的间隔。一个间隔长5,间隔距离6. drawLine(t,0,30*i,3,30*i) t.up() t.goto(-20,30*i ) t.down() t.write(60 * i, align="center") main() # 窗口不关闭 t.done() matplotlib画图 import numpy as np import matplotlib.pyplot as plt xs = [] ys = [] fp=open("yiqun", "r") lines = fp.readlines() for line in lines: x, y = line.strip().split(' ') xs.append(float(x)) ys.append(float(y)) # plt.rc("font",family='zuiyou') plt.plot(xs, ys, 'r-', label='best') plt.xlabel('x') plt.ylabel('y') plt.legend() plt.show()