• python画折线图


    一、从mysql库中查询数据

    新建一个py文件,写查询数据的方法

    import pymysql
    
    def Read_database (sql):
        user_name='root'
        password='root'
        address='localhost'
        port='3306'
        database_name='test'
    
        conn = pymysql.connect(host = address,user = user_name,passwd = password,
                               db = database_name , port = int(port) ,charset = "utf8mb4")
        try:
            #df = pd.read_sql (sql,con = conn)
            cursor = conn.cursor()
            cursor.execute(sql)
            df = cursor.fetchall()
        except:
            print ('
     Reading Error  
    ')    
        finally:
            conn.close()
        print ('
     Completion of data reading 
    ')    
        return (df) 

    二、再新建py文件,查询出数据并构建折线图的数据

    indexData = Read_database("SELECT * FROM `cryindex` where CreateTime >='2019-01-01' and CreateTime < '2020-10-17' ORDER BY CreateTime")
    
    indexList = []
    timeList = []
    
    for i in indexData:
        indexList.append(i[1])
        timeList.append(i[0])

    三、画出折线图

    # plot中参数的含义分别是横轴值,纵轴值,线的形状,颜色,透明度,线的宽度和标签
    plt.plot(timeList,
             indexList,
             'ro-',
             color='#4169E1',
             alpha=0.8,
             linewidth=1,
             label='I')
    # 显示标签,如果不加这句,即使在plot中加了label='一些数字'的参数,最终还是不会显示标签
    plt.legend(loc="upper right")
    plt.xlabel('x轴数字')
    plt.ylabel('y轴数字')
    
    plt.show()
  • 相关阅读:
    MxNet Windows下安装
    Binary Tree Postorder Traversal--leetcode难题讲解系列
    Populating Next Right Pointers in Each Node II--leetcode难题讲解系列
    Recover Binary Search Tree--leetcode难题讲解
    bash + script
    Linux笔记
    谷歌面经 Tree Serialization
    Python strange questions list
    bit操作 转
    ubuntu系统从中文环境改成英文环境
  • 原文地址:https://www.cnblogs.com/dayang12525/p/13835050.html
Copyright © 2020-2023  润新知