协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
matplotlib-pro【开发与工程】Professional sub-skill for Matplotlib focused on high-performance animations, complex multi-figure layouts (GridSpec), interactive widgets, and publication-ready typography (LaTeX/PGF).
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Beyond static plots, Matplotlib is a powerful engine for dynamic data visualization and scientific storytelling. This guide focuses on the "Pro" features: blitting for speed, Artist hierarchy for control, and LaTeX integration for papers.
Everything you see is an Artist. Figures contain Axes, Axes contain Lines, Text, Patches. Pro-level control means manipulating these objects directly instead of using high-level plt commands.
Standard animation redraws the whole figure every frame (slow). Blitting only redraws the parts that changed (e.g., the moving line), while keeping the axes and labels cached as a background image.
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2) # Returns the Line2D artist
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return line, # Note the comma
def update(frame):
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x + frame/10.0)
line.set_data(x, y)
return line,
# blit=True is critical for performance
ani = FuncAnimation(fig, update, frames=100, init_func=init, blit=True)
plt.show()
import matplotlib as mpl
mpl.use("pgf") # Use PGF for perfect LaTeX integration
mpl.rcParams.update({
"pgf.texsystem": "pdflatex",
"font.family": "serif",
"text.usetex": True,
"pgf.rcfonts": False,
})
fig.savefig("figure.pgf") # Import this directly into your LaTeX doc
import matplotlib.gridspec as gridspec
fig = plt.figure(constrained_layout=True)
gs = gridspec.GridSpec(3, 3, figure=fig)
ax_main = fig.add_subplot(gs[0:2, :]) # Top 2/3rds
ax_hist_x = fig.add_subplot(gs[2, 0:2]) # Bottom left
ax_hist_y = fig.add_subplot(gs[2, 2]) # Bottom right
from matplotlib.widgets import Slider
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
line, = ax.plot(x, np.sin(x))
ax_freq = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(ax_freq, 'Freq', 0.1, 30.0, valinit=1.0)
def update(val):
line.set_ydata(np.sin(slider.val * x))
fig.canvas.draw_idle() # Optimized redraw
slider.on_changed(update)
.svg or .pdf to ensure labels don't pixelate in reports.plt.close() to prevent memory leaks in notebooks.fig.get_size_inches() and scale relative to the figure size for portability.fig.set_constrained_layout(True).Matplotlib Pro is the bridge between data and insight. Mastering the blitting engine and the PGF backend allows scientists to create dynamic evidence that is as visually compelling as it is mathematically rigorous.