最被低估的Python绘图库!Matlplotlib 超强实力鉴赏

Matplotlib动画:Sin-Cos实操演示与科学可视化教程
本文介绍了如何使用Matplotlib库在Python中创建动态的sin-cos曲线动画,并分享了开源项目Scientific Visualization Book中的实例。通过代码实现,读者可以体验到Matplotlib的强大和灵活性。

bba9db18d5fb0c64d64e28fb372cfe75.png

Matplotlib

Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。

Python宇宙的绘图库层出不穷,比如boken seaborn pyechart altair plotly pygal ggplot等等。这些库各有优势,很多也是基于Matplotlib的,用起来确实极端代码实现很酷的图表,但是也会失去一些主动性和乐趣。

今天向大家推荐一个开源项目,包括PDF书,完整代码,各种已实现图表。

https://github.com/rougier/scientific-visualization-book/

比如这个sin-cos动图的实现

8a350e630ce8790807de8928312fd489.gif
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure(figsize=(7, 2))
ax = plt.subplot()

X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)
(line1,) = ax.plot(X, C, marker="o", markevery=[-1], markeredgecolor="white")
(line2,) = ax.plot(X, S, marker="o", markevery=[-1], markeredgecolor="white")
text = ax.text(0.01, 0.95, "Test", ha="left", va="top", transform=ax.transAxes)
ax.set_xticks([])
ax.set_yticks([])


def update(frame):
    line1.set_data(X[:frame], C[:frame])
    line2.set_data(X[:frame], S[:frame])
    text.set_text("Frame %d" % frame)
    if frame in [1, 32, 128, 255]:
        plt.savefig("../../figures/animation/sine-cosine-frame-%03d.pdf" % frame)
    return line1, line2, text


plt.tight_layout()
writer = animation.FFMpegWriter(fps=30)
anim = animation.FuncAnimation(fig, update, interval=10, frames=len(X))
from tqdm.autonotebook import tqdm

bar = tqdm(total=len(X))
anim.save(
    "../../figures/animation/sine-cosine.mp4",
    writer=writer,
    dpi=100,
    progress_callback=lambda i, n: bar.update(1),
)
bar.close()

更多漂亮的案例

fc6b4293f0a2fc41423a4631a119d767.gif4b9f7e009f40573a6c874cee31a04430.gif057f8bbd5d1f55e022fe0770bcce22fa.png

2e555d7418f741faf1370624514f21ae.pngb36453ebb8f2170d1a66ab1307b71db8.png

2a3c5d7b59d365975ede0ed49811c6f6.pngc78460dddfe96002b0afd72a9169adfe.png

觉得还不错就给我一个小小的鼓励吧!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值