Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dongzhang0725 opened this issue Feb 10, 2024 · 0 comments

Comments

@dongzhang0725
Copy link

Dear developer,

I would like to express my gratitude for developing such a useful tool. 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 sys

from matplotlib.figure import Figure
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import patchworklib as pw

class 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant