(数据分析)Python-matpoltlib库-绘制散点图

本文展示了如何使用Python的matplotlib库来绘制北京2016年3月和10月每日最高气温的变化散点图。通过散点图,可以直观地观察到气温随时间的波动情况,x轴表示日期,y轴表示温度,不同月份的数据用不同颜色区分,同时对x轴刻度进行了调整并添加了图例,以便于理解图表信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题引入

假设通过爬虫你获取到了北京2016年3,10月份每天白天的最高气温(分别位于列表a,b),那么此时如何寻找出气温和随时间(天)变化的某种规律?
a = [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]
b = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]

数据来源: http://lishi.tianqi.com/beijing/index.html

绘制散点图

在python中画散点图主要是用matplotlib模块中的scatter函数。

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="C:\\Windows\\Fonts\\simfang.ttf")

y_3 = [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]
y_10 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17,
        20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13, 12, 13, 6]
x_3 = range(1, 32)
x_10 = range(51, 82)
# 设置图像大小
plt.figure(figsize=(20, 8), dpi=80)
# 绘制
plt.scatter(x_3, y_3, label="3月份")
plt.scatter(x_10, y_10, label="10月份")

# 调整x轴的刻度
_x = list(x_3) + list(x_10)
_xtick_lables = ["3月{}日".format(i) for i in x_3]
_xtick_lables += ["10月{}日".format(i - 50) for i in x_10]
plt.xticks(_x[::3], _xtick_lables[::3], fontproperties=my_font, rotation=45)

# 添加图例
plt.legend(prop=my_font, loc="upper right")

# 添加描述信息
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度 单位(℃)", fontproperties=my_font)
plt.title("标题", fontproperties=my_font)

plt.show()

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值