用Pyqt绘图并使用mysql中的数据

时间:2012-08-01 14:40:59

标签: python matplotlib pyqt

我想使用mysql表中的数据在pyqt中制作一个实时图,但我不确定如何。该表将每秒更新一次,并具有CPU百分比(例如2.5,3.5,4.5)。我不知道如何使用mysql中的数据制作实时图表。

我在下面制作了一个简单的matplotlib图。我试图合并mysql数据,但我无法满足这个实时图的需要。

import sys
from PyQt4 import QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg \
import FigureCanvasQTAgg as FigureCanvas
import psutil as p

# Total number of iterations
MAXITERS = 2000
class CPUMonitor(FigureCanvas):
    """Matplotlib Figure widget to display CPU utilization"""
    def __init__(self):
        # save the current CPU info (used by updating algorithm)
        self.before = self.prepare_cpu_usage()
        # first image setup
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        # initialization of the canvas
        FigureCanvas.__init__(self, self.fig)
        # set specific limits for X and Y axes
        self.ax.set_xlim(0, 2000)
        self.ax.set_ylim(0, 100)
        # and disable figure-wide autoscale
        self.ax.set_autoscale_on(False)
        # generates first "empty" plots
        self.user, self.nice, self.sys, self.idle =[], [], [], []
        self.l_user, = self.ax.plot([],self.user, label='User %')
        self.l_nice, = self.ax.plot([],self.nice, label='Nice %')
        self.l_sys, = self.ax.plot([],self.sys, label='Sys %')
        self.l_idle, = self.ax.plot([],self.idle, label='Idle %')
        # add legend to plot
        self.ax.legend()
        # force a redraw of the Figure
        self.fig.canvas.draw()
        # initialize the iteration counter
        self.cnt = 0
        # call the update method (to speed-up visualization)
        self.timerEvent(None)
        # start timer, trigger event every 1000 millisecs (=1sec)
        self.timer = self.startTimer(1000)
    def prepare_cpu_usage(self):
        """helper function to return CPU usage info"""
        # get the CPU times using psutil module
        t = p.cpu_times()
        # return only the values we're interested in
        if hasattr(t, 'nice'):
            return [t.user, t.nice, t.system, t.idle]
        else:
            # special case focr Windows, without 'nice' value
            return [t.user, 0, t.system, t.idle]
    def get_cpu_usage(self):
        """Compute CPU usage comparing previous and current measurements"""
        # take the current CPU usage information
        now = self.prepare_cpu_usage()
        # compute delta between current and previous measurements
        delta = [now[i]-self.before[i] for i in range(len(now))]
        # compute the total (needed for percentages calculation)
        total = sum(delta)
        # save the current measurement to before object
        self.before = now
        # return the percentage of CPU usage for our 4 categories
        return [(100.0*dt)/total for dt in delta]
    def timerEvent(self, evt):
        # get the cpu percentage usage
        result = self.get_cpu_usage()
        # append new data to the datasets
        self.user.append(result[0])
        self.nice.append(result[1])
        self.sys.append( result[2])
        self.idle.append(result[3])
        # update lines data using the lists with new data
        self.l_user.set_data(range(len(self.user)), self.user)
        self.l_nice.set_data(range(len(self.nice)), self.nice)
        self.l_sys.set_data( range(len(self.sys)), self.sys)
        self.l_idle.set_data(range(len(self.idle)), self.idle)
        # force a redraw of the Figure
        self.fig.canvas.draw()
        if self.cnt == MAXITERS:
            # stop the timer
            self.killTimer(self.timer)
        else:
           #else, we increment the counter
           self.cnt += 1
# create the GUI application
widget = CPUMonitor()
widget.setWindowTitle("30 Seconds of CPU Usage Updated in RealTime")
widget.show()

sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

一种选择是在无限循环中运行它。循环每秒都会检查表,然后每次都重新绘制matplotlib图。

虽然这不是一个非常有效的解决方案。