Search code examples
numpymatplotlibgnuplot

conditional plot with lines in matplotlib


The following is my log file,

data.log

sa  4011    1259    3840    15      4864    19      156
sa  4011    1267    3840    15      5120    20      157
sa  4011    1275    3840    15      5376    21      158
sa  4010    1282    3072    3       1024    1       56
sd  4010    1283    2048    2       1024    1       41      QIO1
sa  4011    1283    3840    15      5632    22      159
sd  4011    1291    3584    14      5632    22      114     QIO1
sa  4011    1291    3840    15      5632    22      161
sa  4011    1299    3840    15      5888    23      162
sa  4011    1307    3840    15      6144    24      163
sa  4010    1314    3072    3       1024    1       60
sa  4011    1315    3840    15      6400    25      164
sd  4010    1323    2048    2       1024    1       46      QIO1
sa  4011    1323    3840    15      6656    26      166

I want to do a conditional plot, based on the second column. My x axis should be column 3, y axis should be column 5. Since I have two different elements in column 2 (4010 and 4011), I need to use two colours. For example,

red can be used to represent the sa and sd of 4010 and blue can be used to represent the sa and sd of 4010

I tried to do the above using gnuplot but could not succeed due to Gnuplot: conditional plotting ($2 == 15 ? $2 : '1/0') with lines

I am trying to do this using matpyplot, but I am new to it. Can someone guide me how to proceed. This is how far I got.

import numpy as np
import pylab as pl


data = np.loadtxt('data.log')
# plot the 3rd column as x, and 5th column as y
pl.plot(data[:,3], data[:,5], 'ro')
pl.xlabel('x')
pl.ylabel('y')

pl.show()

Thanks again.


Solution

  • Thanks for all your comments. Here is what I finally resorted to.

    cat data.log | grep "^[sa|sd]" | while read l; do i=`echo $l |awk '{print $2}'`; echo $l >> ${i}.log; done
    

    I got the two files 4010.log and 4011.log

    set terminal png
    set output 'overlay_image.png'
    
    set xtic auto                          # set xtics automatically
    set ytic auto
    set yrange [0:63]
    
    set autoscale
    
    set grid
    set title "queue size vs time (in ns)"
    set xlabel "time (in ns)"
    set ylabel "queue size"
    
    set style data linespoints
    
    plot "4010.log" using 3:5 title "IQ 0", \
         "4011.log"  using 3:5 title "IQ 1"