How to separate and graph lines in array by a value in each line
<p>I'm new to Python and am trying to pull data from a growing CSV, and create a live updating plot. I want to create two different x,y arrays depending on which antenna the data is coming through (one of the values in each line of data separated by commas). The data file looks like the following:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>TimeStamp, ReadCount, Antenna, Protocol, RSSI, EPC, Sensor
09/21/2016 15:24:40.560, 5499, 1, GEN2, -21, E036112D912508B3, 23.78,47.00,0.00,2.21, (Infinity%)
09/21/2016 15:24:41.138, 5506, 1, GEN2, -9, E036112D912508B3, 23.99,46.00,0.00,2.26, (Infinity%)
09/21/2016 15:24:41.623, 5513, 1, GEN2, -25, E036112D912508B3, 23.99,46.00,0.00,2.26, (Infinity%)
09/21/2016 15:24:42.120, 5520, 1, GEN2, -18, E036112D912508B3, 23.78,46.00,0.00,2.26, (Infinity%)
09/21/2016 15:24:42.633, 5527, 1, GEN2, -12, E036112D912508B3, 23.99,45.00,0.00,2.23, (Infinity%)
09/21/2016 15:24:43.211, 5534, 1, GEN2, -9, E036112D912508B3, 23.99,46.00,0.00,2.26, (Infinity%)
09/21/2016 15:24:43.744, 5541, 1, GEN2, -16, E036112D912508B3, 23.99,46.00,0.00,2.26, (Infinity%)</code></pre>
</div>
</div>
</p>

<p>Code I have which successfully shows the graph, but just takes in all lines of data into one x,y set of arrays looks like the following: </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from datetime import datetime
      
offset = -7.4954
slope = 0.9548

fig = plt.figure(facecolor='#07000d')

ax1 = fig.add_subplot(111, axisbg='#07000d')

ax1.spines['bottom'].set_color("#5998ff")
ax1.spines['top'].set_color("#5998ff")
ax1.spines['left'].set_color("#5998ff")
ax1.spines['right'].set_color("#5998ff")
    
def animate(i):
    graph_data = open('SensorLog.csv','r').read()
    dataArray = graph_data.split('\n')

    xar=[]
    yar=[]
    
    for eachLine in dataArray:
        if 'TimeStamp' not in eachLine: 
            if len(eachLine)&gt;1:
                t,rc,ant,prot,rssi,epc,temp,ten,powr,unpowr,inf=(eachLine.split(','))         
                time = datetime.strptime(t, '%m/%d/%Y %H:%M:%S.%f')
                clock = time.strftime('%I:%M')
                xs = matplotlib.dates.datestr2num(clock)
                hfmt = matplotlib.dates.DateFormatter('%m/%d/%Y\n%I:%M:%S %p')

#                Convert tension
                tension = int(float(ten)*float(slope)+float(offset))
                
                xar.append(xs)
                yar.append(tension)
                
    ax1.clear()
    
    ax1.grid(True, color='w')

    plt.ylabel('Tension (lb)',color='w', fontsize=20)
    plt.title('Spiral 1 Tension',color='w', fontsize=26)

       
    ax1.tick_params(axis='y', colors='w')
    ax1.tick_params(axis='x', colors='w')
    ax1.xaxis.set_major_formatter(hfmt)

    fig.autofmt_xdate()
    
    ax1.plot (xar,yar, 'c', linewidth=2)

ani = animation.FuncAnimation(fig, animate, interval=10000)
plt.show()</code></pre>
</div>
</div>
</p>

<p>I am trying to separate data pulled in on antenna 1 and 2 and plot each on the same graph (shared x axis) with different colored line plots...my attempt is here but it is not working:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from datetime import datetime
      
offset = -7.4954
slope = 0.9548

fig = plt.figure(facecolor='#07000d')

ax1 = fig.add_subplot(111, axisbg='#07000d')
ax2 = fig.add_subplot(111, axisbg='#07000d')

ax1.spines['bottom'].set_color("#5998ff")
ax1.spines['top'].set_color("#5998ff")
ax1.spines['left'].set_color("#5998ff")
ax1.spines['right'].set_color("#5998ff")

ax2.spines['bottom'].set_color("#5998ff")
ax2.spines['top'].set_color("#5998ff")
ax2.spines['left'].set_color("#5998ff")
ax2.spines['right'].set_color("#5998ff")
    
def animate(i):
    graph_data = open('SensorLog.csv','r').read()
    dataArray = graph_data.split('\n')

    xar=[]
    yar=[]
    xar2=[]
    yar2=[]    

    for eachLine in dataArray:
        if 'TimeStamp' not in eachLine: 
            if len(eachLine)&gt;1:
                t,rc,ant,prot,rssi,epc,temp,ten,powr,unpowr,inf=(eachLine.split(','))         
                time = datetime.strptime(t, '%m/%d/%Y %H:%M:%S.%f')
                clock = time.strftime('%I:%M')
                xs = matplotlib.dates.datestr2num(clock)
                hfmt = matplotlib.dates.DateFormatter('%m/%d/%Y\n%I:%M:%S %p')

