Summing over y values for same x value - gnuplot

I want gnuplot to plot the sum of all z values in all cases where the x and y values are equal.
A dummy data file looks like this:
#testfile
0 0 1
0 1 1
0 1 1
0 1 1
1 0 1
1 1 1
1 1 2
1 1 2
I am using plot "testfile" u 1:2:3 w p ps variable to scale the points according to the value in the third column, and I would like to find a command that gives the same plot for the above data file as if I were to plot this data file:
#testfile2
0 0 1
0 1 3
1 0 1
1 1 5
If that makes it easier, in my real data file, I always have to sum over two lines.

I don't know if you're looking for a gnuplot-only solution, but what you want could be accomplished with a simple awk one-liner, either ran separate or embedded on gnuplot. By the way, this assumes that you you always have to sum over two lines:
Input file:
0 1 1
0 1 1
1 0 1
1 0 2
1 1 2
1 1 2
By running:
awk '{sum+=$3} (NR%2)==0{print $1,$2,sum; sum=0;}' testfile
You would get:
0 1 2
1 0 3
1 1 4
Then you could save in a separate file and plot with the line you mentioned above. Alternatively, you can embed the awk line within gnuplot using:
plot "<awk '{sum+=$3} (NR%2)==0{print $1,$2,sum; sum=0;}' testfile" u 1:2:3 not w p ps variable pt 7
Hope it helps!

Related

heatmap color not relating with data in gnuplot

I am trying to create one heatmap using Gnuplot and my data file structure is looked like below:
6 5 4 3 1 0
3 2 2 0 0 1
0 0 0 0 1 0
0 0 0 0 2 3
0 0 1 2 4 3
the cell values are z values and columns represent y-axis and row are x-axes. that means the first value 6 is the z value where the y-axis is 5th position at x label zero. However, while plotting the heat map I am getting a different color which does not correlate with the z value. Also, I am getting five bins for the x-axis (which is supposed to be 6)and 4 bins (which is supposed to be 5) for the y-axis. My simple code is written below:
set pm3d map
splot 'm.txt' matrix
Please help me out of this confused situation.
Thanks.

Loop every three consecutive rows in linux

I have a file hundred.txt containing 100 rows.
For example:
1 0 0 1
1 1 0 1
1 0 1 0
1 0 1 0
0 1 1 0
....
1 0 0 1
I need to manipulate some calculations within every 3 consecutive rows, for instance, I need to use the Row1-Row3 first to do my calculation:
1 0 0 1
1 1 0 1
1 0 1 0
then the Row2-Row4:
1 1 0 1
1 0 1 0
1 0 1 0
...... the Row98-Row100.
Each output will generate a file (e.g. Row1.txt, Row2.txt,... Row98.txt), How can I solve this problem? Thank you.
bash isn't a great choice for data processing tasks, but it is possible (albeit slow):
{ read row1
read row2
count=0
while read row3; do
# Do something with rows 1-3
{ echo $row1 $row2 $row3; } > Row$((count+=1)).txt
# Slide the window
row1=$row2
row2=$row3
done
} < hundred.txt
awk to the rescue!
$ awk 'NR>2{printf "%s", a2 ORS a1 ORS $0 ORS > FILENAME"."(++c)}
{a2=a1;a1=$0}' file
for the input file
$ cat file
1 0 0 1
1 1 0 1
1 0 1 0
1 0 1 0
0 1 1 0
generates these 3
$ head file.{1..3}
==> file.1 <==
1 0 0 1
1 1 0 1
1 0 1 0
==> file.2 <==
1 1 0 1
1 0 1 0
1 0 1 0
==> file.3 <==
1 0 1 0
1 0 1 0
0 1 1 0
you can embed your computation is the script and output only the results but you didn't provide any details on that.
Explanation
NR>2 starting third row
printf ... start printing last 3 rows
> FILENAME"."(++c) to a file derived from input filename with counter suffix
a2=a1;a1=$0 update last two rows
if your rolling window is small n you can scale this script by changing NR>(n-1) and keeping track of last rows in a(n-1)...a1 and printing accordingly. If n is large, better to use a array (or better a circular array).
This is perhaps the most generic version...
$ awk -v n=3 'NR>n-1{fn=FILENAME"."c;
for(i=c+1;i<c+n;i++) printf "%s\n", a[(i-n)%n] > fn;
print > fn}
{a[(c++)%n]=$0}' file
One hundred rows of four binary-valued columns is not too much; just read it all in at once.
mapfile -t rows < inputfile
for r in "${!rows[#]}"; do # loop by row index
(( r >= 2 )) || continue
# process "${rows[r-2]}" "${rows[r-1]}" and "${rows[r]}"
# into file Row$((r-1))
done
If the quantity of data grows significantly, you really want to use a better tool, such as Python+numpy (because your data looks like binary matrices).

