Real Time temperature plotting with python
<p>I am currently making a project which requires real time monitoring of various quantities like temperature, pressure, humidity etc. I am following a approach of making individual arrays of all the sensors and ploting a graph using matplotlib and drwnow.</p>

<pre><code>HOST = "localhost"
PORT = 4223

UID1 = "tsJ" # S1

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_ptc import BrickletPTC
import numpy as np
import serial

import matplotlib
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

from drawnow import *

# creating arrays to feed the data

tempC1 = []

def makeafig():

    # creating subplots
    fig1 = plt.figure(1)

    a = fig1.add_subplot(111)

    #setting up axis label, auto formating of axis and title
    a.set_xlabel('Time [s]', fontsize = 10)
    a.set_ylabel('Temperature [Â°C]', fontsize = 10)
    y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
    a.yaxis.set_major_formatter(y_formatter)
    title1 = "Current Room Temperature (Side1): " + str(temperature1/100) + " Â°C"
    a.set_title(title1, fontsize = 10)

    #plotting the graph
    a.plot(tempC1, "#00A3E0")

    #saving the figure
    fig1.savefig('RoomTemperature.png', dpi=100)

while True:

    ipcon = IPConnection() # Create IP connection
    ptc1 = BrickletPTC(UID1, ipcon) # S1 

    ipcon.connect(HOST, PORT) # Connect to brickd

    #setting the temperature from PTC bricklet

    temperature1 = ptc1.get_temperature()


    #processing data from a temperature sensor to 1st array
    dataArray1=str(temperature1/100).split(',')
    temp1 = float(dataArray1[0])
    tempC1.append(temp1)

    #making a live figure
    drawnow(makeafig)
    plt.draw()
</code></pre>

<p>This is the approach I found good on the internet and it is working. The only problem I am facing is It consumes more time if I made more arrays for other sensors and the plot being made lags from the real time when I compare it with a stopwatch.</p>

<p>Is there any good and efficient approach for obtaining live graphs that will be efficient with lot of sensors and doen't lag with real time.
Or any command to clear up the already plotted array values?</p>

<p>I'd be obliged if anyone can help me with this problem.</p>

<blockquote>
  <p>I'd like to ask that, does filling data in arrays continuosly makes the process slow or is it my misconception?</p>
</blockquote>

<p>That one is easy to test; creating an empty list and appending a few thousand values to it takes roughly 10^-4 seconds, so that shouldn't be a problem. For me somewhat surprisingly, it is actually faster than creating and filling a fixed size <code>numpy.ndarray</code> (but that is probably going to depend on the size of the list/array). </p>

<p>I quickly played around with your <code>makeafig()</code> function, placing <code>a = fig1.add_subplot(111)</code> up to (including) <code>a.plot(..)</code> in a simple <code>for i in range(1,5)</code> loop, with <code>a = fig1.add_subplot(2,2,i)</code>; that makes <code>makeafig()</code> about 50% slower, but differences are only about 0.1-0.2 seconds. Is that in line with the lag that you are experiencing?</p>

<p>That's about what I can test without the real data, my next step would be to time the part from <code>ipcon=..</code> to <code>temperature1=..</code>. Perhaps the bottleneck is simply the retrieval of the data? I'm sure that there are several examples on SO on how to time parts of Python scripts, for these kind of problems something like the example below should be sufficient:</p>

<pre><code>import time
t0 = time.time()
# do something
dt = time.time() - t0
</code></pre>

python matplotlib