import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("6 matplotlib graphs in 3x2 grid")
self.setGeometry(50, 50, 800, 600)
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
# Create a 3x2 grid layout
grid_layout = QGridLayout(central_widget)
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title("main")
axs[1, 0].plot(x, y**2)
axs[1, 0].set_title("shares x with main")
axs[1, 0].sharex(axs[0, 0])
axs[0, 1].plot(x + 1, y + 1)
axs[0, 1].set_title("unrelated")
axs[1, 1].plot(x + 2, y + 2)
axs[1, 1].set_title("also unrelated")
fig.tight_layout()
plt.show()
# Create 6 figures and canvases
# self.figures = [Figure() for _ in range(6)]
# self.canvases = [FigureCanvas(fig) for fig in self.figures]
# # Add the canvases to the grid layout
# for i, canvas in enumerate(self.canvases):
# row, col = i // 2, i % 2
# grid_layout.addWidget(canvas, row, col)
# # Add some content to the figures
# for i, fig in enumerate(self.figures):
# ax = fig.add_subplot(111)
# ax.plot([0, 1, 2, 3, 4], [10, 1, 20, 3, 40])
# ax.set_title("Graph {}".format(i + 1))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())