import matplotlib.pyplot as plt
# 图像保存# 创建画布
plt.figure(figsize=(20,8), dpi=100)# 绘制图像
x =[1,2,3]
y =[4,5,6]
plt.plot(x, y)# 图像保存
plt.savefig("data/text1.png")# 显示图像
plt.show()
注意:plt.show()会释放figure资源,如果在显示图像之后保存图片将这只能保存空图片。
3. 添加x,y轴刻度
import matplotlib.pyplot as plt
import random
# 生成数据
x =range(60)
y =[random.uniform(10,15)for i in x]# 创建画布
plt.figure(figsize=(20,8), dpi=100)# 图像绘制
plt.plot(x, y)# 添加x,y轴刻度
y_ticks =range(40)
x_ticks_labels =["11h{}m".format(i)for i in x]
plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5])# 参数1必须是数字# 图像显示
plt.show()
4. 添加网格
import matplotlib.pyplot as plt
import random
# 生成数据
x =range(60)
y =[random.uniform(10,15)for i in x]# 创建画布
plt.figure(figsize=(20,8), dpi=100)# 图像绘制
plt.plot(x, y)# 添加x,y轴刻度
y_ticks =range(40)
x_ticks_labels =["11h{}m".format(i)for i in x]
plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5])# 参数1必须是数字# 添加网格
plt.grid(True, linestyle="--", alpha=1)# 图像显示
plt.show()
5. 添加描述信息
import matplotlib.pyplot as plt
import random
# 生成数据
x =range(60)
y =[random.uniform(10,15)for i in x]# 创建画布
plt.figure(figsize=(20,8), dpi=100)# 图像绘制
plt.plot(x, y)# 添加x,y轴刻度
y_ticks =range(40)
x_ticks_labels =["11h{}m".format(i)for i in x]
plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5])# 参数1必须是数字# 添加网格
plt.grid(True, linestyle="--", alpha=1)# 添加描述信息
plt.xlabel("时间", fontsize=20)
plt.ylabel("温度", fontsize=20)
plt.title("一小时温度变化图", fontsize=30)# 图像显示
plt.show()
6. 多条数据线、显示图例
import matplotlib.pyplot as plt
import random
# 生成数据
x =range(60)
y =[random.uniform(10,15)for i in x]
y_avg =[random.uniform(15,25)for i in x]# 创建画布
plt.figure(figsize=(20,8), dpi=100)# 图像绘制 - label:图例名
plt.plot(x, y, label="最低")
plt.plot(x, y_avg, label="平均")# 添加x,y轴刻度
y_ticks =range(40)
x_ticks_labels =["11h{}m".format(i)for i in x]
plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5])# 参数1必须是数字# 添加网格
plt.grid(True, linestyle="--", alpha=1)# 添加描述信息
plt.xlabel("时间", fontsize=20)
plt.ylabel("温度", fontsize=20)
plt.title("一小时温度变化图", fontsize=30)# 显示图例 - loc:显示位置
plt.legend(loc="best")# 图像显示
plt.show()
7. 设置图形风格
颜色字符
风格字符
r 红色
- 实线
g 绿色
– 虚线
b 蓝色
-. 点划线
w 白色
: 点虚线
c 青色
‘’ 留空、空格
m 洋红
y 黄色
k 黑色
import matplotlib.pyplot as plt
import random
# 生成数据
x =range(60)
y =[random.uniform(10,15)for i in x]
y_avg =[random.uniform(15,25)for i in x]# 创建画布
plt.figure(figsize=(20,8), dpi=100)# 图像绘制
plt.plot(x, y, label="最低", color="c", linestyle="-.")
plt.plot(x, y_avg, label="平均")# 添加x,y轴刻度
y_ticks =range(40)
x_ticks_labels =["11h{}m".format(i)for i in x]
plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5])# 参数1必须是数字# 添加网格
plt.grid(True, linestyle="--", alpha=1)# 添加描述信息
plt.xlabel("时间", fontsize=20)
plt.ylabel("温度", fontsize=20)
plt.title("一小时温度变化图", fontsize=30)# 显示图例
plt.legend(loc="best")# 图像显示
plt.show()
8. 图例位置一览
位置字符串
位置编号
“best”
0
“upper right”
1
“upper left”
2
“lower left”
3
“lower right”
4
“right”
5
“center left”
6
“center right”
7
“lower center”
8
“upper center”
9
“center”
10
8. 多个坐标系显示图像
import matplotlib.pyplot as plt
import random
# 生成数据
x =range(60)
y =[random.uniform(10,15)for i in x]
y_avg =[random.uniform(15,25)for i in x]# 创建画布
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20,8), dpi=100)# 图像绘制
axes[0].plot(x, y, label="最低", color="c", linestyle="-.")
axes[1].plot(x, y_avg, label="平均")# 添加x,y轴刻度
y_ticks =range(40)
x_ticks_labels =["11h{}m".format(i)for i in x]
axes[0].set_xticks(x[::5])
axes[0].set_yticks(y_ticks[::5])
axes[0].set_xticklabels(x_ticks_labels[::5])
axes[1].set_xticks(x[::5])
axes[1].set_yticks(y_ticks[::5])
axes[1].set_xticklabels(x_ticks_labels[::5])# 添加网格
axes[0].grid(True, linestyle="--", alpha=1)
axes[1].grid(True, linestyle="--", alpha=1)# 添加描述信息
axes[0].set_xlabel("时间", fontsize=10)
axes[0].set_ylabel("温度", fontsize=10)
axes[0].set_title("一小时温度变化图", fontsize=20)
axes[1].set_xlabel("时间", fontsize=10)
axes[1].set_ylabel("温度", fontsize=10)
axes[1].set_title("一小时温度变化图", fontsize=20)# 显示图例
axes[0].legend(loc="best")
axes[1].legend(loc="best")# 图像显示
plt.show()
5. 折线图的应用场景
表述数据变化
画各种数学函数图像
注意:plt.plot()除了可以画折线图,也可以用于画各种数学函数图像
import matplotlib.pyplot as plt
import numpy as np
# 准备数据
x = np.linspace(-10,10,1000)
y = np.sin(x)# 创建画布
plt.figure(figsize=(20,10), dpi=100)# 绘制图像
plt.plot(x, y)# 添加网格显示
plt.grid()# 显示图像
plt.show()