Try โ€‚โ€‰HackMD

PyQt, matplotlib, Seaborn, and Titanic

References: https://python-graph-gallery.com

Seaborn

Main References:
Seaborn
Zhihu
Tryolabs
Zhihu2

Colab Reference: https://colab.research.google.com/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/04.14-Visualization-With-Seaborn.ipynb

Support References:
https://yanwei-liu.medium.com/python-่ณ‡ๆ–™่ฆ–่ฆบๅŒ–็ญ†่จ˜-ไบŒ-ไฝฟ็”จseaborn็นชๅœ–-3adb03407a9

Installation( import )
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

  • sns.set( style="ticks" )
    • style
      • darkgrid
      • whitegrid
      • dark
      • white
      • ticks

Let's Start!!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More โ†’

Conclusion:

  • Single Var:
    • displot
    • kdeplot
    • rugplot
  • Multi Var:
    • jointplot
    • pairplot
    • kdeplot
  • Relation:
    • relplot
    • scatterplot
    • lineplot
  • Linear Regression
    • regplot
    • resiplot
    • lmplot
  • Matric
    • heatplot
    • clustermap
  • Discrete
    • stripplot
    • swarmplot
  • Range
    • boxplot
    • boxenplot
    • violinplot
  • Statistic
    • barplot
    • pointplot
    • countplot
  • catplot
    • catplot



  • jointplot
    sns.jointplot(x="x", y="y", data=train)

    • kind
      • kde
  • pairplot
    sns.pairplot( train )

  • relplot
    sns.relplot( x="x", y="y", data=train )

    • hue
      • z
    • style
      • z
    • kind
      • line
    • sort
      • False
  • regplot
    sns.regplot( x="x", y="y", data=train

    • ci
      • None
    • order
      • 2
    • robust : remove crazy data
      • True
    • kind
      • scatter
  • catplot
    sns.catplot( x="x", y="y", data=train)

    • jitter
      • False
    • kind
      • swarm
      • box
      • violin
        • split=True
      • bar
      • count
      • point
    • hue
      • z
  • histplot
    sns.histplot( x="x", hue="z", multiple="stack")

    • hue
      • z
    • multiple
      • stack
  • kedplot
    sns.kdeplot( x="x", hue="z", multiple="stack")

    • hue
      • z
    • multiple
      • stack
  • rugplot
    sns.rugplot( x )

    • height
      • 0.3
  • FacetGrid

    โ€‹โ€‹โ€‹โ€‹g = sns.FacetGrid( train, row = 'survived', col = 'class')
    โ€‹โ€‹โ€‹โ€‹g.map( sns.distplot, "age")
    โ€‹โ€‹โ€‹โ€‹plt.show()
    
  • heatmap

Matlibplot:

  • imshow

    โ€‹โ€‹โ€‹โ€‹import matplotlib.pyplot as plt
    โ€‹โ€‹โ€‹โ€‹plt.imshow(Sxx)
    
  • size:

    • plt.figure(figsize=(15, 5))
  • Invert_x/y axis

    • plt.gca().invert_xaxis()
    • plt.gca().invert_yaxis()

PyQt

It's just like a C# window program for Python( linux/mac ) ?!

References: https://medium.com/bucketing/weekday-1-็•ถqt-ๅญธๆœƒpy-9472af78ccce

  • Installation:
  1. check python3 version & install PySide2
    python3 -V
    pip3 install PySide2
  2. Download QtDesigner and Install it.
  • Introduction

    • Widget: Each component is called "Widget"

      • Button
      • Label
      • ComboBox
    • GUI Rendering

      • ui file:
        • QUILoader

          • Import during runtime
        • pyside2-uic

          • Compile to python code, and import it.
            pyside2-uic mainwindow.ui > ui_mainwindow.py

          ex.

          • QtCreator
          • QtDesigner
      • Qt LIB
        • Coding ^^
    • Layout

      • VBox / HBox
      • Grid
      • form
    • Slot
      from PySide2 import QtCore

      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹@QtCore.Slot()
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹def exit(self):
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    self.close()
      
      • connect
        sleep_btn.clicked.connect(self.exit)
    • Signal
      from PySide2.QtCore import Signal

      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹class MainWindow(QMainWindow):
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    hello_signal = Signal(str)
      
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹@QtCore.Slot(str)
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹def say_hello(self, msg):
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    print('Hello ' + msg)
      
      • activate
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹self.hello_signal.connect(self.say_hello)
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹self.hello_signal.emit('Pyside2!')
      
    • Hello World!

    โ€‹โ€‹โ€‹โ€‹#!venv/bin/python3
    
    โ€‹โ€‹โ€‹โ€‹import sys
    
    โ€‹โ€‹โ€‹โ€‹from PySide2 import QtWidgets
    
    โ€‹โ€‹โ€‹โ€‹if '__main__' == __name__:
    โ€‹โ€‹โ€‹โ€‹    app = QtWidgets.QApplication(sys.argv)
    โ€‹โ€‹โ€‹โ€‹    w = QtWidgets.QMainWindow()
    โ€‹โ€‹โ€‹โ€‹    w.show()
    
    โ€‹โ€‹โ€‹โ€‹    ret = app.exec_()
    โ€‹โ€‹โ€‹โ€‹    sys.exit(ret)
    
    • Advanced Hello World!
    โ€‹โ€‹โ€‹โ€‹#!venv/bin/python3
    
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtWidgets import QLabel
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtWidgets import QMainWindow
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtWidgets import QPushButton
    
    
    โ€‹โ€‹โ€‹โ€‹class MainWindow(QMainWindow):
    โ€‹โ€‹โ€‹โ€‹    def __init__(self, parent=None):
    โ€‹โ€‹โ€‹โ€‹        """Main window, holding all user interface including.
    
    โ€‹โ€‹โ€‹โ€‹    Args:
    โ€‹โ€‹โ€‹โ€‹      parent: parent class of main window
    โ€‹โ€‹โ€‹โ€‹    Returns:
    โ€‹โ€‹โ€‹โ€‹      None
    โ€‹โ€‹โ€‹โ€‹    Raises:
    โ€‹โ€‹โ€‹โ€‹      None
    โ€‹โ€‹โ€‹โ€‹    """
    โ€‹โ€‹โ€‹โ€‹    super(MainWindow, self).__init__(parent)
    โ€‹โ€‹โ€‹โ€‹    self._width = 800
    โ€‹โ€‹โ€‹โ€‹    self._height = 600
    โ€‹โ€‹โ€‹โ€‹    self._title = QLabel('PySide2 is Great', self)
    โ€‹โ€‹โ€‹โ€‹    self._exit_btn = QPushButton('Exit', self)
    
    โ€‹โ€‹โ€‹โ€‹    self.setMinimumSize(self._width, self._height)
    
    • pyside2-uic ex.
    โ€‹โ€‹โ€‹โ€‹import sys
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtWidgets import QApplication, QMainWindow
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtCore import QFile
    โ€‹โ€‹โ€‹โ€‹from ui_mainwindow import Ui_MainWindow
    
    โ€‹โ€‹โ€‹โ€‹class MainWindow(QMainWindow):
    โ€‹โ€‹โ€‹โ€‹    def __init__(self):
    โ€‹โ€‹โ€‹โ€‹        super(MainWindow, self).__init__()
    โ€‹โ€‹โ€‹โ€‹        self.ui = Ui_MainWindow()
    โ€‹โ€‹โ€‹โ€‹        self.ui.setupUi(self)
    
    โ€‹โ€‹โ€‹โ€‹if __name__ == "__main__":
    โ€‹โ€‹โ€‹โ€‹    app = QApplication(sys.argv)
    
    โ€‹โ€‹โ€‹โ€‹    window = MainWindow()
    โ€‹โ€‹โ€‹โ€‹    window.show()
    
    โ€‹โ€‹โ€‹โ€‹    sys.exit(app.exec_())
    
    • QUI Loader
    โ€‹โ€‹โ€‹โ€‹import sys
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtUiTools import QUiLoader
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtWidgets import QApplication
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtCore import QFile
    
    โ€‹โ€‹โ€‹โ€‹if __name__ == "__main__":
    โ€‹โ€‹โ€‹โ€‹    app = QApplication(sys.argv)
    
    โ€‹โ€‹โ€‹โ€‹    ui_file_name = "mainwindow.ui"
    โ€‹โ€‹โ€‹โ€‹    ui_file = QFile(ui_file_name)
    โ€‹โ€‹โ€‹โ€‹    if not ui_file.open(QIODevice.ReadOnly):
    โ€‹โ€‹โ€‹โ€‹        print("Cannot open {}: {}".format(ui_file_name, ui_file.errorString()))
    โ€‹โ€‹โ€‹โ€‹        sys.exit(-1)
    โ€‹โ€‹โ€‹โ€‹    loader = QUiLoader()
    โ€‹โ€‹โ€‹โ€‹    window = loader.load(ui_file)
    โ€‹โ€‹โ€‹โ€‹    ui_file.close()
    โ€‹โ€‹โ€‹โ€‹    if not window:
    โ€‹โ€‹โ€‹โ€‹        print(loader.errorString())
    โ€‹โ€‹โ€‹โ€‹        sys.exit(-1)
    โ€‹โ€‹โ€‹โ€‹    window.show()
    
    โ€‹โ€‹โ€‹โ€‹    sys.exit(app.exec_())
    

    VBox/HBox Layout

    โ€‹โ€‹โ€‹โ€‹#!venv/bin/python3
    
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtWidgets import QWidget
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtWidgets import QMainWindow
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtWidgets import QPushButton
    โ€‹โ€‹โ€‹โ€‹from PySide2.QtWidgets import QVBoxLayout, QHBoxLayout, QGridLayout, QFormLayout
    
    
    โ€‹โ€‹โ€‹โ€‹class MainWindow(QMainWindow):
    โ€‹โ€‹โ€‹โ€‹    def __init__(self, parent=None):
    โ€‹โ€‹โ€‹โ€‹        """Main window, holding all user interface including.
    โ€‹โ€‹โ€‹โ€‹        Args:
    โ€‹โ€‹โ€‹โ€‹          parent: parent class of main window
    โ€‹โ€‹โ€‹โ€‹        Returns:
    โ€‹โ€‹โ€‹โ€‹          None
    โ€‹โ€‹โ€‹โ€‹        Raises:
    โ€‹โ€‹โ€‹โ€‹          None
    โ€‹โ€‹โ€‹โ€‹        """
    โ€‹โ€‹โ€‹โ€‹        super(MainWindow, self).__init__(parent)
    โ€‹โ€‹โ€‹โ€‹        self._widget = QWidget()
    โ€‹โ€‹โ€‹โ€‹        self._width = 400
    โ€‹โ€‹โ€‹โ€‹        self._height = 300
    โ€‹โ€‹โ€‹โ€‹        self.setFixedSize(self._width, self._height)
    
    โ€‹โ€‹โ€‹โ€‹        g_layout = QGridLayout()
    โ€‹โ€‹โ€‹โ€‹        v_layout = self.build_v_layout()
    โ€‹โ€‹โ€‹โ€‹        h_layout = self.build_h_layout()
    
    โ€‹โ€‹โ€‹โ€‹        g_layout.addItem(v_layout, 0, 0, 2, 1)
    โ€‹โ€‹โ€‹โ€‹        g_layout.addItem(h_layout, 0, 1, 1, 1)
    
    โ€‹โ€‹โ€‹โ€‹        self._widget.setLayout(g_layout)
    
    โ€‹โ€‹โ€‹โ€‹        self.setCentralWidget(self._widget)
    
    โ€‹โ€‹โ€‹โ€‹    @staticmethod
    โ€‹โ€‹โ€‹โ€‹    def build_v_layout():
    โ€‹โ€‹โ€‹โ€‹        hello_btn = QPushButton('Hello')
    โ€‹โ€‹โ€‹โ€‹        work_btn = QPushButton('Working')
    โ€‹โ€‹โ€‹โ€‹        play_btn = QPushButton('Playing')
    โ€‹โ€‹โ€‹โ€‹        sleep_btn = QPushButton('Sleeping')
    
    โ€‹โ€‹โ€‹โ€‹        v_layout = QVBoxLayout()
    โ€‹โ€‹โ€‹โ€‹        v_layout.addWidget(hello_btn)
    โ€‹โ€‹โ€‹โ€‹        v_layout.addWidget(work_btn)
    โ€‹โ€‹โ€‹โ€‹        v_layout.addWidget(play_btn)
    โ€‹โ€‹โ€‹โ€‹        v_layout.addWidget(sleep_btn)
    
    โ€‹โ€‹โ€‹โ€‹        return v_layout
    
    โ€‹โ€‹โ€‹โ€‹    @staticmethod
    โ€‹โ€‹โ€‹โ€‹    def build_h_layout():
    โ€‹โ€‹โ€‹โ€‹        hello_btn = QPushButton('Hello')
    โ€‹โ€‹โ€‹โ€‹        work_btn = QPushButton('Working')
    โ€‹โ€‹โ€‹โ€‹        play_btn = QPushButton('Playing')
    โ€‹โ€‹โ€‹โ€‹        sleep_btn = QPushButton('Sleeping')
    
    โ€‹โ€‹โ€‹โ€‹        h_layout = QHBoxLayout()
    โ€‹โ€‹โ€‹โ€‹        h_layout.addWidget(hello_btn)
    โ€‹โ€‹โ€‹โ€‹        h_layout.addWidget(work_btn)
    โ€‹โ€‹โ€‹โ€‹        h_layout.addWidget(play_btn)
    โ€‹โ€‹โ€‹โ€‹        h_layout.addWidget(sleep_btn)
    
    โ€‹โ€‹โ€‹โ€‹        return h_layout
    
  • Questions?

    • What's the difference between PyQt and Matplotlib?
      • Just like "C# window" and "Matlab" ?!
    • What's the difference between Matplotlib and Seaborn ?
      • Seaborn is build on top of Matplotlib
      • Seaborn is much prettier