一、从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()