In [13]:
import MySQLdb
import sys
connection = MySQLdb.connect(host='power', user='root', passwd='pet=81zi', db='test')
cursor = connection.cursor()
%pylab

figWidth, figHeight = 18, 10


Using matplotlib backend: module://IPython.kernel.zmq.pylab.backend_inline
Populating the interactive namespace from numpy and matplotlib

In [14]:
SQL = """SELECT MIN( `DateTime` ), 
                    Convert(MIN( `DateTime` ),Date) AS 'Datum', 
                    Convert(MIN( `DateTime` ),Time) AS 'Uhrzeit',
                    AVG( `Voltage` ) AS 'Voltage', 
                    AVG( `Current`*1000 ) AS 'Current', 
                    AVG( `Phase` ) AS 'Phase', 
                    COUNT( `DateTime` ) AS 'Count',
                    AVG( `Voltage`*`Current` ) AS 'Leistung' 
                    FROM `PandasTest`
                    WHERE (CONVERT(`DateTime`, DATE) > DATE_SUB(CURRENT_DATE,INTERVAL 21 DAY))
                    GROUP BY Convert(`DateTime`,Date), Hour( `DateTime` ) , Minute( `DateTime` ) 
                    ORDER BY `DateTime`"""

In [15]:
SQL = """SELECT MIN( `DateTime` ), 
                    Convert(MIN( `DateTime` ),Date) AS 'Datum', 
                    Convert(MIN( `DateTime` ),Time) AS 'Uhrzeit',
                    AVG( `Voltage` ) AS 'Voltage', 
                    AVG( `Current`*1000 ) AS 'Current', 
                    AVG( `Phase` ) AS 'Phase', 
                    COUNT( `DateTime` ) AS 'Count',
                    AVG( `Voltage`*`Current` ) AS 'Leistung' 
                    FROM `PandasTest`
                    WHERE (CONVERT(`DateTime`, DATE) > '2013-11-19')
                    GROUP BY Convert(`DateTime`,Date), Hour( `DateTime` ) , Minute( `DateTime` ) 
                    ORDER BY `DateTime`"""

In [16]:
cursor.execute(SQL)
rows = cursor.fetchall()
nprows = np.array(rows)
fig = plt.figure()
figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
fig.autofmt_xdate()
plot(nprows[:,0],nprows[:,5],alpha = 0.80, color = "#A60628", label = "Phase")
# plt.fill_between(nprows[:,0],nprows[:,5], alpha = .4, facecolor = ["#A60628"])
plt.ylabel(u"Phase $[\%]$")
plt.xlabel(r'Uhrzeit $[HH:MM:SS]$')
plt.title( "Phase %s \n Arbeitsplatz (BW)" % "der letzten Tage")
plt.legend()
plt.grid(True)


<matplotlib.figure.Figure at 0x4f4ea10>

In [17]:
fig = plt.figure()
figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
fig.autofmt_xdate()
plot(nprows[:,0],nprows[:,3],alpha = 0.80, color = "#A60628", label = "Voltage")
plt.ylabel(u"Voltage $[V]$")
plt.xlabel(r'Uhrzeit $[HH:MM:SS]$')
plt.title( "Spannung %s \n Arbeitsplatz (BW)" % "der letzten Tage")
plt.legend()
plt.grid(True)


<matplotlib.figure.Figure at 0x4f34590>

In [18]:
fig = plt.figure()
figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
fig.autofmt_xdate()
plot(nprows[:,0],nprows[:,4],alpha = 0.80, color = "#A60628", label = "Current")
plt.ylabel(u"Current $[mA]$")
plt.xlabel(r'Uhrzeit $[HH:MM:SS]$')
plt.title( "Strom %s \n Arbeitsplatz (BW)" % "der letzten Tage")
plt.legend()
plt.grid(True)


<matplotlib.figure.Figure at 0x4908ad0>

In [19]:
fig = plt.figure()
figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
fig.autofmt_xdate()
plot(nprows[:,0],nprows[:,7],alpha = 0.80, color = "#A60628", label = "Power")
plt.ylim(25, 65)
plt.ylabel(u"Power $[W]$")
plt.xlabel(r'Uhrzeit $[HH:MM:SS]$')
plt.title( "Leistung %s \n Arbeitsplatz (BW)" % "der letzten Tage")
plt.legend()
plt.grid(True)


<matplotlib.figure.Figure at 0x490b390>

Tagesauswertungen

Nachfolgend werden für jeden Tag alle Werte aus der Datenbank geladen und anschließend pro Tag ein Plot erstellt.


In [21]:
SQL = """SELECT MIN( `DateTime` ), 
                    Convert(MIN( `DateTime` ),Date) AS 'Datum', 
                    Convert(MIN( `DateTime` ),Time) AS 'Uhrzeit',
                    AVG( `Voltage` ) AS 'Voltage', 
                    AVG( `Current`*1000 ) AS 'Current', 
                    AVG( `Phase` ) AS 'Phase', 
                    COUNT( `DateTime` ) AS 'Count',
                    AVG( `Voltage`*`Current` ) AS 'Leistung' 
                    FROM `PandasTest`
                    WHERE (CONVERT(`DateTime`, DATE) = '%s') 
                    AND (CONVERT(`DateTime`, Time) > '07:00:00')
                    AND (CONVERT(`DateTime`, Time) < '21:00:00')
                    GROUP BY Convert(`DateTime`,Date), Hour( `DateTime` ) , Minute( `DateTime` ) 
                    ORDER BY `DateTime`"""

def printDays(days):
    """plots Diagramms for each Day"""
    for day in days:
        try:
            SQL_Day = SQL % day
            print "Now plotting: %s" % day
            #print SQL_Day
            cursor.execute(SQL_Day)
            rows = cursor.fetchall()
            nprows = np.array(rows)
        
            fig = plt.figure()
            figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
            fig.autofmt_xdate()
            plot(nprows[:,0],nprows[:,4],alpha = 0.80, color = "#A60628", label = "Current")
            plt.ylabel(u"Current $[mA]$")
            plt.xlabel(r'Uhrzeit $[HH:MM:SS]$')
            plt.title( "Strom vom %s \n Arbeitsplatz (BW)" % day)
            plt.legend()
            plt.grid(True)
            
            fig = plt.figure()
            figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
            fig.autofmt_xdate()
            plot(nprows[:,0],nprows[:,3],alpha = 0.80, color = "#A60628", label = "Voltage")
            plt.ylabel(u"Voltage $[V]$")
            plt.xlabel(r'Uhrzeit $[HH:MM:SS]$')
            plt.title( "Spannung vom %s \n Arbeitsplatz (BW)" % day)
            plt.legend()
            plt.grid(True)
            
            fig = plt.figure()
            figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
            fig.autofmt_xdate()
            plot(nprows[:,0],nprows[:,5],alpha = 0.80, color = "#A60628", label = "Phase")
            plt.ylabel(u"Phase $[\%]$")
            plt.xlabel(r'Uhrzeit $[HH:MM:SS]$')
            plt.title( "Phase %s vom \n Arbeitsplatz (BW)" % day)
            plt.legend()
            plt.grid(True)
            
            fig = plt.figure()
            figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
            fig.autofmt_xdate()
            plot(nprows[:,0],nprows[:,7],alpha = 0.80, color = "#A60628", label = "Power")
            #plt.ylim(25, 65)
            plt.ylabel(u"Power $[W]$")
            plt.xlabel(r'Uhrzeit $[HH:MM:SS]$')
            plt.title( "Leistung vom %s \n Arbeitsplatz (BW)" % day)
            plt.legend()
            plt.grid(True)
            print "Finished sucessfully"
        except:
            print "Fault in %s" % day

printDays(['2013-11-12','2013-11-13','2013-11-14','2013-11-15','2013-11-16','2013-11-17','2013-11-18','2013-11-19',
           '2013-11-20','2013-11-21','2013-11-22','2013-11-23','2013-11-24','2013-11-25'])


Now plotting: 2013-11-12
Finished sucessfully
Now plotting: 2013-11-13
Finished sucessfully
Now plotting: 2013-11-14
Finished sucessfully
Now plotting: 2013-11-15
Finished sucessfully
Now plotting: 2013-11-16
Finished sucessfully
Now plotting: 2013-11-17
Finished sucessfully
Now plotting: 2013-11-18
Finished sucessfully
Now plotting: 2013-11-19
Finished sucessfully
Now plotting: 2013-11-20
Fault in 2013-11-20
Now plotting: 2013-11-21
Finished sucessfully
Now plotting: 2013-11-22
Finished sucessfully
Now plotting: 2013-11-23
Finished sucessfully
Now plotting: 2013-11-24
Finished sucessfully
Now plotting: 2013-11-25
Finished sucessfully
<matplotlib.figure.Figure at 0x50ca950>
<matplotlib.figure.Figure at 0x50ca850>
<matplotlib.figure.Figure at 0x7b2a090>
<matplotlib.figure.Figure at 0x88a6a90>
<matplotlib.figure.Figure at 0x5d6b250>
<matplotlib.figure.Figure at 0x50c1950>
<matplotlib.figure.Figure at 0x528e510>
<matplotlib.figure.Figure at 0x145ba6d0>
<matplotlib.figure.Figure at 0x145e62d0>
<matplotlib.figure.Figure at 0x137d1c10>
<matplotlib.figure.Figure at 0x88a9e50>
<matplotlib.figure.Figure at 0x8a1ae90>
<matplotlib.figure.Figure at 0x14e5dc50>
<matplotlib.figure.Figure at 0x5283b90>
<matplotlib.figure.Figure at 0x8d5d150>
<matplotlib.figure.Figure at 0x875f810>
<matplotlib.figure.Figure at 0x872de50>
<matplotlib.figure.Figure at 0x81c0650>
<matplotlib.figure.Figure at 0x81a1490>
<matplotlib.figure.Figure at 0x85ee6d0>
<matplotlib.figure.Figure at 0x11213f50>
<matplotlib.figure.Figure at 0x84c53d0>
<matplotlib.figure.Figure at 0x7756310>
<matplotlib.figure.Figure at 0x7b50990>
<matplotlib.figure.Figure at 0x7b4f090>
<matplotlib.figure.Figure at 0x8c04b10>
<matplotlib.figure.Figure at 0x529c050>
<matplotlib.figure.Figure at 0x7f53e50>
<matplotlib.figure.Figure at 0xd3ada50>
<matplotlib.figure.Figure at 0x88cb210>
<matplotlib.figure.Figure at 0x1032bb10>
<matplotlib.figure.Figure at 0x832e210>
<matplotlib.figure.Figure at 0xb57fa10>
<matplotlib.figure.Figure at 0xb57ff50>
<matplotlib.figure.Figure at 0xb58e110>
<matplotlib.figure.Figure at 0xe370f90>
<matplotlib.figure.Figure at 0x5d7b450>
<matplotlib.figure.Figure at 0x7dd9f90>
<matplotlib.figure.Figure at 0x7db29d0>
<matplotlib.figure.Figure at 0x6261690>
<matplotlib.figure.Figure at 0x6243b90>
<matplotlib.figure.Figure at 0x636e690>
<matplotlib.figure.Figure at 0xc371c50>
<matplotlib.figure.Figure at 0x70f9d10>
<matplotlib.figure.Figure at 0x70ef3d0>
<matplotlib.figure.Figure at 0x6b065d0>
<matplotlib.figure.Figure at 0x78b2510>
<matplotlib.figure.Figure at 0x9656ad0>
<matplotlib.figure.Figure at 0x7678190>
<matplotlib.figure.Figure at 0x74ec410>
<matplotlib.figure.Figure at 0x74efed0>
<matplotlib.figure.Figure at 0x7f28610>
<matplotlib.figure.Figure at 0x79ea0d0>
<matplotlib.figure.Figure at 0x69aa090>

Statistische Auswertungsbeispiele


