使用python内置库matplotlib,实现折线图的绘制
发布时间:2020-12-17 17:06:39 所属栏目:Python 来源:网络整理
导读:环境准备: 需要安装matplotlib,安装方式: pip install matplotlib ? 直接贴代码喽: 1 # 引入模块 2 from matplotlib import pyplot,font_manager 3 4 # 设置支持中文字体的显示 5 font=font_manager.FontProperties(fname= " C:WindowsFontssimsun.tt
环境准备: 需要安装matplotlib,安装方式: pip install matplotlib ? 直接贴代码喽: 1 #引入模块 2 from matplotlib import pyplot,font_manager 3 4 #设置支持中文字体的显示 5 font=font_manager.FontProperties(fname="C:WindowsFontssimsun.ttc") 6 7 #第一步:准备数据 8 #气温值 9 y1 = [8,5,7,8,6,9,11,10,14,13,12,15,16,14] 10 y2 = [11,17,18,19,20,19] 11 #3月份 12 x = [i for i in range(1,32)] 13 14 #设置图片大小,figsize:设置图片的宽和高,dpi设置每英寸的像素 15 pyplot.figure(figsize=(30,16),dpi=100) 16 17 #给图表起名字 18 pyplot.title('三月份气温变化图',fontproperties=font) 19 20 #绘制图像 21 pyplot.plot(x,y1,label='最低气温',color="red",linewidth=5,linestyle="--") #最低气温 22 pyplot.plot(x,y2,label='最高气温',color="cyan",linewidth=6) #最高气温 23 24 #显示每条线代表什么 25 pyplot.legend(loc="upper left",prop=font) 26 27 #设置X轴坐标 28 pyplot.xticks(x) 29 #设置网格线 30 pyplot.grid(alpha=0.2) 31 32 33 #保存图像 34 pyplot.savefig('./weather.png') 35 36 #显示图像 37 pyplot.show() 最终实现的效果: ? 最后附上官网地址,里边有很多图表,可根据实际需求进行修改: https://matplotlib.org/gallery/index.html ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |