Python3 使用 matplotlib 畫折線圖


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)

結果




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM