如何用 Python Turtle 绘制折线图?

这个问题之前可能已经被问过,但它对我不起作用。有没有办法使用 python turtle 绘制折线图?

我不知道从哪里开始除了

from turtle import Turtle

感谢您的任何帮助。


慕无忌1623718
浏览 176回答 3
3回答

梵蒂冈之花

“很有可能”,它可以做得更简单(例如,没有 x2 和 y2 变量,但我不确定)如果我们的目标是使用 turtle 绘制一个简单的线函数,我们可以更简单:from turtle import Turtle, Screenfrom math import pi, sindef draw_wave(frequency=1):&nbsp; &nbsp; angle = 0&nbsp; &nbsp; while angle < 2 * pi:&nbsp; &nbsp; &nbsp; &nbsp; turtle.goto(angle, sin(angle * frequency))&nbsp; &nbsp; &nbsp; &nbsp; angle += 0.05screen = Screen()screen.setworldcoordinates(0, -1.25, 2 * pi, 1.25)turtle = Turtle()draw_wave(2)turtle.hideturtle()screen.exitonclick()然后根据需要修饰(斧头等)。

拉风的咖菲猫

我不确定我是否理解“折线图”、https://en.wikipedia.org/wiki/Line_graph或https://en.wikipedia.org/wiki/Line_chart是什么意思?对于第二种情况,例如,对于 sinus 函数,可以使用以下简化代码来完成。import turtle as trimport math as mx0, y0 = -300, 275&nbsp; # The point near the upper left corner of the Turtle screen - virtual origin of coordinatesY0 = -y0 + 100&nbsp; # The reference vertical coordinate for the second functionA0 = 100&nbsp; # The amplitude of sinus functionf0 = 80&nbsp; &nbsp;# 1/frequency (reverse frequency)def draw1():&nbsp; &nbsp;x1 = 0&nbsp; &nbsp;y1 = A0 - A0 * m.sin(x1/f0)&nbsp; &nbsp;tr.goto(x0 + x1, y0 - y1)&nbsp; &nbsp;tr.down()&nbsp; &nbsp;tr.dot(size = 1)&nbsp; &nbsp;for x2 in range(abs(x0)*2):&nbsp; &nbsp; &nbsp; y2 = A0 - A0 * m.sin(x1/f0)&nbsp; &nbsp; &nbsp; tr.goto(x0 + x2, y0 - y2)&nbsp; &nbsp; &nbsp; tr.dot(size = 1)&nbsp; &nbsp; &nbsp; x1, y1 = x2, y2def draw2(f0):&nbsp; &nbsp;x1 = 0&nbsp; &nbsp;y1 = Y0 + A0 * m.sin(x1/f0)&nbsp; &nbsp;tr.goto(x0 + x1, y1)&nbsp; &nbsp;tr.down()&nbsp; &nbsp;tr.dot(size = 1)&nbsp; &nbsp;for x2 in range(abs(x0)*2):&nbsp; &nbsp; &nbsp; y2 = Y0 + A0 * m.sin(x1/f0)&nbsp; &nbsp; &nbsp; tr.goto(x0 + x2, y2)&nbsp; &nbsp; &nbsp; tr.dot(size = 1)&nbsp; &nbsp; &nbsp; x1, y1 = x2, y2tr.speed('fastest')tr.up()tr.goto(x0, y0)tr.hideturtle()tr.color('red')draw1()&nbsp; # The pivot point - the virtual origin of coordinates (x0 and y0)tr.up()tr.goto(x0,y0)tr.color('blue')draw2(f0/2)&nbsp; # The pivot point - x0 and Y0input()&nbsp; &nbsp; &nbsp; # waiting for the <Enter> press in the console window“很有可能”,它可以做得更简单(例如,没有x2和y2变量,但我不确定) - 我从 Tkinter 画布上绘制这个方法。而且这种方法可能适用于第一种情况(当然,需要进行某种修改)。

翻阅古今

我在想你想绘制线性方程:mx + bimport turtle as ta = t.window_width() / 2def graph(m_x, m_y, b):&nbsp; &nbsp; x = [i for i in range(int(-a), int(a), m_x)]&nbsp; &nbsp; return list(zip(x, map(lambda x_val: ((m_x / m_y) * x_val) + b, x)))for x, y in graph(1, 10, 0):&nbsp; &nbsp; t.goto(x, y)t.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python