import pyqtgraph as pg
import matplotlib.dates as datesx
import tushare as ts
import datetime as dt
import numpy as np
from pyqtgraph import QtCore, QtGui
## Create a subclass of GraphicsObject.
## The only required methods are paint() and boundingRect()
## (see QGraphicsItem documentation)
class CandlestickItem(pg.GraphicsObject):
def __init__(self, data):
pg.GraphicsObject.__init__(self)
self.data = data ## data must have fields: time, open, close, min, max
self.generatePicture()
def generatePicture(self):
## pre-computing a QPicture object allows paint() to run much more quickly,
## rather than re-drawing the shapes every time.
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('w'))
#w = (datesx.date2num(self.data[1][0]) - datesx.date2num(self.data[0][0])) / 3.
w = (self.data[1][0] - self.data[0][0]) / 3.
for (t, open, close, min, max) in self.data:
#t_int= datesx.date2num(t_date)
p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max))
if open > close:
p.setBrush(pg.mkBrush('r'))
else:
p.setBrush(pg.mkBrush('g'))
p.drawRect(QtCore.QRectF(t-w, open, w*2, close-open))
p.end()
def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
## boundingRect _must_ indicate the entire area that will be drawn on
## or else we will get artifacts and possibly crashing.
## (in this case, QPicture does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())
hist_data = ts.get_hist_data('600519',start='2017-05-01',end='2018-03-06').sort_index()
xdict = dict(enumerate(hist_data.index))
axis_1 = [(i,list(hist_data.index)[i]) for i in range(0,len(hist_data.index),10)]
app = pg.QtGui.QApplication([])
win = pg.GraphicsWindow(title='无下坐标K线')
#win = pg.plot()
stringaxis = pg.AxisItem(orientation='bottom')
stringaxis.setTicks([axis_1,xdict.items()])
data_list = []
int_xi=0
for dates,row in hist_data.iterrows():
# 将时间转换为数字
date_time = dt.datetime.strptime(dates,'%Y-%m-%d')
t = datesx.date2num(date_time)
open,high,close,low = row[:4]
datasitem = (int_xi,open,close,low,high)
data_list.append(datasitem)
int_xi=int_xi+1
item = CandlestickItem(data_list)
plt = win.addPlot(axisItems={'bottom': stringaxis},title='600519')
#plt .setLabel(axis='bottom',text='日期')
plt.addItem(item)
plt.setWindowTitle('pyqtgraph example: customGraphicsItem')
plt.showGrid(x=True,y=True)
plt.plot(x=list(xdict.keys()), y=hist_data['ma5'].values, pen='r', name='5日线')
plt.plot(x=list(xdict.keys()), y=hist_data['ma10'].values, pen='g', name='10日线')
plt.plot(x=list(xdict.keys()), y=hist_data['ma20'].values, pen='b', name='20日线')
#plt.plot(x=list(xdict.keys()), y=hist_data['v_ma5'].values, pen='r', name='5日线')
#plt.plot(x=list(xdict.keys()), y=hist_data['v_ma10'].values, pen='g', name='10日线')
#plt.plot(x=list(xdict.keys()), y=hist_data['v_ma20'].values, pen='b', name='20日线')
#xax = plt.getAxis('bottom')
#xax.setTicks(axis_1)
win.nextRow()
p2 = win.addPlot(title="Multiple curves")
p2.plot(np.random.normal(size=100), pen=(255,0,0), name="Red curve")
#p2.plot(np.random.normal(size=110)+5, pen=(0,255,0), name="Green curve")
#p2.plot(np.random.normal(size=120)+10, pen=(0,0,255), name="Blue curve")
print(list(xdict.keys()))
print(hist_data['volume'].values)
#bg1=pg.BarGraphItem(x=list(xdict.keys()), height=hist_data['volume'].values, width=0.3, brush='r')
#win.addItem(p2)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()