举例:
假设通过爬虫你获取到了2016年3,10月份每天白天的最高气温(分别位于列表a,b)
#那么此时如何寻找出气温和随时间(天)变化的某种规律?
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="c:\windows\fonts\simsun.ttc")
y_3 = [11,17,16,12,11,12,16,16,7,8,9,12,15,14,17,18,21,16,17,19,15,16,8,19,22,25,26,22,15,16]
y_10 = [26,26,25,18,19,17,16,26,20,8,16,14,15,8,9,16,23,15,18,16,22,23,26,26,23,15,26,23,22,19]
x_3 = range(1,31)
x_10 = range(51,81)
# 设置图形大小
plt.figure(figsize=(20,8),dpi=80)
# 注释:figsize中的俩个参数表示长宽 dpi表示清晰度
# 使用scatter绘制散点图
plt.scatter(x_3,y_3,label="3月份")
plt.scatter(x_10,y_10,label="10月份")
# 注释:lable参数是代表图例
# 调整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)
# 注释:这里取步长为3是避免横坐标太密集
# 添加图例
plt.legend(loc="upper left",prop=my_font)
# 注释:这里将图例放于左上的位置,什么参数不都不写默认将图例放于最优的位置
# 添加描述信息
plt.xlabel("时间",fontproperties=my_font)
plt.ylable("温度",fontproperties=my_font)
plt.title("标题",fontproperties=my_font)
# 展示
plt.show()
结果:
用matplotlib绘制散点图与绘制折线图并没有太大的区别,最大的区别就是绘图的时候这里调用的是scatter方法,而绘制折线图用的是plot方法