Python数据分析之——数据可视化(折线图)

来源:互联网 发布:java设置文件权限 编辑:程序博客网 时间:2024/05/21 05:56
matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表。
首先我们先来看看效果图:

然后,是数据:

接着是代码:
#coding:utf-8import numpy as npimport matplotlib.pyplot as pltimport MySQLdbsql = 'SELECT dt_date,nm_watch FROM ******* WHERE vc_name = "速度与激情8" ORDER BY dt_create DESC LIMIT 0, 10'db = MySQLdb.connect(***********)db.set_character_set('utf8')cursor = db.cursor()#执行sql语句data_list = [i for i in cursor.fetchmany(cursor.execute((sql)))]db.close()time_list = []message_list = []for i,j in data_list:    # print str(i)[-8:]     #因为数据i表示的是时间,而时间完整值是类似于2017-08-04 15:59:08这样的数据,我们只取其中时分秒并去掉“:”    # print j    time_list.append((str(i)[-8:]).replace(':',''))    message_list.append(j)x = time_listy = message_list
#设置画布像素plt.figure(figsize=(8,6))#给X、Y轴赋值plt.plot(x,y,"r",linewidth=1)#设置X、Y轴名称plt.xlabel("X")plt.ylabel("Y")plt.show()

关于一些参数:
plt.xlabel()
plt.ylabel()
plt.title()
plt.ylim()
plt.legend()

xlabel : 设置X轴的文字
ylabel : 设置Y轴的文字
title : 设置图表的标题
ylim : 设置Y轴的范围
legend : 显示图示

如果大家想系统的学习的话,这有一个链接分享给大家,是一本叫《用python做科学计算》的书:
http://old.sebug.net/paper/books/scipydoc/matplotlib_intro.html



阅读全文
6 0
原创粉丝点击