I am trying to plot multiple graphs in a pyqt5 window widget. I was able to do that but the size of the figure would just be "compressed" if I add more graphs to the window. See the comparison by running below code and clicking Push for Window 1 vs Push for Window 2.
I wanted to add a scrollbar to the canvas, so that I can preserve the size of the graphs, just like putting multiple graphs in a word file and use the scrollbar to move up and down to review the graphs. How should I do that?
import sys import pandas as pd import numpy as np from PyQt5.QtWidgets import ( QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget, ) from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar import matplotlib.pyplot as plt class AnotherWindow1(QWidget): def __init__(self): super().__init__() self.figure = plt.figure(figsize=(45,15)) self.canvas = FigureCanvas(self.figure) layout = QVBoxLayout() button1 = QPushButton("Plot") button1.clicked.connect(self.plot) button2 = QPushButton("Back") button2.clicked.connect(self.close_window) layout.addWidget(button1) layout.addWidget(button2) layout.addWidget(self.canvas) self.setLayout(layout) def close_window(self): self.close() def plot(self): self.figure.clear() ax1 = self.figure.add_subplot(111) data = pd.DataFrame(np.random.rand(5)) data.plot(ax=ax1) self.canvas.draw() class AnotherWindow2(QWidget): def __init__(self): super().__init__() self.figure = plt.figure(figsize=(45,15)) self.canvas = FigureCanvas(self.figure) self.canvas2 = FigureCanvas(self.figure) self.canvas3 = FigureCanvas(self.figure) layout = QVBoxLayout() button1 = QPushButton("Plot") button1.clicked.connect(self.plot) button2 = QPushButton("Back") button2.clicked.connect(self.close_window) layout.addWidget(button1) layout.addWidget(button2) layout.addWidget(self.canvas) layout.addWidget(self.canvas2) layout.addWidget(self.canvas3) self.setLayout(layout) def close_window(self): self.close() def plot(self): self.figure.clear() ax1 = self.figure.add_subplot(111) data = pd.DataFrame(np.random.rand(5)) data.plot(ax=ax1) self.canvas.draw() ax2 = self.figure.add_subplot(111) data = pd.DataFrame(np.random.rand(5)) data.plot(ax=ax2) self.canvas2.draw() ax2 = self.figure.add_subplot(111) data = pd.DataFrame(np.random.rand(5)) data.plot(ax=ax2) self.canvas3.draw() class MainWindow(QMainWindow): def __init__(self): super().__init__() self.window1 = AnotherWindow1() self.window2 = AnotherWindow2() l = QVBoxLayout() button1 = QPushButton("Push for Window 1") button1.clicked.connect(self.show_window1) l.addWidget(button1) button2 = QPushButton("Push for Window 2") button2.clicked.connect(self.show_window2) l.addWidget(button2) w = QWidget() w.setLayout(l) self.setCentralWidget(w) def show_window1(self): self.window1.show() def show_window2(self): self.window2.show() app = QApplication(sys.argv) w = MainWindow() w.show() app.exec()