0

line chart

Please help me to draw a simple line chart in python turtle module based on user input. I am able to draw the x and y axis. The x-axis tick is equal to the number of data points. For example, if the user input is 5 then it will be 5 ticks labelled 1 to 5 and so on. However, the tick for y-axis has 6 ticks(fixed) labelled from 0 to 20, increment in 4(0,4,8..20).

I can't figure out the logic for plotting y values based on user input. For example, if the user types 15, it needs to go to the corresponding data point in y axis and put a dot in there. Similarly, if the user types 5 it needs to go the corresponding value in y axis as well plus connecting to the preceding data point(in this case connecting 15 from previous point to 5 in the current point) and so on based on user input. Also, my x and y labeling could be done it in a better way. Here is my code. Any help is much appreciated. Please look at the image description, I am aiming for similar results.

import turtle as t
import time
wn = t.Screen()  # create a window for the turtle to draw on
wn.title("Turtle Demo")  # the title to show at the top of the window
WINDOW_WIDTH = 500  # size constants for easy changing
WINDOW_HEIGHT = 500
wn.setup(WINDOW_WIDTH, WINDOW_HEIGHT, 200, 10)  # specify window size (width, height)
user_input = t.numinput(title='Line graph', prompt='How many data points:')
x_max = user_input
# print(userInput)
# graph window
x_min = 0
y_min = 0
y_max = 5
# tick info
t_l = 0.1
x_t_space = 1
y_t_space = 1
wn.setworldcoordinates(x_min, y_min, x_max, y_max)
# Draw x-axis
t.tracer(10)
t.hideturtle()
t.up()
t.goto(x_min, 0)
t.down()
t.goto(user_input, 0.0)
t.up()
# Draw the y-axis
t.goto(0.0, y_min)
t.down()
t.goto(0.0, y_max)
t.up()
# Draw the x tick marks
n_x_ticks = int((x_max - x_min) / x_t_space) + 1
for tick in range(n_x_ticks):
    loc = x_min + tick * x_t_space
    t.up()
    t.goto(loc, -t_l * 0.4)
    t.down()
    t.goto(loc, t_l * 0.4)
    t.up()
    t.write(tick)

# Draw the y tick marks
y_label = range(0, 21, 4)

n_y_ticks = int((y_max - y_min) / y_t_space) + 1
for tick in range(n_y_ticks):
    loc = y_min + tick * y_t_space
    t.up()
    t.goto(-t_l * 0.4, loc)
    t.down()
    t.goto(t_l * 0.4, loc)
    for i in y_label:
        tick = i
        print(tick)
        t.write(tick, align="left", font=("Arial", 8, "normal"))
# get user input and plot the y value as long as user needed.
# the below code marks the value in the x-axis itself, that is not what I want.
#I want the value to get plotted outside the axis as in the normal line chart
t.backward
t.goto(0, 0)
t.tiltangle(45)
user_value = t.numinput(title='Line graph', prompt='Enter the first value:')
t.shape("square")
t.stamp()
t.forward(user_value)
user_value = t.numinput(title='Line graph', prompt='Enter the next value:')
t.shape("square")
t.stamp()
t.forward(user_value)
user_value = t.numinput(title='Line graph', prompt='Enter the next value:')
t.shape("square")
t.stamp()
t.forward(user_value)

        
   

Line chart

justin
  • 1
  • 2
  • Where do you get the y values? – goku May 02 '22 at 01:50
  • Hi goku thanks for the reply.y values comes from user input.for example, if the user enters 7, it needs to go the corresponding y axis(y axis labelled as 0 to 20, increment of 4) and put a dot there and so on.Thanks. – justin May 02 '22 at 01:55
  • Yes, I understand that, but in this code snippet, I don't see any user input for y values. You can always use an array to get the corresponding values, for example, if x = 5, store every input in an array and iterate over the array in order to plot the values – goku May 02 '22 at 01:58
  • Hi @goku thanks for your reply again.Sorry for the confusion.i have updated my code snippet.I know it is still not quite right, but you will get an idea.Thanks. – justin May 02 '22 at 03:22

1 Answers1

0

The input size is defined by the user and you want the same size of y-values.

Do the following:

for i in range (0, int(user_input)):
    user_value = t.numinput(title='Line graph', prompt='Enter the value:')
    t.shape("square")
    t.stamp()
    t.forward(user_value)

EDIT: Just use matplotlib, it is very simple

import numpy as np
import matplotlib.pyplot as plt

user = int(input("Enter number of data: "))
x = np.arange(user)
y = []
for i in range (0, user):
    value = int(input("Enter value: "))
    y.append(value)

y = np.array(y)
plt.scatter(x, y)
plt.plot(x, y)
plt.show()

Input:

Enter number of data: 9
Enter value: 10
Enter value: 30
Enter value: 20
Enter value: 50
Enter value: 60
Enter value: 35
Enter value: 38
Enter value: 12
Enter value: 31

Output:

enter image description here

which is the same as the chart you provided.

goku
  • 167
  • 10
  • Thanks @goku for your reply again.I tried your code and after entering the value, the square is placed in the y axis it self which is not i am aiming for.Please take a look at the above chart.For example the first value is placed in 10, the second value in 30, the third value is in 20 and so on based on user input.Also preceding data point is connected with a line.Here 10 is connected to 30, 30 is connected with 20 and so on. I am aiming for results similar to the above sample graph. from one point to next point based on input.Is there a way to do it.Your help is much appreciated.Thanks. – justin May 02 '22 at 06:14
  • I am not sure this is going to be of any help.my tick variable holds value 0-20. i want the square to be placed out of axis not in the y line itself.That is what currently happens. I assume turtle.forward may not be to use or it may be something to do with my turtle.backward and tiltangle command.Not sure how to get it right. – justin May 02 '22 at 06:27
  • I am not very familiar with the turtle module. Why don't you use matplot? It would be way easier – goku May 02 '22 at 08:10
  • Hi Goku.Thanks for your input.I have seen many examples of line chart using matploit.Unfortunately for this one, the requirement is to use turtle module only.Thanks. – justin May 02 '22 at 08:39