您的位置:首页 > 编程语言 > Python开发

Python3 使用 matplotlib 画折线图

2017-09-19 15:11 357 查看
ChartUtil.py
import matplotlib.pyplot as plt
from pylab import mpl

def plotLine(xData,yData,xLabel,chartTitle):

mpl.rcParams['font.sans-serif'] = ['FangSong']  # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

#xData = range(0, 5)

#xLabel = ["2009-June", "2009-Dec", "2010-June", "2010-Dec", "2011-June"]

ax.set_xticks(range(len(xLabel)))

ax.set_xticklabels(xLabel, rotation=40)

ax.set_title(chartTitle)#设置标题

#yData = [10, 13, 5, 40, 30]

ax.plot(xData, yData, marker='o')

# plt.plot(x1,y1,label='Frist line',linewidth=1.5,color='blue',marker='o',markerfacecolor='red',markersize=7)

# plt.plot(x1,y1,'bo')
plt.show()
使用
import  DbUtil as dbu
import ChartUtil as cu

conn, cursor = dbu.getDbConnection()

sql="select * from data_house where create_time BETWEEN '2017-09-18 00:00:00' and '2017-09-19 00:00:00' ";

cursor.execute(sql);
results = cursor.fetchall();

xLable=[]
yData=[]
for row in results:
xLable.append(row[3])
yData.append(row[4])
createTime=row[2]

dbu.closeDb(conn, cursor)

dataCount=len(xLable)
xData=range(0,dataCount,1)
chartTitle=str(createTime) + '      Average Price'

cu.plotLine(xData, yData, xLable,chartTitle)
结果

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: