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 :.
Use the same formula as in the previous Draw a heart in Python.
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
[^ 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