Last active
December 17, 2015 01:59
-
-
Save bechu/5532600 to your computer and use it in GitHub Desktop.
python matplotlib graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, os, random | |
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
import matplotlib | |
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas | |
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar | |
from matplotlib.figure import Figure | |
import subprocess | |
import re | |
class AppForm(QMainWindow): | |
def __init__(self, parent=None): | |
QMainWindow.__init__(self, parent) | |
self.data = [] | |
self.setWindowTitle('Ping google result') | |
self.create_main_frame() | |
self.on_draw() | |
self.timer = QTimer() | |
self.timer.timeout.connect(self.ping) | |
self.timer.start(1000) | |
def on_draw(self): | |
self.axes.clear() | |
self.axes.grid(True) | |
self.axes.plot(self.data) | |
self.canvas.draw() | |
def create_main_frame(self): | |
self.main_frame = QWidget() | |
self.dpi = 100 | |
self.fig = Figure((5.0, 4.0), dpi=self.dpi) | |
self.canvas = FigureCanvas(self.fig) | |
self.canvas.setParent(self.main_frame) | |
self.axes = self.fig.add_subplot(111) | |
self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) | |
vbox = QVBoxLayout() | |
vbox.addWidget(self.canvas) | |
vbox.addWidget(self.mpl_toolbar) | |
self.main_frame.setLayout(vbox) | |
self.setCentralWidget(self.main_frame) | |
def ping(self): | |
host = "www.google.com" | |
ping = subprocess.Popen( | |
["ping", "-c", "1", host], | |
stdout = subprocess.PIPE, | |
stderr = subprocess.PIPE | |
) | |
out, error = ping.communicate() | |
m = re.search('time=(.*)ms', out) | |
self.data.append(float(m.group(1))) | |
self.on_draw() | |
def main(): | |
app = QApplication(sys.argv) | |
form = AppForm() | |
form.show() | |
app.exec_() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment