赞
踩
这个项目还是有一点小瑕疵的,那就是总是超出那么一点来和没有加入圆滑处理
献上代码
import turtle as t t.colormode(255) t.speed(0) t.delay(0) def _draw_axis(op,xlab,ylab,p): t.hideturtle() xsize = max([i[0] for i in p]) + abs(min([i[0] for i in p])) ysize = max([i[1] for i in p]) + abs(min([i[1] for i in p])) canv = t.getcanvas() w,h = canv.winfo_width() - 30,canv.winfo_height() - 30 if(op == 2): xfac = w / 2 / xsize yfac = h / 2 / ysize t.penup() t.goto(0,0) t.pendown() t.pensize(5) t.goto(0,h / 2 - 10) t.goto(0,-h / 2 + 10) t.goto(0,0) t.goto(w / 2 - 10,0) t.goto(-w / 2 + 10,0) t.goto(0,0) t.pensize(2) return 15,15,xfac,yfac else: bx = abs(min(min([i[0] for i in p]), 0)) by = abs(min(min([i[1] for i in p]), 0)) bx = int(bx) if(int(bx) == bx) else int(bx) + 1 by = int(by) if(int(by) == by) else int(by) + 1 xfac = w / xsize yfac = h / ysize bx,by = bx * xfac,by * yfac t.penup() t.goto(-w / 2 + 10,-h / 2 + 10) t.pendown() t.pensize(5) t.goto(w / 2 + 10,-h / 2 + 10) t.goto(-w / 2 + 10, -h / 2 + 10) t.goto(-w / 2 + 10,h / 2 + 10) t.penup() t.goto(0,0) t.pendown() t.pensize(2) return -w / 2 + 15 + bx, -h / 2 + 15 + by,xfac,yfac def _get_gradient(l,r,n,h): r1,g1,b1 = l r2,g2,b2 = r f1 = float(n) f2 = float(h - n) r3 = int(r1 * f1 + r2 * f2) g3 = int(g1 * f1 + g2 * f2) b3 = int(b1 * f1 + b2 * f2) return (r3,b3,g3) def draw_curve_matplotlib_style(points,xlabel = '',ylabel = '',linecolor=(0,0,190)): xf,yf,xfac,yfac = _draw_axis(1,xlabel,ylabel,points) t.pencolor(linecolor) t.penup() t.goto(points[0][0] + xf,points[0][1] + yf) t.pendown() for i in points: x, y = i x, y = x * xfac, y * yfac t.goto(x + xf,y + yf) t.mainloop() def draw_scatter_matplotlib_style(points,xlabel = '',ylabel = '',pointcolor=(0,0,190),pointr=1): xf,yf,xfac,yfac = _draw_axis(1, xlabel, ylabel,points) t.pencolor(pointcolor) t.fillcolor(pointcolor) for i in points: x,y = i x,y = x * xfac,y * yfac t.penup() t.goto(x + xf,y + yf) t.pendown() t.begin_fill() t.circle(pointr) t.end_fill() t.mainloop() def draw_contour_matplotlib_style(points,xlabel = '',ylabel = '',low=(0,50,50),high=(0,255,255)): xf,yf,xfac,yfac = _draw_axis(1,xlabel,ylabel,points) plot = {} highest = float('-inf') for i in points: x, y, z = i highest = max(highest, y) if (y in plot): plot[y].append((x, z)) else: plot[y] = [(x, z)] for i in plot: t.penup() t.goto(plot[i][0][0] + xf,plot[i][0][1] + yf) t.pendown() t.fillcolor(_get_gradient(low, high, i, highest)) t.begin_fill() for j in plot[i]: x, y = j x, y = x * xfac, y * yfac t.goto(x + xf, y + yf) t.end_fill() t.mainloop() def draw_bar_chart_matplotlib_style(points,xlabel = '',ylabel = '',barcolor=(0,0,190),barwidth=5): xf,yf,xfac,yfac = _draw_axis(1,xlabel,ylabel,points) half_width = barwidth / 2 t.pencolor(barcolor) t.fillcolor(barcolor) for i in points: x,y = i x, y = x * xfac, y * yfac t.penup() t.goto(x - half_width + xf,yf) t.pendown() t.begin_fill() t.goto(x + half_width + xf,yf) t.goto(x + half_width + xf,y + yf) t.goto(x - half_width + xf, y + yf) t.goto(x - half_width + xf, yf) t.end_fill() t.mainloop() def draw_curve_plotter_style(points,xlabel = '',ylabel = '',linecolor=(0,0,190)): xf,yf,xfac,yfac = _draw_axis(2,xlabel,ylabel,points) t.pencolor(linecolor) t.penup() t.goto(points[0][0] + xf, points[0][1] + yf) t.pendown() t.begin_fill() for i in points: x, y = i x, y = x * xfac, y * yfac t.goto(x + xf, y + yf) t.end_fill() t.mainloop() def draw_scatter_plotter_style(points,xlabel = '',ylabel = '',pointcolor=(0,0,190),pointr=1): xf,yf,xfac,yfac = _draw_axis(2,xlabel,ylabel,points) t.pencolor(pointcolor) t.fillcolor(pointcolor) for i in points: x, y = i x, y = x * xfac, y * yfac t.penup() t.goto(x + xf, y + yf) t.pendown() t.begin_fill() t.circle(pointr) t.end_fill() t.mainloop() def draw_contour_plotter_style(points,xlabel = '',ylabel = '',low=(0,50,50),high=(0,255,255)): xf,yf,xfac,yfac = _draw_axis(2,xlabel,ylabel,points) plot = {} highest = float('-inf') for i in points: x,y,z = i highest = max(highest,y) if(y in plot): plot[y].append((x,z)) else: plot[y] = [(x,z)] for i in plot: t.fillcolor(_get_gradient(low,high,i,highest)) t.penup() t.goto(plot[i][0][0] + xf, plot[i][0][1] + yf) t.pendown() t.begin_fill() for j in plot[i]: x, y = j x, y = x * xfac, y * yfac t.goto(x + xf, y + yf) t.end_fill() t.mainloop() def draw_bar_chart_plotter_style(points,xlabel = '',ylabel = '',barcolor=(0,0,190),barwidth=5): xf,yf,xfac,yfac = _draw_axis(2,xlabel,ylabel,points) half_width = barwidth / 2 t.pencolor(barcolor) t.fillcolor(barcolor) for i in points: x, y = i x, y = x * xfac, y * yfac t.penup() t.goto(x - half_width + xf, yf) t.pendown() t.begin_fill() t.goto(x + half_width + xf, yf) t.goto(x + half_width + xf, y + yf) t.goto(x - half_width + xf, y + yf) t.goto(x - half_width + xf, yf) t.end_fill() t.mainloop()
这次代码比较冗长,就讲一下思路和重点代码哈,剩下的自己应该能理解的了。
曲线图:每一次使用goto
函数去到一个需要画的点即可
散点图:每一次提笔并goto
到需要画的点然后落笔画一个圆
等高线:按照高度画曲线图,并以用如下 ↓ \downarrow ↓代码产生的颜色填充
def _get_gradient(l,r,n,h):
r1,g1,b1 = l
r2,g2,b2 = r
f1 = float(n)
f2 = float(h - n)
r3 = int(r1 * f1 + r2 * f2)
g3 = int(g1 * f1 + g2 * f2)
b3 = int(b1 * f1 + b2 * f2)
return (r3,b3,g3)
即将 c 1 c_1 c1和 c 2 c_2 c2按比例混合。
柱状图: goto
到要画的点后画一个长方形并填充即可
def _draw_axis(op,xlab,ylab,p): t.hideturtle() xsize = max([i[0] for i in p]) + abs(min([i[0] for i in p])) ysize = max([i[1] for i in p]) + abs(min([i[1] for i in p])) canv = t.getcanvas() w,h = canv.winfo_width() - 30,canv.winfo_height() - 30 if(op == 2): xfac = w / 2 / xsize yfac = h / 2 / ysize t.penup() t.goto(0,0) t.pendown() t.pensize(5) t.goto(0,h / 2 - 10) t.goto(0,-h / 2 + 10) t.goto(0,0) t.goto(w / 2 - 10,0) t.goto(-w / 2 + 10,0) t.goto(0,0) t.pensize(2) return 15,15,xfac,yfac else: bx = abs(min(min([i[0] for i in p]), 0)) by = abs(min(min([i[1] for i in p]), 0)) bx = int(bx) if(int(bx) == bx) else int(bx) + 1 by = int(by) if(int(by) == by) else int(by) + 1 xfac = w / xsize yfac = h / ysize bx,by = bx * xfac,by * yfac t.penup() t.goto(-w / 2 + 10,-h / 2 + 10) t.pendown() t.pensize(5) t.goto(w / 2 + 10,-h / 2 + 10) t.goto(-w / 2 + 10, -h / 2 + 10) t.goto(-w / 2 + 10,h / 2 + 10) t.penup() t.goto(0,0) t.pendown() t.pensize(2) return -w / 2 + 15 + bx, -h / 2 + 15 + by,xfac,yfac
就是按照要求画两条线,并计算xf
,yf
,xfac
,yfac
分别代表曲线的位置(xf
,yf
)和曲线在两个方向上的缩放倍数(xfac
,yfac
)。
计算方法如下
↓
\downarrow
↓
o
p
=
1
op=1
op=1时:
xf
=
=
= yf
=
15
=15
=15,也就是 坐标系位置
+
+
+ 线宽。
xfac
=
=
= 画布宽度
÷
\div
÷
x
x
x轴长度
yfac
=
=
= 画布高度
÷
\div
÷
y
y
y轴长度
o
p
=
2
op=2
op=2时:
xf
=
=
= 坐标系
x
x
x轴位置
+
+
+ 线宽
+
+
+
x
x
x轴最低位置与0中的最小值的绝对值向上取整(这里这么长的的东西是为了让曲线在图里面)
yf
=
=
= 坐标系
y
y
y轴位置
+
+
+ 线宽
+
+
+
y
y
y轴最低位置与0中的最小值的绝对值向上取整(这里这么长的的东西是为了让曲线在图里面)
xfac
=
=
= 画布宽度
÷
\div
÷
x
x
x轴长度
yfac
=
=
= 画布高度
÷
\div
÷
y
y
y轴长度
格式为draw_..._matplotlib_style
的为matplotlib
风格的绘图函数
格式为draw_..._plotter_style
的为plotter
原生风格的绘图函数
另...
可以为curve
(曲线图)、scatter
(散点图)、contour
(等高线)、bar_chart
参数请自行查看源代码,每个函数中的points
参数格式是
[
(
x
1
,
y
1
,
z
1
)
,
(
x
2
,
y
2
,
z
2
)
,
⋯
]
[(x_1,y_1,z_1),(x_2,y_2,z_2),\cdots]
[(x1,y1,z1),(x2,y2,z2),⋯](等高线contour
函数)或
[
(
x
1
,
y
1
)
,
(
x
2
,
y
2
)
,
⋯
]
[(x_1,y_1),(x_2,y_2),\cdots]
[(x1,y1),(x2,y2),⋯](其他函数)
hit-road
拜拜,下课!
回到顶部
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。