In [128]:
SQL = """SELECT Count(`DateTime`) AS `Anzahl`, 
         AVG(`Voltage`) AS `Voltage`
         FROM `PandasTest` 
         WHERE `Voltage` > 0 
         GROUP BY Round(`Voltage`,1)"""
cursor.execute(SQL)
rows = cursor.fetchall()
nprows = np.array(rows)
fig = plt.figure()
figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
plt.bar( nprows[:,1], nprows[:,0],width=0.1, color="#A60628",
        label = "number of measure points \nmeasured variable: Voltage", alpha = 0.95)
#plot(nprows[:,1],nprows[:,0],alpha = 0.80, color = "#A60628", label = "Phase")
# plt.fill_between(nprows[:,0],nprows[:,5], alpha = .4, facecolor = ["#A60628"])
plt.ylabel(u"number of points")
plt.xlabel(r'Voltage $[V]$')
plt.xlim( 230,240);
plt.title( "statistic distribution of voltage")
plt.legend()
plt.grid(True)

mean = 0
number =0 
for line in nprows:
    mean = mean + line[0]*line[1]
    number = number + line[0]
mean = mean/number
print "The mean value: %s" % mean


The mean value: 233.990251399
<matplotlib.figure.Figure at 0x79e83d0>

In [129]:
SQL = """SELECT Count(`DateTime`) AS `Anzahl`, 
          Round(AVG(`Current`),2) AS `Current`
         FROM `PandasTest` 
         WHERE `Current` > 0 
         GROUP BY Round(`Current`,2)"""
cursor.execute(SQL)
rows = cursor.fetchall()
nprows = np.array(rows)
#print nprows
fig = plt.figure()
figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
plt.bar( nprows[:,1], nprows[:,0],width=0.01, color="#A60628",
        label = "number of measure points \nmeasured variable: Current", alpha = 0.95)
#plot(nprows[:,1],nprows[:,0],alpha = 0.80, color = "#A60628", label = "Phase")
# plt.fill_between(nprows[:,0],nprows[:,5], alpha = .4, facecolor = ["#A60628"])
plt.ylabel(u"number of points")
plt.xlabel(r'Current $[A]$')
# plt.xlim( 2.3,2.35)
plt.ylim( 0,800)
plt.title( "statistic distribution of current")
plt.legend()
plt.grid(True)

mean = 0
number =0 
for line in nprows:
    mean = mean + line[0]*line[1]
    number = number + line[0]
mean = mean/number
print "The mean value: %s" % mean


The mean value: 0.124443098575
<matplotlib.figure.Figure at 0x46dfdd0>

In [130]:
SQL = """SELECT Count(`DateTime`) AS `Anzahl`, 
          Round(AVG(`Phase`),2) AS `Phase`
         FROM `PandasTest` 
         WHERE `Phase` > 0 
         GROUP BY Round(`Phase`,2)"""
cursor.execute(SQL)
rows = cursor.fetchall()
nprows = np.array(rows)
#print nprows
fig = plt.figure()
figure(num=None, figsize=(figWidth, figHeight), dpi=300, facecolor='w', edgecolor='k')
plt.bar( nprows[:,1], nprows[:,0],width=0.01, color="#A60628",
        label = "number of measure points \nmeasured variable: Phase", alpha = 0.95)
#plot(nprows[:,1],nprows[:,0],alpha = 0.80, color = "#A60628", label = "Phase")
# plt.fill_between(nprows[:,0],nprows[:,5], alpha = .4, facecolor = ["#A60628"])
plt.ylabel(u"number of points")
plt.xlabel(r'Phase $[\%]$')
# plt.xlim( 2.3,2.35)
#plt.ylim( 0,800)
plt.title( "statistic distribution of phase")
plt.legend()
plt.grid(True)

mean = 0
number =0 
for line in nprows:
    mean = mean + line[0]*line[1]
    number = number + line[0]
mean = mean/number
print "The mean value: %s" % mean


The mean value: 0.321592419665
<matplotlib.figure.Figure at 0x4784250>

In [ ]: