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 hypotrochoi…


This content originally appeared on DEV Community and was authored by Chris Greening

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

  • Prerequisite imports
  • Creating a hypotrochoid
  • Plotting the hypotrochoid
  • Conclusion

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

"Buy Me A Coffee"

Official website

Official docs

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

Table of Contents

💻 Installation

pip

Install the latest stable release from PyPI using

$ pip3 install spyrograph

or clone the development version from GitHub with

$ git clone https://github.com/chris-greening/spyrograph.git

🌱 Quickstart

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




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

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)
)

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()

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!


This content originally appeared on DEV Community and was authored by Chris Greening


Print Share Comment Cite Upload Translate Updates
APA

Chris Greening | Sciencx (2023-03-30T02:12:26+00:00) Plotting beautiful spirographs with matplotlib and spyrograph. Retrieved from https://www.scien.cx/2023/03/30/plotting-beautiful-spirographs-with-matplotlib-and-spyrograph/

MLA
" » Plotting beautiful spirographs with matplotlib and spyrograph." Chris Greening | Sciencx - Thursday March 30, 2023, https://www.scien.cx/2023/03/30/plotting-beautiful-spirographs-with-matplotlib-and-spyrograph/
HARVARD
Chris Greening | Sciencx Thursday March 30, 2023 » Plotting beautiful spirographs with matplotlib and spyrograph., viewed ,<https://www.scien.cx/2023/03/30/plotting-beautiful-spirographs-with-matplotlib-and-spyrograph/>
VANCOUVER
Chris Greening | Sciencx - » Plotting beautiful spirographs with matplotlib and spyrograph. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/30/plotting-beautiful-spirographs-with-matplotlib-and-spyrograph/
CHICAGO
" » Plotting beautiful spirographs with matplotlib and spyrograph." Chris Greening | Sciencx - Accessed . https://www.scien.cx/2023/03/30/plotting-beautiful-spirographs-with-matplotlib-and-spyrograph/
IEEE
" » Plotting beautiful spirographs with matplotlib and spyrograph." Chris Greening | Sciencx [Online]. Available: https://www.scien.cx/2023/03/30/plotting-beautiful-spirographs-with-matplotlib-and-spyrograph/. [Accessed: ]
rf:citation
» Plotting beautiful spirographs with matplotlib and spyrograph | Chris Greening | Sciencx | https://www.scien.cx/2023/03/30/plotting-beautiful-spirographs-with-matplotlib-and-spyrograph/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.