Polar Plots in Python

Chris Webb
Aug 5 · 6 min read
Image for post
Image for post

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.

Polar Plots

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:

Image for post
Image for post

For example if you plot

Image for post
Image for post

you will get something like this.

Image for post
Image for post
Graph drawn on wolframalpha.com

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

where 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.

The Project

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 math. The 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.

The 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:

  • The height and width are the radius x 2, plus a bit of a border
  • turtle.title sets the window title
  • turtle.screensize sets the size of the drawing surface
  • turtle.setup set the size of the window
  • turtle.shape sets the shape of the turtle itself, which by default is a boring arrow
  • degree_label_radius is the distance of the label from the origin, and needs to be a bit further out than the end of the line
  • turtle.pencolor takes 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 radian equivalent of degrees for use with math.cos and math.sin
  • 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:

python3.8 polarplots.py

You can now sit back and watch the turtle scurrying across your screen drawing these axes:

Image for post
Image for post

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 x and 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.

The circle_function is the first and simplest of the polar plot functions. All it needs to do is calculate the x and 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.

Image for post
Image for post

You might like to try different values for the radius and angle arguments.

Now for the last three functions.

All the functions take radians and radius arguments, and return a dictionary with the corresponding x and 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.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

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…

Explorations in Python

Explorations and experiments in Python

Chris Webb

Written by

I am a software engineer based in London and I write articles on JavaScript, Python, C and other programming topics.

Explorations in Python

Practical and useful projects and tutorials in the Python programming language

Chris Webb

Written by

I am a software engineer based in London and I write articles on JavaScript, Python, C and other programming topics.

Explorations in Python

Practical and useful projects and tutorials in the Python programming language

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store