1

I want to get data from 2 plot at the same time, so I need to sync 2 plot x-axis. However, it was a low tolerance between 2 plot x-axis. I try to use setLimits and setXRange to improve, but it seems no help on it. Am I use it right?

Since the code is too long, the following is a part of code:

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

Time string axis:

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 graph:

    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)

my result: enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
shadow dk
  • 475
  • 1
  • 4
  • 15
  • I recommend publishing a united code, it helps us not waste time in patching code. Make our lives easy please. – eyllanesc May 27 '18 at 06:00
  • Finally, I use `setXLink` to link 2 axis. Reference to [https://stackoverflow.com/questions/44612914/pyqtgraph-align-the-ticks-of-the-x-axes-in-different-plots?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa](https://stackoverflow.com/questions/44612914/pyqtgraph-align-the-ticks-of-the-x-axes-in-different-plots?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – shadow dk May 27 '18 at 10:19

0 Answers0