当前位置:   article > 正文

python用turtle绘图库做科学绘图工具_turtle 画科技图

turtle 画科技图

前言

plotter绘制matplotlib风格图
plotter绘制plotter风格图
plotter绘制matplotlib风格图

这个项目还是有一点小瑕疵的,那就是总是超出那么一点来和没有加入圆滑处理

开始

献上代码

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189

这次代码比较冗长,就讲一下思路和重点代码哈,剩下的自己应该能理解的了。

画图部分

曲线图:每一次使用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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

即将 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

就是按照要求画两条线,并计算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轴长度

项目github

github传送门

附录A:如何使用:

格式为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),](其他函数)

附录B:更方便的matplotlib如何使用:

英文官网
中文官网


作者

hit-road

拜拜,下课!
回到顶部

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/230154
推荐阅读
相关标签
  

闽ICP备14008679号