Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14360

Any examples for using patchworklib with matplotlib in a PyQT environment?

$
0
0

Hollo everyone,

Currently, I am working on a project that involves matplotlib integrated with pyqt. I intend to combine several figures using patchworklib, but I encountered an issue. Despite my efforts, only one figure appears in the GUI. Could you please assist me in identifying what might be causing this issue?

Below are the relevant sections of my code:

import sysfrom matplotlib.figure import Figurefrom PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidgetfrom matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvasimport matplotlib.pyplot as pltimport patchworklib as pwclass MainWindow(QMainWindow):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        self.setWindowTitle('Matplotlib in PyQt5 Example')        self.setGeometry(100, 100, 800, 600)        central_widget = QWidget()        self.setCentralWidget(central_widget)        # 创建 Matplotlib 的画布        self.canvas = PlotCanvas(self, width=5, height=4)        layout = QVBoxLayout(central_widget)        layout.addWidget(self.canvas)class PlotCanvas(FigureCanvas):    def __init__(self, parent=None, width=5, height=4, dpi=100):        # fig, self.ax = plt.subplots(figsize=(width, height), dpi=dpi)        # fig = Figure()        self.ax1 = pw.Brick()        self.ax2 = pw.Brick()        ax1 = self.plot(self.ax1)        ax2 = self.bar(self.ax2)        # Concatenate axes        ax12 = ax1 | ax2        fig = ax12.savefig()        super().__init__(fig)        self.setParent(parent)        self.draw()    def plot(self, ax_):        x = [1, 2, 3, 4, 5]        y = [10, 20, 25, 30, 35]        ax_.plot(x, y)        ax_.set_title('Matplotlib Plot in PyQt5')        ax_.set_xlabel('X Label')        ax_.set_ylabel('Y Label')        return ax_    def bar(self, ax_):        categories = ['A', 'B', 'C', 'D', 'E']        values = [7, 10, 15, 5, 9]        ax_.bar(categories, values, color='skyblue')        ax_.set_title('Matplotlib Bar Chart in PyQt5')        ax_.set_xlabel('Categories')        ax_.set_ylabel('Values')        return ax_if __name__ == '__main__':    app = QApplication(sys.argv)    window = MainWindow()    window.show()    sys.exit(app.exec_())

image

Your assistance in resolving this matter would be greatly appreciated.

Thank you.

Sincerely,

Dong


Viewing all articles
Browse latest Browse all 14360

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>