Image in pyqtgraph is hidden due to zoom

103 views
Skip to first unread message

db30...@gmail.com

unread,
Apr 28, 2019, 2:53:40 PM4/28/19
to pyqtgraph

I use pyqtgraph to draw the candles, but if I zoom graph very much, image hide.

I noticed that when zoom very much, “paint” method stops being called. Code with http request to exchange below.

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_()

I need to display very small values, for example, 0.0000001-0.00000015. What can I do to prevent image hidden at high zoom?

graph.jpg

Patrick

unread,
Apr 29, 2019, 12:55:49 AM4/29/19
to pyqtgraph
Hi,

I don't know exactly why that would be happening, but seems like a rounding error somewhere. Your bounding box function is returning a y-axis range of 0 to 1 (which I assume is from the minimum size of the picture object maybe?) even though the data is a million times smaller than that.

If I change the bounding box method to:
    # 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)
then it seems to work better.

Also needed to change this for python3:
request_data = json.loads(data.decode('utf8'))

Patrick

db30...@gmail.com

unread,
Apr 29, 2019, 1:35:45 AM4/29/19
to pyqtgraph
It works, thank you so much!

понедельник, 29 апреля 2019 г., 7:55:49 UTC+3 пользователь Patrick написал:
Reply all
Reply to author
Forward
0 new messages