假设通过爬虫你获取到了北京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]
代码实现,基本和绘制折线图实现类似,只有略微差别
1 from matplotlib import pyplot as plt 2 import matplotlib 3 4 font = {'family': 'MicroSoft YaHei'} 5 matplotlib.rc('font', **font) 6 # 三月份和十月份的气温 7 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] 8 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] 9 x_3 = range(1, 32) 10 x_10 = range(51, 82) 11 12 plt.figure(figsize=(16, 8), dpi=80) 13 # 使用scatter方法绘制散点图散点图,其它的和之前绘制折线图没有太大区别 14 plt.scatter(x_3, y_3, label='三月份') 15 plt.scatter(x_10, y_10, label='十月份') 16 17 # 调整x轴的刻度 18 _x = list(x_3) + list(x_10) 19 _xtick_labels = ['3月{}号'.format(i) for i in x_3] + ['10月{}号'.format(i-50) for i in x_10] 20 plt.xticks(_x[::3], _xtick_labels[::3], rotation=45) # 步长为3,旋转45度 21 22 # 添加描述信息 23 plt.xlabel('时间') 24 plt.ylabel('温度') 25 plt.title('气温散点图') 26 plt.legend() # 图例 27 28 plt.show() # 展示