Skip to content

Instantly share code, notes, and snippets.

@acbart
Created September 1, 2022 14:34
Show Gist options
  • Select an option

  • Save acbart/7de88f9fcedd2f190beebd7365771c9a to your computer and use it in GitHub Desktop.

Select an option

Save acbart/7de88f9fcedd2f190beebd7365771c9a to your computer and use it in GitHub Desktop.
heart.py
# -*- coding: utf-8 -*-
"""
This program draws a heart using MatPlotLib.
You don't have to worry about how it does it, just
make sure that it runs. If it's successful, a heart
will appear in a new window.
"""
# Import graphing library
import matplotlib.pyplot as plt
# Import math library
from numpy import pi, sin, cos, arange
# Define some simple constants
CIRCLE_CIRCUMFERENCE = 2 * pi
STEP_RATE = 0.1
# Make a list of decimal numbers from 0 to 2*PI
steps = arange(0, CIRCLE_CIRCUMFERENCE, STEP_RATE)
# Calculate the X/Y coordinates using math
x_coordinates = (2 * sin(steps)) ** 3
y_coordinates = (13 * cos(1 * steps) - 5 * cos(2 * steps) -
2 * cos(3 * steps) - 1 * cos(4 * steps))
# Make the graph style XKCD
plt.xkcd()
# Plot the coordinates
plt.plot(x_coordinates, y_coordinates, color='red')
# Make the graph appear
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment