Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt matplotlib

import sys
import time

import numpy as np

from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qtagg import (
    FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure


class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self._main = QtWidgets.QWidget()
        self.setCentralWidget(self._main)
        layout = QtWidgets.QVBoxLayout(self._main)

        static_canvas = FigureCanvas(Figure(figsize=(5, 3)))
        # Ideally one would use self.addToolBar here, but it is slightly
        # incompatible between PyQt6 and other bindings, so we just add the
        # toolbar as a plain widget instead.
        layout.addWidget(NavigationToolbar(static_canvas, self))
        layout.addWidget(static_canvas)

        dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3)))
        layout.addWidget(dynamic_canvas)
        layout.addWidget(NavigationToolbar(dynamic_canvas, self))

        self._static_ax = static_canvas.figure.subplots()
        t = np.linspace(0, 10, 501)
        self._static_ax.plot(t, np.tan(t), ".")

        self._dynamic_ax = dynamic_canvas.figure.subplots()
        t = np.linspace(0, 10, 101)
        # Set up a Line2D.
        self._line, = self._dynamic_ax.plot(t, np.sin(t + time.time()))
        self._timer = dynamic_canvas.new_timer(50)
        self._timer.add_callback(self._update_canvas)
        self._timer.start()

    def _update_canvas(self):
        t = np.linspace(0, 10, 101)
        # Shift the sinusoid as a function of time.
        self._line.set_data(t, np.sin(t + time.time()))
        self._line.figure.canvas.draw()


if __name__ == "__main__":
    # Check whether there is already a running QApplication (e.g., if running
    # from an IDE).
    qapp = QtWidgets.QApplication.instance()
    if not qapp:
        qapp = QtWidgets.QApplication(sys.argv)

    app = ApplicationWindow()
    app.show()
    app.activateWindow()
    app.raise_()
    qapp.exec()
Comment

PREVIOUS NEXT
Code Example
Python :: find an item in a list python 
Python :: name is not defined python 
Python :: python capitalize 
Python :: python herencia 
Python :: select element using Css selector in python 
Python :: expand pandas dataframe into separate rows 
Python :: difference between this and super 
Python :: a function to create a null matrix in python 
Python :: what is the size of cover in facebook 
Python :: how to convert tensorflow 1.15 model to tflite 
Python :: python all but the last element 
Python :: zip() python 
Python :: double for loop in list comprehension 
Python :: python sleep command 
Python :: list programs in python 
Python :: pandas join dataframe 
Python :: traversal tree in python 
Python :: python foreach 2d array 
Python :: how to add pagination in discord.py 
Python :: upload file setup django url 
Python :: Create a hexadecimal colour based on a string with python 
Python :: field in django 
Python :: issubclass python example 
Python :: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names 
Python :: onehot encode list of columns pandas 
Python :: swarm plot 
Python :: how to print second largest number in python 
Python :: iterating string in python 
Python :: python rounding numbers to n digits 
Python :: pandas drop columns 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =