使用python-matplotlib.pyplot库实现网红matplotlib动态爱心,不多废话直接上代码:

import time
import matplotlib.pyplot as plt
import numpy as np
import math

def pow(x,a):
    return math.pow(x,a)

plt.ion()
figure,ax=plt.subplots()
lines,=ax.plot([],[],color="red")
ax.set_autoscaley_on(True)
ax.grid()
X=np.linspace(-1.8,1.8,1000)
a=1

while True:
	#设置函数
    y = [pow(pow(x, 2), 1 / 3) + 0.9 * pow(3.3 - x * x, 0.5) * np.sin(a * np.pi * x) for x in X]
    a=a+0.1
    lines.set_xdata(X)
    lines.set_ydata(y)
    ax.relim()
    ax.autoscale_view()
    figure.canvas.draw()
    figure.canvas.flush_events()
    time.sleep(0.01)

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