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

matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表。
首先我们先来看看效果图:

然后,是数据:

接着是代码:
#coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
import MySQLdb

sql = '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_list
y = 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



猜你喜欢

转载自blog.csdn.net/topkipa/article/details/76687009