python实现绘制爱心函数(绘制过程)

首先,确保已经安装了matplotlib库和numpy库。如果没有安装,可以通过pip来安装:

pip install matplotlib
pip install numpy

了解心形函数公式:

x(t)=16*sin(t)^{3}

y(t)=13cos⁡(t)−5cos⁡(2t)−2cos⁡(3t)−cos⁡(4t)

定义函数:

def heart_shape(t):
    x = 16 * np.sin(t)**3
    y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
    return x, y

然后,使用以下Python代码来绘制心形线

import matplotlib.pyplot as plt
import numpy as np

def plot_function_with_process(func, start, end, points=1000):
    t = np.linspace(start, end, points)

    # 计算爱心形状的坐标
    x, y = func(t)

    # 创建一个子图
    fig, ax = plt.subplots(1, 2, figsize=(12, 4))

    # 绘制图像
    ax[0].plot(x, y)
    ax[0].set_title('Plot of the Function')
    ax[0].set_xlabel('X-axis')
    ax[0].set_ylabel('Y-axis')

    # 绘制绘图过程
    for i in range(1, 500):
        i*=4
        ax[1].clear()  # 清除上一步的绘图
        ax[1].plot(x[:i], y[:i])
        ax[1].set_title('Plotting Process')
        ax[1].set_xlabel('X-axis')
        ax[1].set_ylabel('Y-axis')
        plt.pause(0.001)  # 添加延迟以查看绘图过程

    # 显示图像
    plt.show()


def heart_shape(t):
    x = 16 * np.sin(t)**3
    y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
    return x, y


# 输入函数并绘制图像
plot_function_with_process(heart_shape, 0, 2 * np.pi)

 

 结果展示:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值