0

this question may have been asked before however it doesn't work for me. Is there any way to plot a line graph using python turtle?

I have no idea where to start except for

from turtle import Turtle

Thank you for any help.

3 Answers 3

2

"Highly likely", it can be done even simpler (for example, without x2 and y2 variables, but I am not sure)

If our goal is to plot a simple line function using turtle, we can go simpler:

from turtle import Turtle, Screen
from math import pi, sin

def draw_wave(frequency=1):
    angle = 0

    while angle < 2 * pi:
        turtle.goto(angle, sin(angle * frequency))
        angle += 0.05

screen = Screen()
screen.setworldcoordinates(0, -1.25, 2 * pi, 1.25)

turtle = Turtle()

draw_wave(2)

turtle.hideturtle()
screen.exitonclick()

And then embelish (axes, etc.) as you require.

Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure I understand what do you mean as the "line graph", https://en.wikipedia.org/wiki/Line_graph or https://en.wikipedia.org/wiki/Line_chart? For the second case it may be done, for example, for sinus function, with the following simplified code.

import turtle as tr
import math as m

x0, y0 = -300, 275  # The point near the upper left corner of the Turtle screen - virtual origin of coordinates
Y0 = -y0 + 100  # The reference vertical coordinate for the second function
A0 = 100  # The amplitude of sinus function
f0 = 80   # 1/frequency (reverse frequency)

def draw1():
   x1 = 0
   y1 = A0 - A0 * m.sin(x1/f0)
   tr.goto(x0 + x1, y0 - y1)
   tr.down()
   tr.dot(size = 1)
   for x2 in range(abs(x0)*2):
      y2 = A0 - A0 * m.sin(x1/f0)
      tr.goto(x0 + x2, y0 - y2)
      tr.dot(size = 1)
      x1, y1 = x2, y2

def draw2(f0):
   x1 = 0
   y1 = Y0 + A0 * m.sin(x1/f0)
   tr.goto(x0 + x1, y1)
   tr.down()
   tr.dot(size = 1)
   for x2 in range(abs(x0)*2):
      y2 = Y0 + A0 * m.sin(x1/f0)
      tr.goto(x0 + x2, y2)
      tr.dot(size = 1)
      x1, y1 = x2, y2

tr.speed('fastest')
tr.up()
tr.goto(x0, y0)
tr.hideturtle()
tr.color('red')
draw1()  # The pivot point - the virtual origin of coordinates (x0 and y0)

tr.up()
tr.goto(x0,y0)
tr.color('blue')
draw2(f0/2)  # The pivot point - x0 and Y0
input()      # waiting for the <Enter> press in the console window

"Highly likely", it can be done even simpler (for example, without x2 and y2 variables, but I am not sure) - I took this method from drawing on the Tkinter canvas. And this method, probably, is suitable for the first case (with some kind of a modification, of course).

Comments

0

I am thinking that you want to graph linear equation: mx + b

import turtle as t
a = t.window_width() / 2
def graph(m_x, m_y, b):
    x = [i for i in range(int(-a), int(a), m_x)]
    return list(zip(x, map(lambda x_val: ((m_x / m_y) * x_val) + b, x)))
for x, y in graph(1, 10, 0):
    t.goto(x, y)
t.mainloop()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.