python 画动态心
Python 是一种编程语言,常被用于数据分析和科学计算。Python 能够绘制出各种图表,其中包括心形图。本文将介绍怎样使用 Python 画出动态的心形图。
import matplotlib.animation as animation import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() xdata, ydata = [], [] ln, = ax.plot([], [], 'r-', animated=False) def init(): ax.set_xlim(⑴, 1) ax.set_ylim(⑴, 1) return ln, def update(frame): t = np.linspace(0, 2*np.pi, 100) x = np.sin(t) y = np.cos(t) + np.sin(frame) ln.set_data(x, y) return ln, ani = animation.FuncAnimation(fig, update, frames=1000, init_func=init, blit=True) plt.show()
上述代码首先定义了一个绘图对象 fig,和一个包括坐标数据的空列表 xdata 和 ydata。接着定义了一个名为 “ln” 的线,用于绘制数据。然后定义了两个函数 init 和 update。
init 函数用于初始化绘图,设置 x 和 y 轴的范围。update 函数用于更新数据,并绘制图形。
最后使用 matplotlib.animation.FuncAnimation 方法,将初始化函数 init 和更新函数 update 进行动画制作。调用 plt.show() 函数显示动态心形图。
现在你已掌握了怎样使用 Python 绘制动态心形图。尝试修改代码,探索更多的绘图方法,创造出更加独特的动态图形。
文章来源:丸子建站
文章标题:python 画动态心
https://www.wanzijz.com/view/57281.html