<jupyter_start><jupyter_text>A PyQt5 Application<jupyter_code>import sys
import random

from plotnine import ggplot, geom_point, aes, geom_line, theme
import pandas as pd
import numpy as np

from PyQt5.QtWidgets import QApplication, QPushButton, QDialog, QVBoxLayout

import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure<jupyter_output><empty_output><jupyter_text>This example applicate creates a gui windows with a canvas and a plot button which when pressed draws out a plot from random data.<jupyter_code>dpi = 72
size_inches = (11, 8)                                       # size in inches (for the plot)
size_px = int(size_inches[0]*dpi), int(size_inches[1]*dpi)  # For the canvas

class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.button = QPushButton('Plot')
        self.button.clicked.connect(self.plot)
        
        self.canvas.setMinimumSize(*size_px)

        layout = QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        self.setLayout(layout)

    def plot(self):
        # generate some 'data' to plot
        n = 100
        x = np.linspace(0, 2 * np.pi, n)
        df = pd.DataFrame({
            'x': x,
            'y1': np.random.rand(n),
            'y2': np.sin(x),
            'y3': np.cos(x) * np.sin(x)
        })

        # change the dependent variable and color each time this method is called
        y = random.choice(['y1', 'y2', 'y3'])
        color = random.choice(['blue', 'red', 'green'])

        # specify the plot and get the figure object
        ff = (ggplot(df, aes('x', y))
              + geom_point(color=color)
              + geom_line()
              + theme(
                  figure_size=size_inches,
                  dpi=dpi
              )
             )
        fig = ff.draw()

        # update to the new figure
        self.canvas.figure = fig

        # refresh canvas
        self.canvas.draw()

        # close the figure so that we don't create too many figure instances
        plt.close(fig)


# To prevent random crashes when rerunning the code,
# first check if there is instance of the app before creating another.
app = QApplication.instance()
if app is None:
    app = QApplication(sys.argv)

main = Window()
main.show()
app.exec_()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09660766961651918, 'fraction_numerical': 0.008480825958702065, 'mean_word_length': 2.487146529562982, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '120934', 'license': 'MIT', 'n_tokens_mistral': 844, 'n_tokens_neox': 810, 'n_words': 231, 'path': 'demo_plot/plotnine-examples/tutorials/miscellaneous-a-pyqt5-application.ipynb', 'repo_name': 'hermanzhaozzzz/DataScienceScripts', 'size': '2712'}