[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Draw a butterfly curve in Python

[A butterfly curve drawn in Python]

The butterfly curve was discovered in 1989 by mathematician Temple H. Fay in 1989. The program uses the following equation to draw the curve.

[These equations generate a butterfly curve]

It's a complicated curve drawn from complicated equations, but the code is pleasantly straightforward.

Drawing the Curve

When the program starts, it initializes tkinter, creates a Canvas widget, and then calls the following method to draw the curve.

def draw_curve(self): '''Draw the butterfly curve.''' # Generate the points. period = 24 num_points = 5000 dt = period * math.pi / num_points points = [get_point(i * dt) for i in range(num_points)] # Transform to fit the canvas. self.canvas.update() wid = self.canvas.winfo_width() hgt = self.canvas.winfo_height() margin = 5 target_rect = (margin, margin, wid - margin, hgt - margin) points = transform_points(points, target_rect) # Draw. self.canvas.create_line(points, fill='red')

This code first defines period, the number of multiples of π over which it should vary the variable t in the equations. In this example, the values range over 0 ≤ t < 24 π.

The code then sets num_points to the number of points to draw. It uses period and num_points to calculate dt, the amount by which t should change between points.

Next, the method uses list comprehension to call the get_point function (described shortly) for all of the values of t to generate the curve's points.

Having generated the points, the code gets the Canvas widget's dimensions and calls transform_points (also described shortly) to fit the points to the canvas.

The method finishes by using create_line to connect the lines on the canvas.

Getting Points

The following code shows the get_point method.

def get_point(t): '''Calculate x and y for this t.''' expr = math.exp(math.cos(t)) - 2 * math.cos(4 * t) - \ math.pow(math.sin(t / 12), 5) x = math.sin(t) * expr y = -math.cos(t) * expr return (x, y)

If you look at the earlier equations, you'll see that the X and Y equations both contain a complicated expression inside parentheses. This function calculates that expression and then multiples the result by math.sin(t) and -math.cos(t) to get the X and Y coordinate. It returns those values as a tuple.

Fitting Points

It's hard to know what X and Y coordinates the butterfly curve's points will span without doing some analysis. That would work, but it's easier to just generate the points and then look at their coordinates. Then you can figure out how to translate and scale the points to position them within a target area like this program's canvas.

My post Map points so they fit within a target area in Python does exactly that. It describes a transform_list_of_lists function that transforms a list of lists of points to fit into a target area. This example is a bit simpler because it has a single list of points rather than a list of lists of points.

Look at the previous example for an explanation of how it works. Download this example to see the version of the code that this example uses to transform its list of points.

Conclusion

The butterfly curve is technically not a fractal, although it does exhibit some of the same characteristics of a fractal. In particular, it uses equations that are simple (relatively speaking) to draw a complex shape. It also draws several curves that are approximately self-similar, although they repeat after t exceeds 24 π.

Download the example to experiment with it and to see additional details.

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.