1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| import sys
from random import randrange
import matplotlib
import time
matplotlib.use("Qt5Agg")
from PyQt5 import QtCore
from PyQt5.QtCore import (QLineF, QPointF, QRectF, Qt, QTimer)
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import *
from matplotlib import pylab
import matplotlib.pyplot as plt
class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
a=2
b=0
c=10
refresh_graph=200
refresh_graphb=(refresh_graph*-1)-1
listex=[0,1]
listey=[randrange(1,c),randrange(1,c)]
def __init__(self, parent=None, width=2, height=2, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
self.axes.hold(True)
super(MyMplCanvas, self).__init__(fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,QSizePolicy.Expanding,QSizePolicy.Expanding)
FigureCanvas.setFixedSize(self,800,500)
FigureCanvas.updateGeometry(self)
timer = QTimer(self)
self.setWindowTitle("title1")
timer.timeout.connect(self.update_figure)
self.tstart=time.time()
timer.start(2)
def update_figure(self):
pass
class MyStaticMplCanvas(MyMplCanvas):
def update_figure(self):
self.a=self.a+1
self.b=time.time()-self.tstart
self.listex.extend([self.a])
self.listey.extend([randrange(1,self.a)])
print (time.time())
if (self.a % self.refresh_graph)==0:
self.axes.plot(self.listex[self.refresh_graphb:], self.listey[self.refresh_graphb:],color="blue", linewidth=0.5, linestyle="-")
self.axes.axis([0, self.a, 0, max(self.listey)])
self.draw()
def save(self):
print("ok")
matplotlib.pylab.savefig('chaussette.png')
print("ok1")
plt.savefig('chaussette2.png')
print("ok2")
self.main_widget.savefig('chaussette2.png')
print("ok3")
class ApplicationWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("application main window")
self.main_widget = QWidget(self)
l = QVBoxLayout(self.main_widget)
sc = MyStaticMplCanvas(self.main_widget, width=2, height=2, dpi=100)
self.bouton=QPushButton("SAVE")
l.addWidget(sc)
l.addWidget(self.bouton)
self.setCentralWidget(self.main_widget)
self.bouton.clicked.connect(MyStaticMplCanvas.save)
def fileQuit(self):
self.close()
def closeEvent(self, ce):
self.fileQuit()
def about(self):
QMessageBox.about(self, "About",)
if __name__ == '__main__':
app = QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("PyQt5 Matplot Example")
aw.show()
app.exec_() |