Polar plots with Tkinter

A point in space can be represented using the Cartesian coordinate using two numbers x and y.  The same point can also be represented in the polar coordinate by using the distance from the origin (r) and the angle from the x axis (theta), as shown in the following diagram:

To convert between polar and Cartesian coordinates, we use the following equalities:

x= r cos(θ) and y = rsin(θ)

It is easier to plot equations expressed in terms of r and θ on a special kind of graph called the polar plot, which is divided into small concentric circles and radial lines emanating from the center. The radial lines are normally spaced at intervals of 15◦, while the radius of concentric circles depends on the scale on which the distance is to be measured from the center.  Here's an example of a polar plot that we will draw:

The Tkinter canvas understands Cartesian coordinates. It is, however, easy to convert from polar to Cartesian coordinates. We  accordingly define a method named polar_to_cartesian; see 8.06_polar_plot.py:

def polar_to_cartesian(r, theta, scaling_factor, x_center, y_center):
x = r * math.cos(theta) * scaling_factor + x_center
y = r * math.sin(theta) * scaling_factor + y_center
return(x, y)

Here's a brief description of the preceding code:

  • The method converts an input of  (r, theta) value to (x, y) coordinates using the equalities x= r cos(θ) and y = rsin(θ).
  • The scaling_factor in the preceding equation decides how many pixels will equal to one unit in our polar plot and is set to a constant value. Changing it changes the size of the plot.
  • We add the x_center and y_center values to the final results. x_center is defined as half the window_width while y_center is half the window size. We add these as offsets because Canvas considers (0,0) as the top left of the canvas, while we want to consider the center of the canvas as (0,0).

We begin by creating a canvas in a Tkinter root window, and add radial lines and concentric circles to the canvas using the following code:

# draw radial lines at interval of 15 degrees
for theta in range(0,360,15):
r = 180
x, y = x_center + math.cos(math.radians(theta))*r,
y_center - math.sin(math.radians(theta)) *r
c.create_line(x_center, y_center, x, y, fill='green', dash=(2, 4),
activedash=(6, 5, 2, 4) )
c.create_text(x, y, anchor=W, font="Purisa 8", text=str(theta) + '°')

# draw concentric_circles
for radius in range(1,4):
x_max = x_center + radius * scaling_factor
x_min = x_center - radius * scaling_factor
y_max = y_center + radius * scaling_factor
y_min = y_center - radius * scaling_factor
c.create_oval(x_max, y_max, x_min, y_min, width=1, outline='grey',
dash=(2, 4), activedash=(6, 5, 2, 4))

Now that our graph paper is ready, it's time to plot the actual polar plot. The following code plots 3000 points of the polar equation r = 2*math.sin(2*theta) on the graph:

for theta in range(0, 3000):
r = 2*math.sin(2*theta)
x, y = polar_to_cartesian(r, theta, scaling_factor, x_center, y_center)
c.create_oval(x, y, x, y, width=1, outline='navy')

This creates the curve of the form. r = a sin nθ, where n is even. It is a  2n-leaved rose. If n is odd, it will form an n-leaved rose. There are many other good looking plots that you can plot by changing the r equation in the previous method. A few other equations that you can try are as follows:

r = 0.0006 * theta   # an archimedean spiral
r = 1 + 2*math.cos(theta) # cardoid pattern
r = 3 * math.cos(theta) # circle
r = 2*math.sin(5*theta) # 5 leaved rose
r = 3 * math.cos(3*theta) # 3 leaved rose
r = 2 * math.sin(theta)**2 # a lemniscate
r = (4 * math.cos(2*theta))**1/2 # another lemniscate

You can also play with the parameters of the individual equation to see the difference they make to the plot.

This concludes the iteration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.144.248