zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    PHP网络编程
    traits的使用
    在Apache中使用mod_rewrite模块重写URL
    Lamp下安全配置随笔
    微信开发准备工作
    SQL SERVER技术内幕之7 透视与逆透视
    MVP开发模式的理解
    SQL SERVER技术内幕之5 表表达式
    SQL SERVER技术内幕之4 子查询
    SQL Server Management Studio
  • 原文地址:https://www.cnblogs.com/benben-wu/p/10457836.html
Copyright © 2011-2022 走看看