Matplotlib 是一个强大的 Python 数据可视化库,用于绘制各种图形。以下是 Matplotlib 常用方法的详细说明及示例,帮助你快速上手。
1. 安装和导入 Matplotlib
安装 Matplotlib:
pip install matplotlib
导入 Matplotlib:
import matplotlib.pyplot as plt
2. 基本绘图
绘制简单折线图
import matplotlib.pyplot as plt
# 数据
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
# 绘图
plt.plot(x, y)
# 添加标题和标签
plt.title("Simple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# 显示图形
plt.show()
3. 图形样式
设置颜色、线型和标记
plt.plot(x, y, color='r