【问题标题】:Set subplot height/width设置子图高度/宽度
【发布时间】:2020-11-08 17:32:47
【问题描述】:

我正在尝试创建一个包含一组子图的窗口。子图的高度必须不同:

  • 1st -- 50%
  • 第二和第三 -- 各 25%

我正在使用 rowcolrowspancolspan 参数,但没有运气。 所以,问题是:如何控制子图的大小(高度、宽度)(在创建时或/和创建后)?

Current heights

Heights needed

我当前的代码如下:

import PyQt5
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
from random import randint as ri

def BarChart(x):
    global layout
    barPlot = layout.addPlot(row=0, col=0, rowspan=2, colspan=1)
    barPlot.showGrid(x=True, y=True, alpha=0.5)
    barPlot.plot(x)
    ax = barPlot.getAxis('bottom')
    ax.setStyle(showValues=False)
    return barPlot

def IndChart(x, barPlot):
    global layout
    indPlot = layout.addPlot(row=2, col=0, rowspan=1, colspan=1)
    indPlot.showGrid(x=True, y=True, alpha=0.5)
    indPlot.plot(x)
    indPlot.setXLink(barPlot)
    ax = indPlot.getAxis('bottom')
    ax.setStyle(showValues=False)
    return indPlot

def IndChart2(x, barPlot):
    global layout
    indPlot = layout.addPlot(row=3, col=0, rowspan=1, colspan=1)
    indPlot.showGrid(x=True, y=True, alpha=0.5)
    indPlot.plot(x)
    indPlot.setXLink(barPlot)
    return indPlot

if __name__ == '__main__':
    app = QtGui.QApplication([])
    view = pg.GraphicsView()
    layout = pg.GraphicsLayout()
    layout.layout.setSpacing(0)
    layout.setContentsMargins(0, 0, 0, 0)
    view.setCentralItem(layout)
    view.show()
    x = [0] + [ri(0, 100) for i in range(100)] + [100]
    barPlot = BarChart(x)
    indPlot = IndChart(x, barPlot)
    indPlot2 = IndChart2(x, barPlot)
    x = [0] + [ri(0, 100) for i in range(100)] + [100]
    barPlot.plot(x, pen=(255, 0, 0))
    app.exec_()

【问题讨论】:

    标签: python pyqt pyqtgraph


    【解决方案1】:

    我找到了一些解决方案(也许不是最好的,但至少是这样)。 layoutheight 属性可以与绘图的setFixedHeight 属性一起使用。

    使用示例:

    indPlot.setFixedHeight(layout.height() * 0.25)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      相关资源
      最近更新 更多