I create program on python and tkinter that can build plot. But I can`t build more than one plot at one time. Please help me. Explain how i can implement it.
from tkinter import *
import math
wind = Tk()
wind.geometry("400x400")
canw = Canvas(wind, width=400, height=400)
canw.pack()
canw.place(x=-1, y=-1)
lines_index = 0
numbers = -8
#create cages(like in notebook) [one cage = 10x10 px]
while lines_index <= 40:
if lines_index == 20:
canw.create_line(0, 0+10*lines_index, 400, 0+10*lines_index, fill="#000", width=2) #x_line
canw.create_line(10*lines_index, 0, 10*lines_index, 400, fill="#000", width=2) #y_line
else:
canw.create_line(0, 0+10*lines_index, 400, 0+10*lines_index, fill="#4C8098") #x_line
canw.create_line(10*lines_index, 0, 10*lines_index, 400, fill="#4C8098") #y_line
lines_index = lines_index + 1
numbers = numbers + 1
print(lines_index, numbers)
# b = [-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,
# 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] #test properties
# fun_index = 1 #don`t use now
b = [] #array for properties
i = -20
while i <= 20:
b.append(i)
i= i + 0.01 #change accuracy
print(i)
for x in b: #loop create points for plot
y = x**2
# y = (6*(x**2))+(2*x)+8
# y = (6*(x**2))+8
# y= 3*(x+3)
# y = 3**x
# y=x**3
# y = x**(1/2) #not work
# y = 1/x
canw.create_oval((x*10)+200, -(y*10)+200, (x*10)+200, -(y*10)+200, outline='red', fill="red")
print(y)
wind.mainloop()
I try to use if else in loop(where creates points for plot) but it not a good idea. Becase all points divided into all amount of plots.
Firstly, note that the create_oval
method draws ellipses (or circles in special cases where you satisfy the proper radius condition) and it has inputs like x0, y0, x1, y1
where:
"The ellipse is fit into a rectangle defined by the coordinates (x0, y0)
of the top left corner and the coordinates (x1, y1)
of a point just outside of the bottom right corner.".
Source: https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/create_oval.html
Secondly, your coordinate system is a bit off. In the plot popping up, your origin (0,0)
point is actually at the very top left corner of the plot. You can verify this by doing something like canw.create_oval(50, 50, 20, 20, outline='red', fill="red")
and see that the circle is actually near to the top left corner of the plot window instead of being near to the origin you seem to define with your two main black lines. Thus, in the for x in b:
loop of the code you've shared, your endeavors to plot multiple ellipses on the same canvas are in-vain because these ellipses go well beyond the visible area.
The solution is to shift the origin of the coordinate system to fit exactly into the desired (0,0)
point, i.e., where the two black lines intersect. You can achieve this by including offsets of value 200
(since your grids are 10x10):
canw.create_oval(x1+200, -y1+200, x2+200, -y2+200, outline='red', fill="red")
However, there is also an important point to which you need to pay attention here. The (x0,y0)
, and (x1,y1)
are not top left and bottom right (respectively) corners of the rectangle that the ellipse is fit into, but rather after the origin shift, (x0,y0)
becomes the bottom left, and (x1,y1)
becomes the top right corner of the rectangle.
Having these in mind, you can plot whatever you want inside the for
loop. Since I did not understand what you are (mathematically) trying to plot, I cannot give you a test case here, but if you can provide me with any special case, I can help further by updating my answer.