Matplotlib 是 Python 中常用的科学绘图库,可生成高质量的 2D/3D 图表。以下是基础使用指南:
安装与导入
pip install matplotlib
import matplotlib.pyplot as plt
📌 了解更多安装技巧
常用图表类型
- 📈 折线图:
plt.plot(x, y)
- 📊 柱状图:
plt.bar(x, height)
- 📐 散点图:
plt.scatter(x, y)
- 📌 饼图:
plt.pie(labels, sizes)
示例代码
plt.plot([1, 2, 3], [5, 7, 4])
plt.title('示例图表')
plt.xlabel('X轴')
plt.ylabel('Y轴')
plt.show()
进阶功能
- 📈 动态图表:使用
matplotlib.animation
- 🧠 自定义样式:
plt.style.use('ggplot')
- 📁 保存图像:
plt.savefig('output.png')
学习资源
- Matplotlib 官方文档(英文)
- Seaborn 可视化教程(推荐进阶学习)
- 📘 Python 数据可视化指南