• 使用python内置库matplotlib,实现折线图的绘制


    环境准备:

      需要安装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,5,7,8,8,7,5,6,7,9,11,10,10,11,14,13,12,12,12,12,13,14,15,16,15,15,15,15,14,14]
    10 y2 = [11,10,14,17,13,12,12,10,14,16,18,16,13,17,16,16,15,14,15,14,15,18,19,20,18,17,17,18,16,17,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

  • 相关阅读:
    svn中trunk、branches、tags
    支付宝支付对接过程
    分享插件
    ES6学习笔记
    VS code
    Angular45
    React笔记
    查询Table name, Column name, 拼接执行sql文本, 游标, 存储过程, 临时表
    通过脚本把远程服务器上的表和数据拷贝到本地数据库
    mongo客户端mongo VUE增删改查
  • 原文地址:https://www.cnblogs.com/benben-wu/p/10457836.html
Copyright © 2020-2023  润新知