Py学习  »  Python

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

happy科研 • 2 年前 • 311 次点击  

Matplotlib

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

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

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

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

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

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()

更多漂亮的案例

最后送大家5张matplotlib必备速查表

pdf书/代码/高清速查表已打包,公众号后台回复【速查表】获取

推荐阅读

  1. 决策树可视化,被惊艳到了!
  2. 开发机器学习APP,太简单了
  3. 周志华教授:关于深度学习的一点思考
  4. 200 道经典机器学习面试题总结
  5. 卷积神经网络(CNN)数学原理解析
  6. 收手吧,华强!我用机器学习帮你挑西瓜

如有收获,欢迎三连👇


Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/124919
 
311 次点击