Advertisement
here2share

# Tk_math_waves.py

May 27th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. # Tk_math_waves.py
  2.  
  3. from Tkinter import *
  4. import math
  5. root = Tk()
  6. root.title("Simple plot using canvas and line")
  7. width = 400
  8. height = 300
  9. center = height//2
  10. x_increment = 1
  11. # width stretch
  12. x_factor = 0.04
  13. # height stretch
  14. y_amplitude = 80
  15. c = Canvas(width=width, height=height, bg='white')
  16. c.pack()
  17. str1 = "sin(x)=blue  cos(x)=red"
  18. c.create_text(10, 20, anchor=SW, text=str1)
  19. center_line = c.create_line(0, center, width, center, fill='green')
  20. # create the coordinate list for the sin() curve, have to be integers
  21. xy1 = []
  22. for x in range(400):
  23.     # x coordinates
  24.     xy1.append(x * x_increment)
  25.     # y coordinates
  26.     xy1.append(int(math.sin(x * x_factor) * y_amplitude) + center)
  27. sin_line = c.create_line(xy1, fill='blue')
  28. # create the coordinate list for the cos() curve
  29. xy2 = []
  30. for x in range(400):
  31.     # x coordinates
  32.     xy2.append(x * x_increment)
  33.     # y coordinates
  34.     xy2.append(int(math.cos(x * x_factor) * y_amplitude) + center)
  35. cos_line = c.create_line(xy2, fill='red')
  36. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement