PyQt, matplotlib, Seaborn, and Titanic
======
- [Kaggle's Titanic part 2 Google Colab](https://colab.research.google.com/drive/1u9KCazgvSG9W5V__Vy90EX5HE2ZNCXot?usp=sharing)
- [Seaborn Practice](https://colab.research.google.com/drive/1ob7O3AlDsy0Y9g5n_PPOsEeMAgLj5m5z?usp=sharing)
Python Graph Gallery
---
References: https://python-graph-gallery.com
Seaborn
---
Main References:
[Seaborn](https://seaborn.pydata.org/tutorial/function_overview.html)
[Zhihu](https://zhuanlan.zhihu.com/p/40303932)
[Tryolabs](https://tryolabs.com/blog/2017/03/16/pandas-seaborn-a-guide-to-handle-visualize-data-elegantly/)
[Zhihu2](https://zhuanlan.zhihu.com/p/342945532)
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!!

**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
<br>
<br>
<br>
- **displot**: show the histogram of a column
`sns.displot( age )`
- kde
- False
- hist
- False
- bin
- 30
- **distplot**: show the histogram with KDE(kernel density estimation) of a column
`sns.distplot( age )`
Warning: not supported in the future.
- **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](https://build-system.fman.io/qt-designer-download) 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!')
```
- stop at https://medium.com/bucketing/pyside-6-基礎物件介紹-label-button-edit-e4efa9fb424d
- 2021/01/14
- Label, Button, Edit
- ComboBox, RadioButton, CheckButton, SpinBox
- ListWidget, TableWidget, TreeWidget
- DockWidget, ToolBox, StackWidget
- QColor, QcolorDialog, QPalette
- StyleSheet, QFont, QFontDialog
- QSystemTrayIcon, Frameless, Window, MouseEvent
- Qdialog, QMessageBox
- QMenu, QAction, ToolTip, StatusTip
- QTimer, QCalendar, QDateEdit
- QTimeEdit, QLCDNumber, AnalogClock
- Logging
- 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