一、折线图绘制
1、绘图流程
import matplotlib.pyplot as plt
import numpy as np
# 1、创建画布
# figsize ==画布尺寸
# dpi ---画布像素
plt.figure()
# 默认不支持中文,需要配置RC 参数
plt.rcParams['font.sans-serif'] = 'SimHei'
# 设置字体之后不支持负号,需要去设置RC参数更改编码
plt.rcParams['axes.unicode_minus'] = False
# 2、绘图
# 准备坐标
# 准备x 坐标 准备y 坐标 不这样(x0,y0) (x1,y1)
# 下周北京的天气温度
x = np.arange(1, 8)
y = np.array([30, 29, 28, 22, 23, 30, 32])
# 绘制折线图
plt.plot(x, y)
for i, j in zip(x, y):
# print(i,j)
# 参数1 x 轴位置
# 参数2 标注的y轴位置
# 参数3 标注的内容
plt.text(i, j + 0.5, "%d℃" % j, horizontalalignment='center')
# 设置横轴中文 刻度
xticks = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
plt.xticks(x, xticks)
# 3、显示图片
plt.show()
2、详细介绍
import matplotlib.pyplot as plt
import numpy as np
# 1、创建画布
# figsize=(20, 8), dpi=80
fig = plt.figure()
# 默认不支持中文,需要配置RC 参数
plt.rcParams['font.sans-serif'] = 'SimHei'
# 设置字体之后不支持负号,需要去设置RC参数更改编码
plt.rcParams['axes.unicode_minus'] = False
# 2、绘图
# 绘制sin曲线图
# 准备坐标
x = np.arange(0, 2 * np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
# get current axis 获得坐标轴对象
ax = plt.gca()
# 去除上边 和 右边的 坐标边框
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 指定