Dear all

could some expert can help me.
I have modify from one demo. but i do not how to change the x_lable to time
like H:M:S, and can move it.  i have try some way, but failed.

hope some expert can do me a favor.
thanks a lot

######################
# coding=utf-8
import os
import pprint
import random, time
import sys
from PyQt4 import QtGui, QtCore

from threading import *
import time
import datetime

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import \
    FigureCanvasQTAgg as FigCanvas, \
    NavigationToolbar2QT as NavigationToolbar
import numpy as np
import pylab


class DataGen(object):
    """ A silly class that generates pseudo-random data for
        display in the plot.
    """
    def __init__(self, init=50):
        self.data = self.init = init

    def next(self):
        self._recalc_data()
        return self.data

    def _recalc_data(self):
        delta = random.uniform(-0.5, 0.5)
        r = random.random()

        if r > 0.9:
            self.data += delta * 15
        elif r > 0.8:
            # attraction to the initial value
            delta += (0.5 if self.init > self.data else -0.5)
            self.data += delta
        else:
            self.data += delta

class myThing():
    class myThread(Thread):
        def __init__(self):
            Thread.__init__(self)
            self.running = True
            self.vec = [0]
            self.dg = DataGen()

            print "Initializing myThread..."

        def run(self):
            print "Running myThread..."
            while self.running:
                time.sleep(1)
                self.vec.append(self.dg.next())
                print "Splat"

        def getVec(self):
            return self.vec

        def stop(self):
            self.running = False

    def __init__(self):
        self.theThread = self.myThread()
        self.threadRunning = True
        print "initializing myThing..."
        self.theThread.start()

    def __del__(self):
        self.theThread.stop()

    def getVec(self):
        #print self.theThread.vec[:]
        return self.theThread.vec[:]


class ApplicationWindow(QtGui.QMainWindow):
    """ The main window of the application
    """

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle('Demo: dynamic matplotlib graph')

        self.thing1 = myThing()
        self.thing2 = myThing()
        self.starttime =  int(time.time())


        self.create_menu()
        #self.create_status_bar()
        self.create_main_panel()

        self.redraw_timer = QtCore.QTimer(self)
        QtCore.QObject.connect(self.redraw_timer,
QtCore.SIGNAL("timeout()"), self.on_redraw_timer)
        self.redraw_timer.start(4000)


    def create_menu(self):
        menu_file = QtGui.QMenu("&File", self)
        #menu_file.addAction(u'&Save plot', self.on_save_plot,
        #                         QtCore.Qt.CTRL + QtCore.Qt.Key_S)
        menu_file.addSeparator()
        menu_file.addAction(u'E&xit', self.on_exit,
                                 QtCore.Qt.CTRL + QtCore.Qt.Key_X)

        self.menuBar().addMenu(menu_file)

    def create_main_panel(self):
        self.panel = QtGui.QFrame(self)
        self.setCentralWidget(self.panel)

        self.init_plot()
        self.canvas = FigCanvas(self.fig)
        self.canvas.setMinimumHeight(150)

        #self.toolbar = NavigationToolbar(self.canvas, None)
        self.vbox = QtGui.QVBoxLayout()
        self.vbox.addWidget(self.canvas)

        self.panel.setLayout(self.vbox)
        #self.vbox.Fit(self)

        self.unit = 20
        width, height = self.geometry().width(), self.geometry().height()
        self.show()

    def init_plot(self):
        self.dpi = 100
        self.fig = Figure((5.0, 3.0), dpi=self.dpi)

        self.axes = self.fig.add_subplot(111, navigate=False)
        self.axes.set_axis_bgcolor('black')

        self.axes.set_title('Very important random data', size=10)
        self.axes.set_xlabel('Time flies like an arrow',size=10)
        self.axes.set_ylabel('Random is just random',size=10)

        pylab.setp(self.axes.get_xticklabels(), fontsize=8)
        pylab.setp(self.axes.get_yticklabels(), fontsize=8)

        self.plot_data = self.axes.plot(
            self.thing1.getVec(),
            linewidth=0.5,
            color=(1, 1, 0),
            #marker='o',
            label="set1",
            )[0]
        print  self.thing1.getVec(), "<<>>"
        self.plot_data2 = self.axes.plot(
            self.thing2.getVec(),
            linewidth=1,
            dashes=[.2, .4],
            color=(0, 1, 1),
            label="set2",
            )[0]


    def draw_plot(self):
        """ Redraws the plot
        """
        self.data = self.thing1.getVec()
        self.data2 = self.thing2.getVec()
        def do_cal(urdata):
            newdata = []
            for x in range(len(urdata)):
                urtime = x + self.starttime
                newdata.append(urtime)
            return newdata

        xmax = len(self.data) if len(self.data) > 50 else 50

        xmin = xmax - 50

        min1 = min(self.data)
        min2 = min(self.data2)
        theMin = min(min1, min2)

        ymin = round(theMin, 0) - 1

        max1 = max(self.data)
        max2 = max(self.data2)
        theMax = max(max1, max2)

        ymax = round(theMax, 0) + 1

        self.axes.set_xbound(lower=xmin, upper=xmax)
        self.axes.set_ybound(lower=ymin, upper=ymax)

        self.axes.grid(True, color='gray')
        pylab.setp(self.axes.get_xticklabels(),
                visible=True)

        self.plot_data.set_xdata(np.arange(len(self.data)))
        self.plot_data.set_ydata(np.array(self.data))
        self.plot_data2.set_xdata(np.arange(len(self.data2)))
        #self.plot_data2.set_xdata(np.array(newdata2))
        self.plot_data2.set_ydata(np.array(self.data2))

        self.canvas.draw()


    def on_redraw_timer(self):
        self.draw_plot()

    def on_exit(self):
        self.close()

    def closeEvent(self, event):
        for thing in (self.thing1, self.thing2):
            thing.theThread.stop()
            thing.theThread.join()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    aw = ApplicationWindow()
    aw.show()
    sys.exit(app.exec_())

#################################
[image: 内嵌图片 1]
------------------------------------------------------------------------------
HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions
Find What Matters Most in Your Big Data with HPCC Systems
Open Source. Fast. Scalable. Simple. Ideal for Dirty Data.
Leverages Graph Analysis for Fast Processing & Easy Data Exploration
http://p.sf.net/sfu/hpccsystems
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to