如何控制pyqtgraph子图的高度与宽度?实现自定义尺寸分配
Hey there! Let's fix that subplot height issue you're having with pyqtgraph. The problem with using rowspan alone is that it just spans multiple grid rows, but if those rows aren't sized proportionally, you won't get the exact 50%/25%/25% split you want.
Instead, pyqtgraph's GraphicsLayout has a handy method called setRowHeight() that lets you define relative heights for each row directly. Here's how to adjust your code to get the exact layout you need:
First, we'll set the row heights right after creating the layout. Then we'll tweak the addPlot() calls to use single rows instead of spanning them, since the row heights themselves will handle the proportion.
Here's the modified code with key changes highlighted:
import PyQt5 from pyqtgraph.Qt import QtGui, QtCore import pyqtgraph as pg from random import randint as ri def BarChart(x): global layout # Now just use row 0, no need for rowspan since we set the row height to 50% barPlot = layout.addPlot(row=0, col=0) 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 # Use row 1, which we set to 25% height indPlot = layout.addPlot(row=1, col=0) 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 # Use row 2, also 25% height indPlot = layout.addPlot(row=2, col=0) 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) # This is the key part! Set relative row heights # Values are proportional; sum can be 1.0 for exact percentages layout.setRowHeight(0, 0.5) # First row = 50% of total height layout.setRowHeight(1, 0.25) # Second row = 25% layout.setRowHeight(2, 0.25) # Third row = 25% 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_()
Key Notes:
setRowHeight(): This method takes a row index and a relative height value. You can use values that add up to 1.0 for exact percentage splits, or any positive numbers (they'll be normalized to proportions automatically).- No more
rowspan: Since we're defining the row heights directly, each subplot only needs to occupy one row. This makes the code cleaner and more predictable. - X-axis linking: Your
setXLink()calls are perfect—they keep all subplots aligned on the x-axis, which is great for comparing data across plots.
If you ever need to adjust widths (for columns instead of rows), you can use setColumnWidth() the same way!
内容的提问来源于stack exchange,提问作者ENIAC

