1. 子图与多面板布局 📌
在复杂数据分析中,使用 subplots
创建多面板图表是常见需求。通过以下代码可快速生成网格布局:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1,2,3], [1,2,3]) # 1. 简单折线图
2. 风格定制与主题切换 🎨
Matplotlib 提供了丰富的样式配置选项:
- 使用
plt.style.use('ggplot')
切换主题 - 通过
rcParams
全局设置字体/颜色 - 自定义图例与坐标轴样式
3. 动画制作与动态可视化 📈
利用 matplotlib.animation
模块实现动态效果:
from matplotlib.animation import FuncAnimation
fig = plt.figure()
def update(frame):
plt.plot([frame, frame+1], [frame, frame+1])
ani = FuncAnimation(fig, update, frames=range(10))