#                Convert tension
                tension = int(float(ten)*float(slope)+float(offset))
                              
                if ant == '1':
                    xar.append(xs)
                    yar.append(tension)
               
                if ant == '2':
                    xar2.append(xs)                    
                    yar2.append(tension)
                   
    ax1.clear()
    ax2.clear()
    
    ax1.grid(True, color='w')
    ax2.grid(True, color='w')

    plt.ylabel('Tension (lb)',color='w', fontsize=20)
    plt.title('Spiral 1 Tension',color='w', fontsize=26)

       
    ax1.tick_params(axis='y', colors='w')
    ax1.tick_params(axis='x', colors='w')
    ax1.xaxis.set_major_formatter(hfmt)
   
    ax2.tick_params(axis='y', colors='w')
    ax2.tick_params(axis='x', colors='w')
    ax2.xaxis.set_major_formatter(hfmt)
   
    fig.autofmt_xdate()
    
    ax1.plot (xar,yar, 'c', linewidth=2)
    ax2.plot (xar2,yar2,'r', linewidth=3)

ani = animation.FuncAnimation(fig, animate, interval=10000)
plt.show()</code></pre>
</div>
</div>
</p>

<p>Do you guys have any input on how I could successfully separate ant 1 and ant 2 data and plot it on the same figure in different colors?</p>

<p>You can simply plot each data set using the same axis. </p>

<p>The following approach uses the Python <code>csv.DictReader</code> to help with reading in the data, along with a <code>defaultdict(list)</code> to automatically split the data into lists based on the antenna for each row.</p>

<p>This also adds code to addresses your comment regarding grouping data points not more than 60 seconds apart from each other, and only displaying the last 5 minutes worth of entries:</p>

<pre><code>import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation

from datetime import datetime, timedelta
import collections
import csv

offset = -7.4954
slope = 0.9548

def plot(ax, data, colour, width):
    if data:
        last_dt = data[0][0]
        sixty = timedelta(seconds=60)

        x = []
        y = []

        # Plot groups of data not more than 60 seconds apart
        for dt, ten in data:
            if dt &lt;= last_dt + sixty:
                x.append(dt)
                y.append(ten)
            else:
                ax.plot(matplotlib.dates.date2num(x), y, colour, linewidth=width)
                x = [dt]
                y = [ten]

            last_dt = dt

        ax.plot(matplotlib.dates.date2num(x), y, colour, linewidth=width)


def animate(i, fig, ax):
    # Read in the CSV file
    data = collections.defaultdict(list)
    fields = ["TimeStamp", "ReadCount", "Antenna", "Protocol", "RSSI", "EPC", "Temp", "Ten", "Powr", "Unpowr", "Inf"]

    with open('SensorLog.csv') as f_input:
        csv_input = csv.DictReader(f_input, skipinitialspace=True, fieldnames=fields)
        header = next(csv_input)

        # Separate the rows based on the Antenna field
        for row in csv_input:
            try:
                data[row['Antenna']].append(
                    [datetime.strptime(row['TimeStamp'], '%m/%d/%Y %H:%M:%S.%f'), 
                    int(float(row['Ten']) * float(slope) + float(offset))])
            except TypeError as e:
                pass

    # Drop any data points more than 5 mins older than the last entry

    latest_dt = data[row['Antenna']][-1][0]     # Last entry
    not_before = latest_dt - timedelta(minutes=5)

    for antenna, entries in data.items():
        data[antenna] = [[dt, count] for dt, count in entries if dt &gt;= not_before]

    # Redraw existing axis
    ax.clear()

    ax.spines['bottom'].set_color("#5998ff")
    ax.spines['top'].set_color("#5998ff")
    ax.spines['left'].set_color("#5998ff")
    ax.spines['right'].set_color("#5998ff")

    hfmt = matplotlib.dates.DateFormatter('%m/%d/%Y\n%I:%M:%S %p')
    ax.xaxis.set_major_formatter(hfmt)
    fig.autofmt_xdate()

    plot(ax, data['1'], 'c', 2)     # Antenna 1
    plot(ax, data['2'], 'r', 3)     # Antenna 2

    ax.grid(True, color='w')
    plt.ylabel('Tension (lb)', color='w', fontsize=20)
    plt.title('Spiral 1 Tension', color='w', fontsize=26)

    ax.tick_params(axis='y', colors='w')
    ax.tick_params(axis='x', colors='w')

fig = plt.figure(facecolor='#07000d')
ax = fig.add_subplot(111, axisbg='#07000d')

ani = animation.FuncAnimation(fig, animate, fargs=(fig, ax), interval=1000)
plt.show()
</code></pre>

<p>This would give you the following kind of output:</p>

<p><a href="http://i.stack.imgur.com/RsCrD.png" rel="nofollow"><img src="http://i.stack.imgur.com/RsCrD.png" alt="demo plot"></a></p>

python