from matplotlib import pyplot as plt
from matplotlib import font_manager
import random
# 设置字体用来显示中文
font_list = sorted([f.name for f in font_manager.fontManager.ttflist])
for i in font_list:
print(i)
# 设置字体显示
plt.rcParams['font.family'] = ['Microsoft YaHei']
# 准备数据
x_3 = range(1, 32)
x_10 = range(51, 82)
y_3 = [random.randint(18, 30) for i in range(31)]
y_10 = [random.randint(18, 30) for i in range(31)]
# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)
# 自定义刻度
_x = list(x_3)+list(x_10)
_xtick_labels = ["3月{}日".format(i) for i in x_3]
_xtick_labels += ["10月{}日".format(i-50) for i in x_10]
plt.xticks(_x[::3], _xtick_labels[::3], rotation=45)
# 两次绘图,
plt.scatter(x_3, y_3)
plt.scatter(x_10, y_10)
# 绘制网格,alpha参数设置网格透明度0-1的值
plt.grid(alpha=0.4, linestyle=':')
# 保存图片
plt.savefig("./t2.png")
# plt.show()
运行