DEV Community

Cover image for Plotting beautiful spirographs with matplotlib and Spyrograph
Chris Greening
Chris Greening

Posted on • Updated on • Originally published at chris-greening.github.io

Plotting beautiful spirographs with matplotlib and Spyrograph

DISCLAIMER: This blog post was written by a human with the help of AI

In this blog post, we'll be exploring the plot method of the Hypotrochoid class in the spyrograph library

This method allows users to create beautiful visualizations of hypotrochoids and epitrochoids using the popular matplotlib library

Black matplotlib tracing of a hypotrochoid

We will walk through the process of creating a hypotrochoid and then use the plot method to visualize it


Table of contents

GitHub logo chris-greening / spyrograph

Python library for analyzing, exploring, and visualizing epitrochoids and hypotrochoids in just a few lines of code

spyrograph: elegant mathematics and geometries

Animation of three geometric, symmetrical shapes being drawn next to one another left to right. The shape on the left is red, the middle green, and the right is blue.

What is it?

spyrograph is a lightweight Python package that provides an expressive and flexible set of tools for drawing beautiful mathematically driven art. With just a few lines of easy-to-read code you can start analyzing, visualizing, and exploring elegant mathematics

Downloads Issues License Version Documentation Status

"Buy Me A Coffee"

Official website

Official docs

Table of Contents


πŸ”‘ Key features

  • Expressive and consistent syntax
  • Robust underlying mathematics
  • Beginner and expert friendly
  • numpy is the only required third-party installation
  • Clear visualizations and animations
  • Flexible to a wide range of usecases
  • Lightweight, just plug and play

Sample hypotrochoid drawing showing a circle rolling around the interior of another circle drawing a geometric shape


πŸ’» Installation

pip

Install the latest stable release from PyPI using

$ pip3 install spyrograph
Enter fullscreen mode Exit fullscreen mode

or clone the development version from GitHub with

$ git clone https://github.com/chris-greening/spyrograph.git
Enter fullscreen mode Exit fullscreen mode

🌱 Quickstart

spyrograph is designed to be expressive and easy-to-use - simply import spyrograph and jump right into drawing elegant, complex shapes…





Prerequisite imports

Before we dive into creating and plotting a hypotrochoid, let's make sure we have spyrograph and matplotlib installed

pip3 install spyrograph matplotlib
Enter fullscreen mode Exit fullscreen mode

Creating a hypotrochoid

To create a hypotrochoid, we need to specify the parameters R (radius of the fixed circle), r (radius of the rolling circle), d (distance from the rolling circle), and thetas (a list of theta values)

Here's an example of creating a hypotrochoid:

from spyrograph import Hypotrochoid

hypotrochoid = Hypotrochoid(
    R=100,
    r=51,
    d=75,
    thetas=np.arange(0, 20 * np.pi, 0.01)
)
Enter fullscreen mode Exit fullscreen mode

Plotting the hypotrochoid

Now that we have our hypotrochoid, we can use the plot method to visualize it

This method will return a matplotlib figure and axis objects, which we can further customize if desired

fig, ax = hypotrochoid.plot(
    color="red",
    linewidth=2
)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Red matplotlib tracing of a hypotrochoid

In this example, we've specified the color of the hypotrochoid to be blue and the line width to be 1

You can customize the appearance of the plot by passing additional keyword arguments that are accepted by the matplotlib.pyplot.plot function


Conclusion

The plot method in the spyrograph library makes it incredibly easy to create visually appealing plots of hypotrochoids and epitrochoids

With just a few lines of code, we can create stunning spirograph patterns that can be used for artistic, educational, or scientific purposes

Start experimenting with different parameter values and see what amazing designs you can create!

Top comments (0)