python matplotlib库绘制散点图例题解析
假设通过爬虫你获取到了北京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]
代码实现,基本和绘制折线图实现类似,只有略微差别
from matplotlib import pyplot as plt
import matplotlib
font = {'family': 'MicroSoft YaHei'}
matplotlib.rc('font', **font)
# 三月份和十月份的气温
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=(16, 8), dpi=80)
# 使用scatter方法绘制散点图散点图,其它的和之前绘制折线图没有太大区别
plt.scatter(x_3, y_3, label='三月份')
plt.scatter(x_10, y_10, label='十月份')
# 调整x轴的刻度
_x = list(x_3) + list(x_10)
_xtick_labels = ['3月{}号'.format(i) for i in x_3] + ['10月{}号'.format(i-50) for i in x_10]
plt.xticks(_x[::3], _xtick_labels[::3], rotation=45) # 步长为3,旋转45度
# 添加描述信息
plt.xlabel('时间')
plt.ylabel('温度')
plt.title('气温散点图')
plt.legend() # 图例
plt.show() # 展示
效果图
以上是 python matplotlib库绘制散点图例题解析 的全部内容, 来源链接: utcz.com/z/312480.html