【Python】笛卡尔心形线——数学家的浪漫(花式哄小女友第一天)


简介

pythonmatplotlib库绘制心形线。

演示

心-直角坐标心-极坐标系
扁点的心动态绘制

安装

pip install matplotlib

方程

参数方程

{ x ( θ ) = a ( 1 − c o s θ ) s i n θ y ( θ ) = a ( 1 − c o s θ ) c o s θ \left\{\begin{matrix} x(\theta )=a\left(1-cos\theta \right) sin\theta \\ y(\theta )=a\left(1-cos\theta \right) cos\theta \end{matrix}\right. {x(θ)=a(1cosθ)sinθy(θ)=a(1cosθ)cosθ

极坐标方程

r = a ( 1 − s i n θ ) r=a(1-sin\theta) r=a(1sinθ)

源码

  1. 根据参数方程绘制心形线。
import matplotlib.pyplot as plt
import numpy as np


def cardioid_parametric(a):
    theta = np.linspace(0, 2*np.pi, 1000)
    x = a*(1-np.cos(theta))*np.sin(theta)
    y = a*(1-np.cos(theta))*np.cos(theta)
    plt.plot(x, y, c='r')
    plt.axis('off')
    plt.savefig('./img/heart.png')
    plt.show()


if __name__ == '__main__':
    cardioid_parametric(1) # draw a cardioid according to the parametric equation
  1. 根据极坐标方程绘制心形线。
import matplotlib.pyplot as plt
import numpy as np


def cardioid_polar(a):
    theta = np.linspace(0, 2*np.pi, 1000)
    r = a*(1 - np.sin(theta))
    graph = plt.subplot(111, polar=True)
    graph.plot(theta, r, color='red')
    plt.savefig('./img/heart-polar.png')
    plt.show()


if __name__ == '__main__':
    cardioid_polar(1) # draw a cardioid according to the polar equation
  1. 绘制一个扁点的心形线。
import matplotlib.pyplot as plt
import numpy as np


def cardioid_flat():
    t = np.linspace(0, np.pi, 1000)
    x = np.sin(t)
    y = np.cos(t) + np.power(x, 2/3)
    plt.plot(x, y, color='r')
    plt.plot(-x, y, c='r')
    plt.axis('off')
    plt.savefig('./img/heart-flat.png')
    plt.savefig('./img/heart-flat.png')
    plt.show()


if __name__ == '__main__':
    cardioid_flat() # darw a flat cardioid
  1. 动态绘制心形线。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation


figure = plt.figure()
line1, = plt.axes(xlim=(-1.5, 1.5), ylim=(-2.2, 0.45)).plot([], [], c='r')
line2, = plt.axes(xlim=(-1.5, 1.5), ylim=(-2.2, 0.45)).plot([], [], c='r')


def init():
    line1.set_data([], [])
    line2.set_data([], [])
    return line1, line2


def update(i, a):
    theta = np.linspace(0, i/np.pi, 100)
    x = a*(1-np.cos(theta))*np.sin(theta)
    y = a*(1-np.cos(theta))*np.cos(theta)
    line1.set_data(x, y)
    line2.set_data(-x, y)
    return line1, line2


def cardioid_animate(a):
    ani = animation.FuncAnimation(figure, update, init_func=init, frames=11, fargs=(a,), blit=True)
    plt.axis('off')
    ani.save('./img/heart.gif')
    plt.show()


if __name__ == '__main__':
    cardioid_animate(1) # darw a cardioid dynamically

拓展

1650年,斯德哥尔摩街头,52岁的笛卡尔邂逅了18岁瑞典公主克莉丝汀。笛卡尔落魄无比、穷困潦倒又不愿意请求别人的施舍,每天只是拿着破笔破纸研究数学题。有一天,克莉丝汀的马车路过街头发现了笛卡尔是在研究数学。公主便下车询问,最后笛卡尔发现公主很有数学天赋,道别后的几天笛卡尔收到通知,国王要求他做克莉丝汀公主的数学老师,其后几年中相差34岁的笛卡尔和克莉丝汀相爱,国王发现并处死了笛卡尔。在最后笛卡尔写给克莉丝汀的情书中出现了r=a(1-sinθ)的数学坐标方程,解出来是个心形图案,就是著名的“心形线”。这封情书最后被收录到欧洲笛卡尔博物馆。

仓库

https://github.com/XavierJiezou/python-cardioid-matplotlib

其它

love.gif

参考

https://en.wikipedia.org/wiki/Cardioid
https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.animation.FuncAnimation.html
https://zhuanlan.zhihu.com/p/32380300

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xavier Jiezou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值