Python如何绘制K线图?

首页 > 教育 > 正文 2021-06-04

发表自话题:怎么画k线图

这是py中的案例



"""
Demonstrate creation of a custom graphic (a candlestick plot)

"""
# import initExample ## Add path to library (just for examples; you do not need this)

import py as pg
from py import QtCore, QtGui


## Create a subclass of GraphicsObject.
## The only required methods are paint() and boundingRect()
## (see 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 object allows paint() to run much more ,
## rather than re-drawing the shapes every time.
self.picture = QtGui.()
p = QtGui.(self.picture)
p.setPen(pg.mkPen('w'))
w = (self.data[1][0] - self.data[0][0]) / 3.
print('w is:',w)
for (t, open, close, min, max) in self.data:
p.drawLine(QtCore.(t, min), QtCore.(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, does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())


data = [ ## fields are (time, open, close, min, max).
(1., 10, 13, 5, 15),
(2., 13, 17, 9, 20),
(3., 17, 14, 11, 23),
(4., 14, 15, 5, 19),
(5., 15, 9, 8, 22),
(6., 9, 15, 8, 16),
]
item = CandlestickItem(data)
plt = pg.plot()
plt.addItem(item)
plt.setWindowTitle('py example: customGraphicsItem')

## 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..instance().exec_()

标签组:[python

上一篇如何使用Matlab绘制常见的K线图(蜡烛图)

下一篇使用Python画股票的K线图的方法步骤-其它代码类资源-CSDN下载

相关阅读

相同话题文章

推荐内容

热门阅读