Third 'Y' axis not appearing when embedded in PyQt4 GraphicsView

58 views
Skip to first unread message

MudloggerMike

unread,
Sep 28, 2016, 12:04:14 AM9/28/16
to pyqtgraph

I am new to pyqtgraph. I modified a multiaxis example that I found on the Internet and placed into a PyQt4 GraphicsView widget. The example shows the third axis before I embedded into the GUI, however when I embed, the third axis doesn't show up. I have read everything that I can find with no luck. Can someone please help? Ideally, I would like for the third axis to appear just to the right of the second axis.The graph is pictured above. Here is the code:


#imports
from PyQt4 import QtGui
from PyQt4 import QtCore
import ui_test  #Gui File
import sys
import pyqtgraph as pg

class Gui(QtGui.QMainWindow, ui_test.Ui_MainWindow):       
    def __init__(self):       
        super(self.__class__, self).__init__()       
        self.setupUi(self) 
        self.pg_plot()
        self.updateViews()
        self.plotdata()
        self.pushButton_movecursor.clicked.connect(self.movecursor)
   
    def pg_plot(self):         
        ## Switch to using white background and black foreground
        self.bepx = 30
        self.bepy = 800
        pg.setConfigOption('background', 'w')
        pg.setConfigOption('foreground', 'k') 
        lr = pg.LinearRegionItem([20,40], movable = False, brush = '#ffcccc33')  #Set linear region
        self.p1 = pg.PlotItem()
       
        self.p1.addItem(lr)
        self.graphicsView.setCentralWidget(self.p1)
        self.p1.showGrid(x = True, y = True ,alpha = 0.3) 
        ## create a new ViewBox, link the right axis to its coordinate system
        self.p2 = pg.ViewBox()
       
        self.p1.showAxis('right')
        self.p1.scene().addItem(self.p2)
        self.p1.getAxis('right').linkToView(self.p2)
        self.p2.setXLink(self.p1)
        self.p1.getAxis('right').setLabel('eff', color='g')
        pg.LinearRegionItem([20,40])
       
        ## create third ViewBox.
        ## this time we need to create a new axis as well.
        self.p3 = pg.ViewBox()
        ax3 = pg.AxisItem('right')
        self.p1.layout.addItem(ax3,2,3)
        self.p1.scene().addItem(self.p3)
        ax3.linkToView(self.p3)
        self.p3.setXLink(self.p1)
        ax3.setZValue(-10000)
        ax3.setLabel('Power', color='r')   
       
        self.arrow = pg.ArrowItem(angle=90)
        self.text = pg.TextItem(html='<div style="text-align: center"><span style="color: 'b' ;">^</span><br><span style="color: #000; font-size: 16pt;">BEP</span></div>', anchor=(-0.3,1.3), border='w', fill=(255, 255, 255, 100))
        self.p3.addItem(self.text)
       
        self.p3.addItem(self.arrow)
        self.text.setPos(self.bepx - 1.2, self.bepy - 530)
        ax3.setStyle(showValues=False)   #Not needed workig on gridlines
       
    ## Handle view resizing
    def updateViews(self):
        ## view has resized; update auxiliary views to match
       
        self.p2.setGeometry(self.p1.vb.sceneBoundingRect())
        self.p3.setGeometry(self.p1.vb.sceneBoundingRect())
       
        ## need to re-update linked axes since this was called
        ## incorrectly while views had different shapes.
        ## (probably this should be handled in ViewBox.resizeEvent)
        self.p2.linkedViewChanged(self.p1.vb, self.p2.XAxis)
        self.p3.linkedViewChanged(self.p1.vb, self.p3.XAxis)
      
    def plotdata(self):
        #Plot Datapoints
        self.p1.plot([0,10,20,30,40,50], [1,2,4,8,16,32], pen = 'g')
        self.p2.addItem(pg.PlotCurveItem([0,10,20,30,40,50], [10,20,40,80,40,20], pen='b'))
        self.p3.addItem(pg.PlotCurveItem([0,10,20,30,40,50], [3200,1600,800,2500,200,100], pen='r'))
        self.bepx = 50
        self.bepy = 1000
       
    x = 1
    y = 1
    def movecursor(self):
        try:
            self.cursor.clear()
        except:
            pass
        self.x += 1
        if self.x > 50:
            self.x = 1
        self.y += 1
        if self.y > 30:
            self.y = 1
       
        self.cursor = self.p1.plot([self.x], [self.y], pen=None, symbol='+', color = 'b')
               
def main():
    app = QtGui.QApplication(sys.argv)
    form = Gui()
    form.show() 
    app.exec_()
   
if __name__ == '__main__':  # if we're running file directly and not importing it
    main()  # run the main function





Carlos Ferras

unread,
Sep 28, 2016, 4:37:14 PM9/28/16
to pyqtgraph
post the ui file to run your code exactly

MudloggerMike

unread,
Sep 28, 2016, 5:43:30 PM9/28/16
to pyqtgraph
@Carlos Here are both the UI and the compiled version.
ui_test.py
ui_test.ui

Carlos Ferras

unread,
Sep 29, 2016, 9:33:29 AM9/29/16
to pyqtgraph
well, it just works fine to me, the problem was with the background color, because for me, the background color in your code appear black, because pg.setConfigOption('background', 'w') did not work, instead, i use self.graphicsView.setBackground('w').

MudloggerMike

unread,
Sep 29, 2016, 4:45:05 PM9/29/16
to pyqtgraph
Thanks for the tip on the background. The background was loading black for the first time that I opened the app in a new kernal. Then it loaded correctly. That fixed the problem. I fixed the Axis display by changing ax3 to self.ax3. Thank you.
Reply all
Reply to author
Forward
0 new messages