Gnuplot draw logical gate output in time

I am working on a school project, which is a simulation of logical gates. I can implement and run the simulation with ease, but i need help with showing the output.
Right now, i print everything to the console, like this:
sample frequency: 50
###############################################
IN NOT(1) OUT
IN1:0 IN1:3 IN1:5
IN2:0 IN2:0 IN2:0
OUT:3 OUT:5 OUT:0
0 1 -1 -1
50 1 -1 -1
100 1 0 0
150 0 0 0
200 1 1 1
250 1 0 0
300 1 0 0
350 1 0 0 (IN = 1, delay is 1 so we can see
400 0 0 0 the correct output of NOT element in line 400 <-> 350 + 1*50)
450 1 1 1
500 1 0 0
550 1 0 0
600 1 0 0
650 0 0 0
700 0 1 1
750 1 1 1
800 1 0 0
850 1 0 0
900 1 0 0
950 1 0 0
1000 1 0 0
on the left, there is the simulation time (step). In each step, the values are printed out and new set of inputs is generated.
where there is -1, this means undefined output.
The 3rd row ( IN NOT(1) OUT ) means that there are 3 elements, 1 input, 1 NOT gate and an output. The value in brackets means the delay of the element, so an element with delay value of X will show the correct output after X*sample_freq (excluding the 0 time).
The rows after mean:
IN1 - the index of the node that is read as input 1
IN2 - the index of the node that is read as input 2
OUT - the index of the output node
In this situation, the IN is giving its output to node #3. The NOT element reads its input from node #3 and gives some output to node #5. The overall output of this system is the OUT element, which reads from #5.
Here is the file that specifies the topology:
3 (number of elems)
IN 0 0 3 (no inputs for input element obviously)
NOT 3 0 5 (reads from #3 and outputs to #5)
OUT 5 0 0 (reads from #5 and this is the end point of the system)
There can obviously be more elements, IN's and OUT's, but lets stick to this for the sake of simplicity.
And what i want to see as the result is: X-axis tells the simulation time (0 - 1000, step is 50), y axis tells the output value of each element in the system and the elements write their output one above the other, see this picture as an example.
Can you tell me how to create this kind of gnuplot script, that transforms the output of my application into the desired plot?
Thank you!
ok, I have found a solultion myself, here it is:
first, I had to transform the output of the app a bit, so that it looks like this:
0 1 2 4
49 1 2 4
50 1 2 4
99 1 2 4
100 0 2 4
149 0 2 4
150 0 3 5
199 0 3 5
200 1 3 5
249 1 3 5
250 1 2 4
299 1 2 4
300 0 2 5
349 0 2 5
350 1 3 5
399 1 3 5
400 0 2 4
449 0 2 4
450 1 3 5
499 1 3 5
the extra sim time steps make the edges look almost square, I also separated each column by 2 (added 0 to column #2, added 2 to column #3, added 4 to column #4 and so on), so that it is drawn one above each other and the simple command to plot this is:
plot 'out.txt' using 1:2 with lines, 'out.txt' using 1:3 with lines, 'out.txt' using 1:4 with lines
plus some set xtics, set ytics and other cosmetic stuff
now I have to deal with naming the lines with the names of the elements and voila.

gnuplot hypertext is not working

I have the below gnuplot script, I'm trying to display the third column when the mouse hover over a point of the plot.
set title "Cloud"
set xlabel "Date"
set ylabel "Number"
filename ='data.dat'
stats filename using 4 nooutput
set xdata time
set timefmt '%Y-%m-%d'
set format x '%Y'
rand_x(x) = x + 60*60*24*7 * (rand(0) - 0.5)
rand_y(y) = y + (rand(0) - 0.5)
set xrange [ "1995-01-19":"2013-12-12" ]
plot for [i=0:int(STATS_max)-1] filename \
using (rand_x(timecolumn(1))):(i < $4 ? rand_y($2) : 1/0):3 pointtype 7 linecolor palette notitle
u 0:1:2 with labels hypertext point pt 7 ps var lc rgb "#ffee99"
And the data file looks like below:
1999-01-19 21 0 1
2009-07-01 0 1 1
2008-08-20 2 1 1
2008-12-18 1 1 1
2004-05-12 4 1 1
2009-07-29 2 1 1
2008-08-07 0 1 1
2006-03-08 1 1 1
2004-08-31 9 1 1
2001-03-27 12 1 1
2009-08-19 0 1 1
2010-07-14 2 1 1
2009-06-24 0 1 1
2009-11-11 0 1 1
2010-10-13 0 1 1
2012-02-22 0 1 1
2011-05-11 0 1 1
2011-03-03 0 1 1
2011-09-21 0 1 1
2011-12-20 0 1 1
2011-10-05 0 1 1
2012-05-03 0 1 1
2011-10-05 0 2 1
2013-01-09 0 2 1
2011-06-03 0 2 1
So can you please tell me what's wrong with my script?
Thanks.
First a remark for the readers: hypertext works only with the 4.7 development version.
To your problem: For plotting the labels, you must also use the same x and y columns 1 and 2 (you use 0 and 1). And you need the third column for the labels and a fourth one for the ps var. So your plot part for the labels is:
plot for [i=0:int(STATS_max)-1] filename \
using (rand_x(timecolumn(1))):(i < $4 ? rand_y($2) : 1/0):3 pointtype 7 linecolor palette notitle,\
'' u 1:2:3:3 with labels hypertext point pt 7 ps var lc rgb "#ffee99"

gnuplot | 3D layers

I have the below data file which has:
1st column is the layer number.
2nd column is the X axis.
3rd column is the Y axis.
1 1999-01-19 21 0 1
1 2009-07-01 0 1 1
1 2008-08-20 2 1 1
1 2008-12-18 1 1 1
2 2004-05-12 4 1 1
2 2009-07-29 2 1 1
3 2008-08-07 0 1 1
4 2006-03-08 1 1 1
4 2004-08-31 9 1 1
4 2001-03-27 12 1 1
My questions:
1. How can I plot the above data file in 3D knowing that each layer must have different Z offset and different color?
the below must be plotted with Z=1
1 1999-01-19 21 0 1
1 2009-07-01 0 1 1
1 2008-08-20 2 1 1
1 2008-12-18 1 1 1
and the below with Z=2
2 2004-05-12 4 1 1
2 2009-07-29 2 1 1
and so on.
2.If I want to select the layer number 2, other layers must be shaded with gray and this layer must be colored with red for example, is that possible? so it's like highlighting the selected layer.
thx.
To plot the points just use
set xdata time
set timefmt '%Y-%m-%d'
set format x '%Y'
splot 'data.dat' using 2:3:1
That uses the layer number as z-value. To get something else, just specify a function for the z-value depending on the layer number:
zpos(z) = 1 + 0.5*z
splot 'data.dat' using 2:3:(zpos($1))
For the coloring use linecolor rgb variable. That allows you to specify the color in the last column. This color must be the integer representation of an rgb-tuple which is 65536*red + 256*green + blue, with red, green and blue being in the range [0:255].
The following script plots the points in layer 2 in dark red:
set xdata time
set timefmt '%Y-%m-%d'
set format x '%Y'
rgb(r,g,b) = 65536*r + 256*g + b
gray = rgb(200,200,200)
red = rgb(200,0,0)
layer = 2
set view 66,20
splot 'data.dat' using 2:3:1:($1 == layer ? red : gray) with points pt 7 linecolor rgb variable notitle
The result with 4.6.4 is:

Resources