Drawing polar plots using the Turtle Graphics library which comes as part of the Python Standard Library.
Turtle Graphics in Python
If you look down the list of Python standard libraries you might spot one with the mysterious name of turtle. This is Python’s own Tkinter-based implementation of Turtle Graphics, the concept of which dates right back to 1966 and the Logo programming language.
The idea is simple — you control a turtle holding a pen, using various functions to make him run around the screen, drawing as he goes. You can change the pen colour, tell the turtle to lift up the pen to go to another part of the screen without drawing a line, draw text in a font of your choice and so on. You can also capture keyboard and mouse events so can write interactive applications.
You can find the full documentation at https://docs.python.org/3/library/turtle.html although I will only be using a small subset of the library’s functionality here.
You may be familiar with plotting functions on a Cartesian Plane, where for every point on the horizontal x-axis you can calculate a corresponding coordinate on the y-axis using a function of the form:
For example if you plot
you will get something like this.
The above was drawn by iterating
x from -10 to 10, and for each value of
x calculating the corresponding value of
y. Each point
x,y is then plotted on the graph.
A polar plot works in a very different way, and plots functions in the form
r is the radius, or distance from the origin at 0,0, and
θ is the angle. (θ is the Greek letter theta, generally used to denote an angle.)
To draw a polar plot we therefore need to iterate between two angles (for example 0° to 360°) and calculate the corresponding value of
r for each angle. We then plot the point at the current angle from the origin 0,0, at the current distance
r from that origin.
This project consists of a single Python file called polarplots.py which you can clone/download the Github repository.
This is the first part of the source code.
Firstly we need to import
turtle, and also
main function first calls
draw_axes which I’ll get to in a moment, and then we have a few function calls which are commented out for the time being. At the end of main we call
turtle.hideturtle, and then
turtle.exitonclick to prevent the window closing.
draw_axes function draws a set of lines out from the origin at 15° intervals from 0° to 360°, and also prints the angle at the end of each. In Turtle Graphics the centre of the screen is 0,0 which makes this process a lot simpler.
Before looping from 0° to 360° we need to get a few things set up:
radiusx 2, plus a bit of a border
turtle.titlesets the window title
turtle.screensizesets the size of the drawing surface
turtle.setupset the size of the window
turtle.shapesets the shape of the turtle itself, which by default is a boring arrow
degree_label_radiusis the distance of the label from the origin, and needs to be a bit further out than the end of the line
turtle.pencolortakes a tuple of values between 0.0 and 1.0 NOT rgb values of 0 to 255
Lastly comes the loop, iterating from 0° to 360° at 15° intervals. It doesn’t actually get to 360° because that would clash with 0°.
Within the loop we need to do the following:
- Calculate the
degreesfor use with
- Lift up the pen and go “home”, ie. to 0,0, the centre of the drawing surface
- Put the pen down and go out to the end of the line, drawing as we go
- Lift up the pen and go out to the location where the angle is printed
- Move up from the current position by the size (height) of the font
- Put the pen down and write the current angle including the ° symbol
We now have enough code to run:
You can now sit back and watch the turtle scurrying across your screen drawing these axes:
You can speed things up or slow them down by uncommenting
turtle.delay and entering a value different from the default 10ms.
Let’s move on to drawing the plot itself.
Most of the work that goes into drawing a polar plot is the same whatever the function calculating the radii. I have therefore written a general-purpose plot function which takes a function as one of its parameters.
After a bit of housekeeping setting the title and pen attributes we iterate between the two angles. After calculating the radian equivalent of the angle we call the function supplied as an argument. This must return a dictionary with the
y coordinates for the given angle and radius, which we then use in a call to
turtle.goto. Lastly we call turtle.pendown: this function draws the plot by moving from one position to the next on each iteration.
circle_function is the first and simplest of the polar plot functions. All it needs to do is calculate the
y coordinates for the given angle and radius combination and return them as a dictionary.
Uncomment the first call of the
plot function in
main and run the program again.
You might like to try different values for the radius and angle arguments.
Now for the last three functions.
All the functions take
radius arguments, and return a dictionary with the corresponding
y coordinates. You might like to have a go at writing some more — as long as the arguments and return values match you can do anything in the functions themselves.
Uncomment the second, third and fourth calls to
plot one at a time and run the program with each in turn. These are the plots.
Again, you can vary the parameters for each function. And of course you can have a go at writing your own functions from scratch. Over to you…