导入matplotlib库中的pyplot模块,并将字体设置为SimHei

from matplotlib import pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei']

定义y轴数据

y = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22, 22, 23]

定义x轴数据

x = range(1, 32)

创建画布并设置大小

fig=plt.figure(figsize=(7,7))

绘制散点图并设置点的大小、颜色、标记和标签

plt.scatter(x, y, s=100, color='red',marker='*',label="3月份")

添加图例并设置位置为左上角

plt.legend(loc="upper left")

定义x轴刻度标签

x_tick_labels= ["3月{}日".format(i) for i in x]

设置x轴刻度并设置标签旋转角度为45度

plt.xticks(x[::3], x_tick_labels[::3], rotation=45)

设置x轴标签为“日期”,y轴标签为“温度”,标题为“散点图”

plt.xlabel("日期") plt.ylabel("温度") plt.title("散点图")

显示图形

plt.show(


原文地址: https://www.cveoy.top/t/topic/eCOi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录