Как синхронизировать ось X в 2-м графике в pqytgraph - PullRequest
0 голосов
/ 27 мая 2018

Я хочу получать данные с 2 графиков одновременно, поэтому мне нужно синхронизировать 2 графиков по оси абсцисс.Тем не менее, это был низкий допуск между двумя участками оси абсцисс.Я пытаюсь использовать setLimits и setXRange для улучшения, но это не помогает.Я правильно его использую?

Поскольку код слишком длинный, в код входит следующее:

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('k'))
            # w = (self.data[1][0] - self.data[0][0]) / 3.
            w = 1 / 3.

            index = 0
            for (t, close, max, min, open) in self.data:
                if open > close:
                    p.setBrush(pg.mkBrush('r'))
                    p.setPen(pg.mkPen('r'))
                else:
                    p.setBrush(pg.mkBrush('g'))
                    p.setPen(pg.mkPen('g'))    

                p.drawLine(QtCore.QPointF(index, min), QtCore.QPointF(index, max))
                p.drawRect(QtCore.QRectF(index-w, open, w*2, close-open))

                index = index + 1

            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 TimeAxisItem(pg.AxisItem):
    def __init__(self, xdict, *args, **kwargs):
            pg.AxisItem.__init__(self, *args, **kwargs)
            # self.x_values = np.asarray(xdict.keys())
            self.x_values = list(xdict.keys())
            self.x_strings = list(xdict.values())

    def tickStrings(self, values, scale, spacing):
            strings = []
            for v in values:
                # vs is the original tick value
                vs = int(v * scale)
                # if we have vs in our values, show the string
                # otherwise show nothing
                if vs in self.x_values:
                    vstr = datetime.fromtimestamp(self.x_strings[vs]).strftime("%Y-%m-%d")
                else:
                    vstr = ""
                strings.append(vstr)

            return strings

plotграфик:

    date_axis = TimeAxisItem(self.xdict, orientation='bottom')
    self.plot1 = pg.PlotWidget(axisItems = {'bottom': date_axis})
    self.plot1.setXRange(-1, 50)
    self.plot1.setLimits(xMin=-100, xMax=1100)

    item = CandlestickItem(data)    #plot candel stick
    self.plot1.addItem(item)

    date_axis = TimeAxisItem(self.xdict, orientation='bottom')
    self.plot2 = pg.PlotWidget(axisItems = {'bottom': date_axis})
    self.plot2.setXRange(-1, 50)
    self.plot2.setLimits(xMin=-100, xMax=1100)

    self.plot2.plot(list(self.xdict.keys()), y)

мой результат: enter image description here

...