Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。
1. 简单显示
plt.plot() 绘制折线图
plt.show() 显示折线图
x坐标可以不写,默认从0开始
from matplotlib import pyplot as plt
x = range(2, 28, 2)
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
plt.plot(x, y)
plt.show()
2. 显示图片大小
plt.figure() 调整图片大小
plt.savefig() 保存图片
from matplotlib import pyplot as plt
plt.figure(figsize=(20, 15), dpi=80)
x = range(2, 28, 2)
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
plt.plot(x, y)
plt.savefig("./t1.png")
plt.show()
3. 给坐标轴设置刻度文字
plt.xticks(x坐标,刻度文字)
plt.yticks(x坐标,刻度文字)
from matplotlib import pyplot as plt
plt.figure(figsize=(20, 15), dpi=80)
_x = range(13)
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
plt.plot(y)
_xtick_labels = [f"label{i}" for i in _x]
plt.xticks(_x, _xtick_labels)
plt.savefig("./t1.png")
plt.show()
# 中文
import matplotlib
from matplotlib import pyplot as plt
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
plt.figure(figsize=(20, 15), dpi=80)
_x = range(13)
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
plt.plot(y)
_xtick_labels = [f"第{i}个" for i in _x]
plt.xticks(_x, _xtick_labels)
plt.savefig("./t1.png")
plt.show()
4. 设置说明文字
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.title("标题")
import matplotlib
from matplotlib import pyplot as plt
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
_x = range(13)
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
plt.plot(y)
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.title("标题")
plt.savefig("./t1.png")
plt.show()