import sys
from random import randrange
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from threading import Thread
from PyQt5 import QtTest
xdata = []
ydata = []
def background_task():
while True:
global xdata
global ydata
cp = randrange(10)
ct = randrange(10)
if len(xdata) > 21:
xdata = xdata[-21:]
ydata = ydata[-21:]
else:
pass
xdata.append(str(cp))
ydata.append(str(ct))
QtTest.QTest.qWait(1000)
bg = Thread(target=background_task, daemon = True)
bg.start()
class MplCanvas(FigureCanvas):
def __init__(self, parent=None, width=7, height=3, dpi=80):
fig = plt.figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setFixedSize(600, 400)
self.canvas = MplCanvas(self, dpi=80)
self.
canvas.
setFixedSize(QSize(600,
250)) self.update_plot()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.update_plot)
self.timer.start()
self.setCentralWidget(central_widget)
layout.addWidget(self.canvas)
def update_plot(self):
self.canvas.axes.cla()
self.canvas.axes.plot(xdata, ydata, 'r')
plt.xticks(rotation = 45)
plt.tight_layout()
every_nth = 3
for n, label in enumerate(plt.gca().xaxis.get_ticklabels()):
if n % every_nth != 0:
label.set_visible(False)
plt.tight_layout()
self.canvas.draw()
window = MainWindow()
window.show()
sys.exit(app.exec_())
import sys
from random import randrange
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from threading import Thread
from PyQt5 import QtTest
xdata = []
ydata = []
def background_task():
while True:
global xdata
global ydata
cp = randrange(10)
ct = randrange(10)
if len(xdata) > 21:
xdata = xdata[-21:]
ydata = ydata[-21:]
else:
pass
xdata.append(str(cp))
ydata.append(str(ct))
QtTest.QTest.qWait(1000)
bg = Thread(target=background_task, daemon = True)
bg.start()
class MplCanvas(FigureCanvas):
def __init__(self, parent=None, width=7, height=3, dpi=80):
fig = plt.figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setFixedSize(600, 400)
self.canvas = MplCanvas(self, dpi=80)
self.canvas.setFixedSize(QSize(600, 250))
self.update_plot()
self.timer = QtCore.QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.update_plot)
self.timer.start()
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.addWidget(self.canvas)
def update_plot(self):
self.canvas.axes.cla()
self.canvas.axes.plot(xdata, ydata, 'r')
plt.xticks(rotation = 45)
plt.tight_layout()
every_nth = 3
for n, label in enumerate(plt.gca().xaxis.get_ticklabels()):
if n % every_nth != 0:
label.set_visible(False)
plt.tight_layout()
self.canvas.draw()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks