diff options
author | Sacha Schutz <sacha@labsquare.org> | 2021-01-17 19:36:08 +0100 |
---|---|---|
committer | Cristian Maureira-Fredes <Cristian.Maureira-Fredes@qt.io> | 2021-04-15 16:33:35 +0200 |
commit | cf82fcabfd5b7d4168c9088a2c3f0236231f68ab (patch) | |
tree | 4f5c1024ac87bf91edeed53c5d4b3d3d5ba3a199 /examples | |
parent | 80cb8e0a3dd905edbb1226604f3e8b3e31039728 (diff) |
Add new matplotlib example and rearrange dirs
I add a widget showing a 2D gaussian with 2 inputs to adjust mu and sigma
Pick-to: 6.0
Task-number: PYSIDE-841
Change-Id: I602b07943ebeb007332bc77c4372ef5a1db20422
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/external/matplotlib/widget3d/requirements.txt (renamed from examples/external/matplotlib/requirements.txt) | 1 | ||||
-rw-r--r-- | examples/external/matplotlib/widget3d/widget3d.py (renamed from examples/external/matplotlib/widget_3dplot.py) | 0 | ||||
-rw-r--r-- | examples/external/matplotlib/widget3d/widget3d.pyproject | 3 | ||||
-rw-r--r-- | examples/external/matplotlib/widget_gaussian/requirements.txt | 3 | ||||
-rw-r--r-- | examples/external/matplotlib/widget_gaussian/widget_gaussian.py | 112 | ||||
-rw-r--r-- | examples/external/matplotlib/widget_gaussian/widget_gaussian.pyproject | 3 |
6 files changed, 122 insertions, 0 deletions
diff --git a/examples/external/matplotlib/requirements.txt b/examples/external/matplotlib/widget3d/requirements.txt index 6ccafc3f9..db5d81e01 100644 --- a/examples/external/matplotlib/requirements.txt +++ b/examples/external/matplotlib/widget3d/requirements.txt @@ -1 +1,2 @@ matplotlib +numpy diff --git a/examples/external/matplotlib/widget_3dplot.py b/examples/external/matplotlib/widget3d/widget3d.py index 874e7e439..874e7e439 100644 --- a/examples/external/matplotlib/widget_3dplot.py +++ b/examples/external/matplotlib/widget3d/widget3d.py diff --git a/examples/external/matplotlib/widget3d/widget3d.pyproject b/examples/external/matplotlib/widget3d/widget3d.pyproject new file mode 100644 index 000000000..6b25c5be7 --- /dev/null +++ b/examples/external/matplotlib/widget3d/widget3d.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["widget3d.py"] +} diff --git a/examples/external/matplotlib/widget_gaussian/requirements.txt b/examples/external/matplotlib/widget_gaussian/requirements.txt new file mode 100644 index 000000000..26a2b438f --- /dev/null +++ b/examples/external/matplotlib/widget_gaussian/requirements.txt @@ -0,0 +1,3 @@ +matplotlib +scipy +numpy diff --git a/examples/external/matplotlib/widget_gaussian/widget_gaussian.py b/examples/external/matplotlib/widget_gaussian/widget_gaussian.py new file mode 100644 index 000000000..ba790af4e --- /dev/null +++ b/examples/external/matplotlib/widget_gaussian/widget_gaussian.py @@ -0,0 +1,112 @@ +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +import sys + +import numpy as np +from scipy.stats import norm +from matplotlib import pyplot as plt +from matplotlib.figure import Figure +from matplotlib.backends.backend_qt5agg import FigureCanvas +from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT +from PySide6.QtCore import Qt, Slot +from PySide6.QtWidgets import ( + QApplication, + QWidget, + QDoubleSpinBox, + QVBoxLayout, + QHBoxLayout, +) + + +"""This example implements the interaction between Qt Widgets and a 2D +matplotlib plot showing a gaussian curve with scipy""" + + +class PlotWidget(QWidget): + def __init__(self, parent=None): + super().__init__(parent) + + # create widgets + self.view = FigureCanvas(Figure(figsize=(5, 3))) + self.axes = self.view.figure.subplots() + self.toolbar = NavigationToolbar2QT(self.view, self) + self.mu_input = QDoubleSpinBox() + self.std_input = QDoubleSpinBox() + self.mu_input.setPrefix("μ: ") + self.std_input.setPrefix("σ: ") + self.std_input.setValue(10) + + # Create layout + input_layout = QHBoxLayout() + input_layout.addWidget(self.mu_input) + input_layout.addWidget(self.std_input) + vlayout = QVBoxLayout() + vlayout.addWidget(self.toolbar) + vlayout.addWidget(self.view) + vlayout.addLayout(input_layout) + self.setLayout(vlayout) + + # connect inputs with on_change method + self.mu_input.valueChanged.connect(self.on_change) + self.std_input.valueChanged.connect(self.on_change) + + self.on_change() + + @Slot() + def on_change(self): + """ Update the plot with the current input values """ + mu = self.mu_input.value() + std = self.std_input.value() + + x = np.linspace(-100, 100) + y = norm.pdf(x, mu, std) + + self.axes.clear() + self.axes.plot(x, y) + self.view.draw() + + +if __name__ == "__main__": + + app = QApplication(sys.argv) + w = PlotWidget() + w.show() + sys.exit(app.exec_()) diff --git a/examples/external/matplotlib/widget_gaussian/widget_gaussian.pyproject b/examples/external/matplotlib/widget_gaussian/widget_gaussian.pyproject new file mode 100644 index 000000000..72c5adc78 --- /dev/null +++ b/examples/external/matplotlib/widget_gaussian/widget_gaussian.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["widget_gaussian.py"] +} |