【问题标题】:PyQtGraph grid with linked axes带有链接轴的 PyQtGraph 网格
【发布时间】:2015-01-21 21:30:21
【问题描述】:

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

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

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

【问题讨论】:

    标签: python pyqt pyqtgraph


    【解决方案1】:

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

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

    【讨论】:

    • 效果很好,但是当使用showValues = False 时,分隔 AxisItem 和 ViewBox 的线仍然存在。有没有办法像使用hideAxis一样摆脱它?
    • 感谢...感谢其他人试图让这个工作你需要隐藏标签以及axis.showLabel(False)
    • @pev.hall 在最新版本中,您无需使用 showLabel 设置任何内容。如果你 showLabel(True) 那么标签空间将被创建但它会是空的,因此当使用 showValues=False 时 showLabel 默认为 False。
    【解决方案2】:

    所以,这是经过修改的完整代码:

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

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多