pyqtgraph中的图像由于缩放而被隐藏

时间:2019-04-28 13:48:46

标签: python pyqt5 pyqtgraph

我使用pyqtgraph绘制蜡烛,但是如果我非常放大图形,则会隐藏图像。

我注意到当非常放大时,“ paint”方法停止调用。带有http请求的代码可在下面进行交换。

图片示例

enter image description here

import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui
from PyQt5 import QtWidgets

class CandlestickItem(QtWidgets.QGraphicsRectItem):
    def __init__(self, data):
        super(CandlestickItem, self).__init__()
        self.data = data
        self.generatePicture()

    def generatePicture(self):
        self.picture = QtGui.QPicture()
        p = QtGui.QPainter(self.picture)
        p.setPen(pg.mkPen('w'))
        w = (self.data[1][0] - self.data[0][0]) / 3.
        for (t, open, close, min, max) in self.data:
            if max != min:
                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):
        print('paint call')
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())


if __name__ == '__main__':
    import sys
    import urllib.request
    import json

    get_symbols_url = 'https://api.hitbtc.com/api/2/public/candles/BCNBTC?period=M30&limit=1000'
    response = urllib.request.urlopen(get_symbols_url)
    request_data = json.loads(response.read())
    data_list = []
    for i in range(0, len(request_data)):
        data_list.append((float(i), float(request_data[i]['open']), float(request_data[i]['close']),
                          float(request_data[i]['min']), float(request_data[i]['max'])))

    item = CandlestickItem(data_list)
    plt = pg.plot()
    plt.addItem(item)

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

我需要显示非常小的值,例如0.0000001-0.00000015。如何防止高倍放大隐藏图像?

1 个答案:

答案 0 :(得分:0)

在支持论坛上回答了我,它对我有用。 question on forum

这似乎是一个舍入错误。需要替换boundingRect的方法。

    # import numpy as np
    def boundingRect(self):
        data = np.array(self.data)
        xmin = data[:,0].min()
        xmax = data[:,0].max()
        ymin = data[:,1:].min()
        ymax = data[:,1:].max()
        return QtCore.QRectF(xmin, ymin, xmax-xmin, ymax-ymin)