最被低估的Python繪圖庫!Matlplotlib 超強實力鑒賞
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書/代碼/高清速查表已打包,公眾號后台回復【速查表】獲取