Draw a heart in Python Part 2 (SymPy)

Overview

Last night, while reading the book "Introduction to Mathematics Starting with Python", I wrote an article "Drawing a Heart in Python" (http://qiita.com/QUANON/items/ccbdfb8048f223e8892b). I was reading it in: bed: tonight as well, but I was amazed at the library SymPy introduced in Chapter 4. It was. You can calculate expressions in Python! You can easily program it as if you were writing an equation on paper with a pencil and solving it. I wondered if this would simplify the code that draws: heart: yesterday, so I jumped from: bed :.

Draw a heart

Use the same formula as in the previous Draw a heart in Python.

x = 16sin^3(t) y = 13cos(t)-5cos(2t)-2cos(3t)-cos(4t)

You can plot this formula by passing it directly to the plot_parametric function [^ 1]. It's too intuitive! : open_mouth:

from sympy.plotting import plot_parametric
from sympy import Symbol, cos, sin


def draw_heart():
  t = Symbol('t')
  x = 16 * sin(t) ** 3
  y = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)

  p = plot_parametric(x, y, autoscale=True, title='heart', show=False)
  p[0].line_color = 'pink'
  p.show()


if __name__ == '__main__':
  try:
    draw_heart()
  except KeyboardInterrupt:
    pass

figure_1.png

[^ 1]: The SymPy plotting function introduced in the book is plot. However, this time it is necessary to plot the function represented by the parameter display (parameter display), so [plot_parametric](http://docs.sympy.org/dev/modules/plotting.html#sympy.plotting. I used plot.plot_parametric).

Recommended Posts

Draw a heart in Python Part 2 (SymPy)
Draw a heart in Python
Draw a CNN diagram in Python
Draw a heart in Ruby with PyCall
Draw a tree in Python 3 using graphviz
Draw graph in python
Draw a graph of a quadratic function in Python
[Python] How to draw a histogram in Matplotlib
Take a screenshot in Python
Create a function in Python
Draw mp3 waveform in Python
Create a dictionary in Python
Draw "Draw Ferns Programmatically" in Python
Make a bookmarklet in Python
Draw implicit function in python
UI Automation Part 2 in Python
Study math with Python: Draw a sympy (scipy) graph with matplotlib
Draw Sine Waves in Blender Python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
Create a DI Container in Python
Draw knots interactively in Plotly (Python)
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Create a binary file in Python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Create a random string in Python
Schedule a Zoom meeting in Python
When writing a program in Python
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
Draw a watercolor illusion with edge detection in Python3 and openCV3
Generate a first class collection in Python
Basic Linear Algebra Learned in Python (Part 1)
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Solve ABC175 A, B, C in Python
Use print in a Python2 lambda expression
GUI creation in python using tkinter part 1
A simple HTTP client implemented in Python
AM modulation and demodulation in Python Part 2
Precautions when pickling a function in python
Write the test in a python docstring
Display a list of alphabets in Python 3
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Create a simple GUI app in Python
Draw Nozomi Sasaki in Excel with python
Create a JSON object mapper in Python
Write a short property definition in Python
[Python] [Windows] Take a screen capture in Python
Run the Python interpreter in a script
How to get a stacktrace in python
Write a Caesar cipher program in Python