在上兩篇文章中,我們介紹了:

使用PyQtGraph模組的三種方式:

http://

zmister。com/archives/21

8。html

使用PyQtGraph繪製圖形的6種方法:

http://

zmister。com/archives/21

9。html

本篇文章將介紹使用PyQtGraph繪製一個精美折線圖,

文章首發於個人部落格:pyqtgraph資料視覺化3:使用PyQtGraph繪製精美折線圖--以上證指數為例 - 州的先生zmister.com

在瞭解了基本的PyQtGraph模組繪製圖形功能之後,我們通過幾個常用常見的資料視覺化圖形來演示使用PyQtGraph進行Python資料視覺化。

本篇,我們介紹使用PyQtGraph模組繪製一個完整的折線圖,透過tushare模組獲取上證指數過去兩個月的指數波動資料作為資料來源。

下面我們分步驟講解這個折線圖形的繪製。

引入相關模組

在本例中,我們需要使用到pyqtgraph模組、numpy模組和tushare模組。

import pyqtgraph as pg

import tushare as ts

import numpy as np

獲取資料來源

我們使用tushare模組的get_hist_data()方法獲取上證指數從2017年10月到2017年12月的歷史行情資料:

data = ts。get_hist_data(‘sh’,start=‘2017-10-01’,end=‘2017-12-01’)。sort_index()

返回的是一個Pandas的DataFrame資料結構,操作起來很方便。

處理資料來源

在獲取到上證指數的歷史行情資料之後,我們需要對其進行一些處理,以方便其後進行座標軸刻度文字的設定。

首先,將data的日期索引轉換為一個字典:

xdict = dict(enumerate(data。index))

再按5步長來去data的索引,生成一個包含索引序號和索引值元組的列表:

axis_1 = [(i,list(data。index)[i]) for i in range(0,len(data。index),5)]

繪製圖形

在稍微處理好資料來源之後,我們就可以進行圖形繪製了。

首先例項化一個QT例項:

app = pg。QtGui。QApplication([])

接著藉助GraphicsWindow()子模組建立一個空的圖形視窗,並使用title引數設定了視窗的標題:

win = pg。GraphicsWindow(title=‘州的先生zmister。com pyqtgraph資料視覺化 - 繪製精美折線圖’)

透過之前建立的字典xdict和列表axis_1,設定圖形的X座標軸刻度文字,orientation引數表示座標軸的位置:

stringaxis = pg。AxisItem(orientation=‘bottom’)

stringaxis。setTicks([axis_1,xdict。items()])

在視窗中新增一個空的圖形,透過axisItems引數指定座標軸及其內容,並使用title引數設定了圖形的標題:

plot = win。addPlot(axisItems={‘bottom’: stringaxis},title=‘上證指數 - zmister。com繪製’)

在圖形中新增一個文字:

label = pg。TextItem()

plot。addItem(label)

設定圖形的圖例:

plot。addLegend(size=(150,80))

設定圖形網格的形式,我們設定顯示橫線和豎線,並且透明度惟0。5:

plot。showGrid(x=True, y=True, alpha=0。5)

繪製開盤和收盤的指數,pen引數表示線的顏色,name引數可用於圖例的顯示,symbolBrush用來設定點的顏色:

plot。plot(x=list(xdict。keys()), y=data[‘open’]。values, pen=‘r’, name=‘開盤指數’,symbolBrush=(255,0,0),)

plot。plot(x=list(xdict。keys()), y=data[‘close’]。values, pen=‘g’, name=‘收盤指數’,symbolBrush=(0,255,0))

設定圖形的軸標籤:

plot。setLabel(axis=‘left’,text=‘指數’)

plot。setLabel(axis=‘bottom’,text=‘日期’)

最後設定十字游標:

vLine = pg。InfiniteLine(angle=90, movable=False,)

hLine = pg。InfiniteLine(angle=0, movable=False,)

plot。addItem(vLine, ignoreBounds=True)

plot。addItem(hLine, ignoreBounds=True)

vb = plot。vb

def mouseMoved(evt):

pos = evt[0] ## using signal proxy turns original arguments into a tuple

if plot。sceneBoundingRect()。contains(pos):

mousePoint = vb。mapSceneToView(pos)

index = int(mousePoint。x())

pos_y = int(mousePoint。y())

print(index)

if 0 < index < len(data。index):

print(xdict[index],data[‘open’][index],data[‘close’][index])

label。setHtml(“

日期:{0}

開盤:{1}

收盤:{2}

”。format(xdict[index],data[‘open’][index],data[‘close’][index]))

label。setPos(mousePoint。x(),mousePoint。y())

vLine。setPos(mousePoint。x())

hLine。setPos(mousePoint。y())

proxy = pg。SignalProxy(plot。scene()。sigMouseMoved, rateLimit=60, slot=mouseMoved)

再按常例,呼叫app的exec_()方法即可:

app。exec_()

最終執行程式,繪製出來的圖表如下所示:

使用PyQtGraph進行Python資料視覺化:繪製精美線圖(以上證指數走勢為例)

動圖如下所示:

使用PyQtGraph進行Python資料視覺化:繪製精美線圖(以上證指數走勢為例)

是不是很簡單

有問題歡迎前往部落格pyqtgraph資料視覺化3:使用PyQtGraph繪製精美折線圖——以上證指數為例 - 州的先生zmister。com

留言討論:)