PyQt5_pyqtgraph蜡烛图
目录
效果:
代码:
使用:
效果:
代码:
import sys
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import Qt
from typing import Any,Dict
import pyqtgraph as pg
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
自定义横坐标控件
class RotateAxisItem(pg.AxisItem):def drawPicture(self, p, axisSpec, tickSpecs, textSpecs):p.setRenderHint(p.Antialiasing,False)p.setRenderHint(p.TextAntialiasing,True)## draw long line along axispen,p1,p2 = axisSpecp.setPen(pen)p.drawLine(p1,p2)p.translate(0.5,0) ## resolves some damn pixel ambiguity## draw ticksfor pen,p1,p2 in tickSpecs:p.setPen(pen)p.drawLine(p1,p2)## draw all text# if self.tickFont is not None:# p.setFont(self.tickFont)p.setPen(self.pen())for rect,flags,text in textSpecs:# this is the important partp.save()p.translate(rect.x(),rect.y())p.rotate(-30)p.drawText(-rect.width(),rect.height(),rect.width(),rect.height(),flags,text)# restoring the painter is *required*!!!p.restore()
绘制蜡烛控件
## 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, maxself.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('d'))w = (self.data[1][0] - self.data[0][0]) / 3.for (t, open, close, min, max) in self.data: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())
绘制蜡烛图控件
class PyQtGraphWidget(QtWidgets.QWidget):def __init__(self):super().__init__()self.init_data()self.init_ui()def init_data(self):passdef init_ui(self):self.title_label = QtWidgets.QLabel('折线图')self.title_label.setAlignment(Qt.AlignCenter)xax = RotateAxisItem(orientation='bottom')xax.setHeight(h=50)self.pw = pg.PlotWidget(axisItems={'bottom': xax})self.pw.setMouseEnabled(x=True, y=False)# self.pw.enableAutoRange(x=False,y=True)self.pw.setAutoVisible(x=False, y=True)layout = QtWidgets.QVBoxLayout()layout.addWidget(self.title_label)layout.addWidget(self.pw)self.setLayout(layout)passdef set_k_data(self,data:Dict[str,Any]):'''k线图'''self.pw.clear()title_str = data['title_str']xTick_show = [data['xTick_show']]xTick = data['xTick']x = data['x']candle_data = data['candle_data']self.y_data = candle_dataself.xTick = xTickself.title_label.setText(title_str)xax = self.pw.getAxis('bottom')xax.setTicks(xTick_show)candle_item_list = CandlestickItem(candle_data)self.pw.addItem(candle_item_list)self.vLine = pg.InfiniteLine(angle=90, movable=False)self.hLine = pg.InfiniteLine(angle=0, movable=False)self.label = pg.TextItem()self.pw.addItem(self.vLine, ignoreBounds=True)self.pw.addItem(self.hLine, ignoreBounds=True)self.pw.addItem(self.label, ignoreBounds=True)self.vb = self.pw.getViewBox()self.proxy = pg.SignalProxy(self.pw.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)# 显示整条折线图self.pw.enableAutoRange()passdef mouseMoved(self,evt):pos = evt[0]if self.pw.sceneBoundingRect().contains(pos):mousePoint = self.vb.mapSceneToView(pos)index = int(mousePoint.x())if index >= 0 and index < len(self.y_data):x_str = self.xTick[index][1]y_str_html = ''# time, open, close, min, maxy_str_html += '<br/> 开盘:'+str(self.y_data[index][1])y_str_html += '<br/> 收盘:'+str(self.y_data[index][2])y_str_html += '<br/> 最低:'+str(self.y_data[index][3])y_str_html += '<br/> 最高:'+str(self.y_data[index][4])html_str = '<p style="color:black;font-size:18px;font-weight:bold;"> ' + x_str +' '+y_str_html+ '</p>'self.label.setHtml(html_str)self.label.setPos(mousePoint.x(), mousePoint.y())self.vLine.setPos(mousePoint.x())self.hLine.setPos(mousePoint.y())passpass
使用:
if __name__ == '__main__':app = QtWidgets.QApplication(sys.argv)xTick = [(0,'2022-01-01'),(1,'2022-01-02'),(2,'2022-01-03'),(3,'2022-01-04'),(4,'2022-01-05')]xTick_show = xTickx = [0,1,2,3,4]candle_data = [(0, 9, 15, 8, 16),(1, 10, 13, 5, 15),(2, 13, 17, 9, 20),(3, 17, 14, 11, 23),(4, 14, 15, 5, 19)]pre_data = {}pre_data['title_str'] = 'K线图'pre_data['xTick_show'] = xTick_showpre_data['xTick'] = xTickpre_data['x'] = xpre_data['candle_data'] = candle_datatemp_w = PyQtGraphWidget()temp_w.show()temp_w.set_k_data(pre_data)sys.exit(app.exec_())pass
PyQt5_pyqtgraph蜡烛图相关推荐
- PyQt5_pyqtgraph股票蜡烛图与常用均线
股票分析常用到的均线有MA5 MA10 MA20 MA30 MA60,本例中会实现在pyqtgraph中显示这些常用均线 目录 效果 代码 使用 数据 效果 代码 需要用到的包导入,与需要用到的蜡烛控 ...
- plotly基于dataframe数据绘制股票蜡烛图(Candlestick)
plotly基于dataframe数据绘制股票蜡烛图(Candlestick) # 股票蜡烛图(Candlestick) import plotly as py # 导入plotly库并命名为py i ...
- PTA 基础编程题目集 7-13 日K蜡烛图 C语言
PTA 基础编程题目集 7-13 日K蜡烛图 C语言 股票价格涨跌趋势,常用蜡烛图技术中的K线图来表示,分为按日的日K线.按周的周K线.按月的月K线等.以日K线为例,每天股票价格从开盘到收盘走完一天, ...
- python蜡烛图预测_python tushare股票K线蜡烛图绘制
序言:学着学着就学到股票图形绘制了,尝试了下,入门蛮简单的,后面就不知道了,现在好像mplfinance更换了新版本,老版本不支持了.以下代码能实现单个股票K线蜡烛图图形输出,不过我用的是tushar ...
- 蜡烛图plotly_Python数据分析:基于Plotly的动态可视化绘图简介,目录书摘
目录:第1章 快速开始 1.1 Plotly简介 1.2 安装与安装环境 1.3 在线初始化 1.4 在线绘图隐私说明 1.5 开始在线绘图 1.6 使用离线绘图库 1.7 参数解读 ...
- 7-13 日K蜡烛图
股票价格涨跌趋势,常用蜡烛图技术中的K线图来表示,分为按日的日K线.按周的周K线.按月的月K线等.以日K线为例,每天股票价格从开盘到收盘走完一天,对应一根蜡烛小图,要表示四个价格:开盘价格Open(早 ...
- python绘制k线图(蜡烛图)报错 No module named 'matplotlib.finance
使用python绘制蜡烛图报错:No module named 'matplotlib.finance 部分版本移除了finance模块,需要独立安装 安装命令:pip install git+htt ...
- Java 蜡烛图_分支-15. 日K蜡烛图
股票价格涨跌趋势,常用蜡烛图技术中的K线图来表示,分为按日的日K线.按周的周K线.按月的月K线等.以日K线为例,每天股票价格从开盘到收盘走完一天,对应一根蜡烛小图,要表示四个价格:开盘价格Open(早 ...
- 常用K线图(蜡烛图)基本概念
有关K线图的经典书籍: [美]史蒂夫·尼森(Steve Nison) <日本蜡烛图技术新解>,机械工业出版社,2018-07-17 K线图(技术分析的一种图表–k线图) - 百度百科 大盘 ...
最新文章
- host ntrip 千寻rtk_什么是千寻知寸cors账号?它提供的定位服务精度如何?使用时需要注意哪些问题?...
- [Hive_add_8] Hive 常用参数配置
- NUC970开发资源
- Eclipse 常用快捷键
- 教你移除IE 7.0浏览器的默认搜索框
- 17 张程序员壁纸(赶快挑一张吧)
- 综述:编程语言的发展趋势及未来方向
- 【渝粤教育】电大中专消费者心理学_1作业 题库
- Jeewx-Api 1.3.2 版本发布,微信开发SDK
- EJB3.0学习笔记---JMS/MDB/Pub/Sub/P2P
- CDH ecosystem components
- Python+OpenCV:色彩空间转换
- 按之字形顺序打印二叉树(C++)
- Android 中Animation简单例子
- php静态方法的问题,php 静态方法问题
- Python游戏编程快速上手
- 金蝶苍穹,报表查询插件
- 头歌Python,7号的,作业,
- 京东智能云APP可用来做什么?
- bzoj 3007 拯救小云公主