具有链接轴的PyQtGraph网格

时间:2014-11-24 08:03:42

标签: python pyqt pyqtgraph

使用PyQtGraph进行简单的图形布局,其中图的x轴链接在一起,网格也显示在两个图中:

from pyqtgraph.Qt import QtGui, QtCore                                              
import pyqtgraph as pg                                                              

app = QtGui.QApplication([])                                                        
view = pg.GraphicsView()                                                            
l = pg.GraphicsLayout()                                                             
view.setCentralItem(l)                                                              
view.show()                                                                         
view.resize(800,600)                                                                

p0 = l.addPlot(0, 0)                                                                
p0.showGrid(x = True, y = True, alpha = 0.3)                                        
#p0.hideAxis('bottom')                                                              
p1 = l.addPlot(1, 0)                                                                
p1.showGrid(x = True, y = True, alpha = 0.3)                                        

p1.setXLink(p0)                                                                     

l.layout.setSpacing(0.)                                                             
l.setContentsMargins(0., 0., 0., 0.)                                                

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

enter image description here

如果我在第一个图中隐藏x轴(取消注释代码中的p0.hideAxis('bottom')行),那么轴将会消失,但网格也会消失:

enter image description here

我怎么能强迫它留在那里?由于两个x轴都连在一起,我希望这是可能的(上图中的网格可以从下图的x轴中获取)。

2 个答案:

答案 0 :(得分:4)

尝试axis.setStyle(showValues=False)而不是隐藏轴。

(这可能仅在开发分支中可用)

答案 1 :(得分:0)

因此,这是带有修改的完整代码:

#https://stackoverflow.com/questions/27100277/pyqtgraph-grid-with-linked-axes
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

app = QtGui.QApplication([])
view = pg.GraphicsView()
l = pg.GraphicsLayout()
view.setCentralItem(l)
view.show()
view.resize(800,600)

p0 = l.addPlot(0, 0)
p0.showGrid(x = True, y = True, alpha = 1.0)

#have no x-axis tickmark below the upper plot (coordinate 0,0)
#without these lines, there will be separate coordinate systems with a gap inbetween
ax0 = p0.getAxis('bottom')      #get handle to x-axis 0
ax0.setStyle(showValues=False)  #this will remove the tick labels and reduces gap b/w plots almost to zero
                                #there will be a double line separating the plot rows


p1 = l.addPlot(1, 0)
p1.showGrid(x = True, y = True, alpha = 1.0)

p1.setXLink(p0)

l.layout.setSpacing(0.)
l.setContentsMargins(0., 0., 0., 0.)

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