如何在 QtCharts 中添加子图?

How to add subplots in QtCharts?

我需要创建两个图(子图,同步图)并按如下方式设置维度:

类似于这张草图。

一个图表很容易创建(下面的代码)。但是要添加第二个子图 - 它不起作用。我尝试通过 QVBoxLayout() 添加它,但也失败了。

我找到了一个需要的例子, but it is not written in Python (which causes trouble when trying to translate to Python). Here https://doc.qt.io/qt-5/qchart.html#chartType-prop也没有,Python也没有。

如何添加子图并指示大小(以像素或%为单位)?

from random import uniform
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from PyQt5.QtChart import QChart, QChartView, QLineSeries



class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 100, 680, 500)
        self.create_linechart()
        self.show()

    def create_linechart(self):

        series = QLineSeries(self)
        for i in range(100):
            series.append(i, uniform(0, 10))

        chart = QChart()
        chart.addSeries(series)
        chart.createDefaultAxes()
        chartview = QChartView(chart)
        self.setCentralWidget(chartview)


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())

您必须创建 2 个 QChartView,因为在您的代码中您只创建并将其设置为 centralwidget,那么您必须使用 QWidget 作为容器并使用 QVBoxLayout 添加它们,对于高度比,您必须设置一个拉伸系数:

from random import uniform
import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtChart import QChart, QChartView, QLineSeries


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 100, 680, 500)
        view1 = self.create_linechart()
        view2 = self.create_linechart()

        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        lay = QVBoxLayout(central_widget)
        lay.addWidget(view1, stretch=3)
        lay.addWidget(view2, stretch=1)

    def create_linechart(self):
        series = QLineSeries()
        for i in range(100):
            series.append(i, uniform(0, 10))

        chart = QChart()
        chart.addSeries(series)
        chart.createDefaultAxes()
        chartview = QChartView(chart)
        return chartview


if __name__ == "__main__":
    App = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(App.exec_())