gnuplot - conditional colors in a points plot - colors

I have the following data:
ClockIndex Max AvgStd Avg Num Threshold
"ck1 (1.54 GHz)" 35 +16.30 11 11583 X
"ck2 (1.54 GHz)" 28 +16.66 12 10669 -
"ck3 (1.54 GHz)" 29 +14.47 9 8036 -
"ck4 (1.54 GHz)" 35 +18.99 12 5685 -
"ck5 (1.54 GHz)" 9 +6.04 3 11 -
I'm plotting columns 2, 3, 4 together in a points plot, one line per entry.
Using this code:
set xtics rotate
set xlabel ""
set ylabel "Levels"
set title "Levels - foo"
set key autotitle columnhead
set term png medium size 1200,600
set grid
set output "foo.png"
plot "foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "blue",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black
**Goal : If there's a "X" in the "Threshold" column, I want the "Max" point to be green, not blue.
I've tried using awk, but to no avail.
plot "< awk '{if($6 == \"X\") print }' foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "blue",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black"
"< awk '{if($6 == \"-\") print }' foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "green",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black"
I keep getting errors like:
"foo_gnuplot", line 11: warning: Skipping data file with no valid points
Any help would be greatly appreciated! Thanks much for taking a look! :)

You can do it like this:
plot "foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "blue",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black",\
"" using (strcol(6) eq "X" )?($2):(1/0):xticlabels(1) with points pt 13 ps 2 lt rgb "green"
(condition)?($plotThisValue):(1/0) is a common gnuplot technique for conditional plotting.
With that code the original point is overprinted with the green one. (If you want to remove the original (now hidden) blue point completely you can use the same technique in line one of your plot command.)

Thanks much to havogt for the great answer! Here's what I ended up doing:
plot "foo.rpt" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black",\
"" using (strcol(6) eq "X" )?($2):(1/0):xticlabels(1) with points pt 13 ps 2 lt rgb "green"
"" using (strcol(6) eq "-" )?($2):(1/0):xticlabels(1) with points pt 13 ps 2 lt rgb "blue"
Works great! Really appreciate the help!

Related

gnuplot: how to set the different fill and border colors for `with points`?

I would like to achieve a similar effect in gnuplot.
Here is what I tried:
unset key
set style line 11 lc rgb '#808080' lt 1
set border 3 ls 11
set tics nomirror
set grid
set style line 1 lc rgb '#808080' pt 9 ps 3
set style line 2 lc rgb '#808080' pt 20 ps 3
set style line 3 lc rgb '#BD3828' pt 7 ps 3
set yrange [4:9]
$data << EOD
5 5.1
5.3 6.8
6 6
EOD
$data2 << EOD
5 5
7 7
8 6
EOD
$data3 << EOD
5.5 7
6 6
7 7.1
EOD
plot $data u 1:2 w points ls 1, $data2 u 1:2 w points ls 2, \
$data3 u 1:2 w points ls 3
As we can see, points can be overlapped. Then how can we darken the overlap areas?
A possible solution is to set transparency (e.g., lc rgb '#80808080'), but it will also make both border and filling transparent. So how to set the different fill and border colors for with points?
Another solution is to use set object, but we need to do more work to read data from files.
I think the closest you could come to what you describe is to draw the points in two passes.
First pass: draw using a point type that produces only the outlines (point types N = 4 6 8 10 12 ...).
Second pass: draw using the corresponding point type N+1 that produces only the interior, using the same color but adding an alpha channel value to make it partially transparent.
set print $RAND1
do for [i = 1:50] { print rand(0), rand(0) }
unset print
set print $RAND2
do for [i = 1:50] { print rand(0), rand(0) }
unset print
set pointsize 4
plot $RAND1 with points pt 8 lc rgb "#00b8860b", \
'' with points pt 9 lc rgb "#AAb8860b", \
$RAND2 with points pt 6 lc rgb "#00c04000", \
'' with points pt 7 lc rgb "#AAc04000"

How to make "filledcurves" change color when two or more meet? (GNUPlot)

I have created a graph that uses lines for the average of my data and filledcurves for the error (average-error, average+error). My problem is that the last filledcurves that is plotted, covers all other fills, even though I have set its transparency. I want to change this so that when two or more filledcurves meet the color is changed and thus the errors are clearly shown for all lines.
My script is:
set datafile separator whitespace
set style line 1 lc rgb '#aa5500' lt 1 lw 2 pt 7 ps 1
set style line 2 lc rgb '#55aaff' lt 1 lw 2 pt 7 ps 1
set style line 3 lc rgb '#aa557f' lt 1 lw 2 pt 7 ps 1
set style line 4 lc rgb '#55007f' lt 1 lw 2 pt 7 ps 1
set style line 5 lc rgb '#005500' lt 1 lw 2 pt 7 ps 1
set style line 6 lc rgb '#0055ff' lt 1 lw 2 pt 7 ps 1
set tics nomirror
set tics front
set style line 12 lc rgb '#808080' lt 0 lw 1
set grid front ls 12
set output 'network_utilization_servers_3_2.png'
set terminal png size 1000,800
set style fill transparent solid 0.25
set style fill noborder
set xrange[0:77]
set yrange[0:1500]
set xlabel "Time (seconds)" font ",12"
set ylabel "Incoming Traffic (KB/s)" font ",12"
set title "Network Utilization in the Servers" font "Helvetica,16"
set key box title "Server" width 10
plot for [i=0:|Group|-1] $Data3 u ($2-6):($3-$4):($3+$4) index i with filledcurves ls i+1 notitle, \
for [i=0:|Group|-1] $Data3 u ($2-6):3:4 index i w lines ti sprintf("%g",Group[i+1]) ls i+1
and my pragh:
My goal is something like this:
Is there a way to achieve this?

how to define X's label on gnuplot

This is My data :
18_AGT_s 8234.00 8234.00 8234.00
18_MAC_s 8414.36 8308.36 8246.33
9_MAC_r 8414.36 8308.36 8246.33
9_MAC_s 8414.55 8309.55 8246.45
8_MAC_r 8414.55 8309.55 8246.45
8_MAC_s 8414.56 8310.08 8246.47
6_MAC_r 8414.56 8310.08 8246.47
6_MAC_s 8416.19 8310.21 8246.49
1_MAC_r 8416.19 8310.21 8246.49
and here is my gnuplot code :
plot "dat" using ($0+1):2 with linespoints pt 8 ps 2 lt 2 lw 4 lc rgb
"green" title "DMSR","dat" using ($0+1):3 with linespoints pt 5 ps 2
lt 3 lw 4 lc rgb "blue" title "Alarm","dat" using ($0+1):4 with
linespoints pt 6 ps 2 lt 4 lw 4 lc rgb "red" title "Emergency"
and here is my out put :
But In the step part I want to have 18_AGT_s and 18_MAC_s and 9_MAC_s and .... for example on the X part I want to have 18_AGT_s in stand of 1 or I want to have 18_MAC_2 instand of 2 and 9_MAC_r instand of 3 etc. Any help thanks
Use the xticlabels() option with the column number with the labels as argument (1, in this case):
# Optionally rotate labels so they fit
set xtics rotate
plot "dat" using ($0+1):2 with linespoints pt 8 ps 2 lt 2 lw 4 lc rgb \
"green" title "DMSR","dat" using ($0+1):3 with linespoints pt 5 ps 2 \
lt 3 lw 4 lc rgb "blue" title "Alarm","dat" using \
($0+1):4:xticlabels(1) with linespoints pt 6 ps 2 lt 4 lw 4 lc rgb \
"red" title "Emergency"
Here you only need to use it for the last plot instance so that it overwrites the number options.

Gnuplot 1d plot

I have a text file of the position of particles along the x axis which changes after every collision. Example data.
0 7.5 10 30 30 40
0 9.375 10 32.5 40 40
0 10 10 33.3333 36.6667 40
0 10.25 10.75 34 34 40
0 11.0938 13.2812 28.75 40 40
I am currently trying to plot the data using gnu plot. What I want it to do is have these points along the x axis but instead of plotting the whole file at once I would like gnu plot to plot one line at a time . Furthermore, so the data is identifiable I am trying to plot the points as large markers instead of points. I am struggling to do this and any help would be appreciated.
Firstly, convert the rows to columns using AWK
awk '{for(i=1;i<=NF;i++)a[NR,i]=$i}END{for(i=1;i<=NF;i++){for(j=1;j<=NR;j++)printf a[j,i]"\t";printf "\n"}}' original.dat > particle.dat
#suppose that your input data is original.dat and the output data is particle.dat
The converted data are:
0 0 0 0 0
7.5 9.375 10 10.25 11.0938
10 10 10 10.75 13.2812
30 32.5 33.3333 34 28.75
30 40 36.6667 34 40
40 40 40 40 40
Then, plot your data with the following code in gnuplot:
set border 1
#`set border 1` means only showing the bottom border of the plot. see `help border` for more information
set xtics nomirror
#only show the bottom tics on the x axis and suppress the upper tics of the x axis
unset ytics
#suppress the tics on the y axis
set key outside
#set the legend out side the plot
plot "particle.dat" using 1:(1) with points pointtype 7 pointsize 3 title "particle 1", "" u 2:(2) w p pt 7 ps 3 t "particle 2", "" u 3:(3) w p pt 7 ps 3 t "particle 3", "" u 4:(4) w p pt 7 ps 3 t "particle 4", "" u 5:(5) w p pt 7 ps 3 t "particle 5"
#`using 1:(1)` means use the first column as X and a constant number of 1 as Y. see `help using` for more details
#`u` is short for `using`and `w p pt 7 ps 3` is short for `with points pointtype 7 pointsize 3.
The output of the plot is
I don't think that you have to transpose the data using awk, as each row already contains the data of a single particle.
So, based on the code from DragonHu, I have this:
To generate this plot, I also added lines to connect the points. Also, I used the special column number 0 which just gives the line number in the datafile, starting at 0.
Another trick: Using backslash \, you can split a command to multiple lines. Here is the plot command I used:
plot "particle.dat" using 1:0 with points linetype 1 pointtype 7 pointsize 3 title "particle 1",\
"" u 1:0 notitle w l lt 1,\
"" u 2:0 w p lt 2 pt 7 ps 3 t "particle 2", \
"" u 2:0 notitle w l lt 2,\
"" u 3:0 w p lt 3 pt 7 ps 3 t "particle 3", \
"" u 3:0 notitle w l lt 3,\
"" u 4:0 w p lt 4 pt 7 ps 3 t "particle 4", \
"" u 4:0 notitle w l lt 4,\
"" u 5:0 w p lt 5 pt 7 ps 3 t "particle 5",\
"" u 5:0 notitle w l lt 5
Still, this is not yet the answer, as the question is to plot one set of points at a time. This can be achieved with the following code. It generates five single plots which I dumped into an animated gif figure:
set key center
set yrange[0:1]
set xrange[0:40]
set terminal gif size 600, 200 animate delay 100
set output "animated.gif"
do for [n=0:4] {
set title sprintf("Lineno. %d", n)
plot "particle.dat" every ::n::n using 1:(0) with points pointtype 7 pointsize 3 title "particle 1",\
"" every ::n::n u 2:(0) w p pt 7 ps 3 t "particle 2", \
"" every ::n::n u 3:(0) w p pt 7 ps 3 t "particle 3", \
"" every ::n::n u 4:(0) w p pt 7 ps 3 t "particle 4", \
"" every ::n::n u 5:(0) w p pt 7 ps 3 t "particle 5",\
}
unset output
If single images should be created, it is possible via
set terminal ongcairo
do for [n=0:4] {
set title sprintf("Lineno. %d", n)
set output sprintf("PictureNumber_%d",n)
plot ...
unset output
}

Plot with shaded regions between lines

I have the 2 files from either of them I'll plot a point line using the following code:
set terminal postscript eps color solid font "Helvetica, 22"
set size ratio 0.625
set output "example.eps"
set key right top
plot "traffic.txt" using 1:2 title "traffic" with lp pt 7 ps 1 lc rgb "red", \
"solar.txt" using 1:($2*100) title "solar" with lp pt 9 ps 1 lc rgb "blue"
Either line forms a region together with x axis and the regions formed by both lines overlaps. I was wondering how may I shade the overlapping parts.
Thanks!
(The files used are as follows)
File 1
1 66.660000
2 47.830000
3 39.270000
4 27.940000
5 24.990000
6 27.930000
7 32.060000
8 43.650000
9 70.470000
10 73.430000
11 87.690000
12 111.790000
13 122.170000
14 114.930000
15 111.620000
16 109.330000
17 121.370000
18 118.600000
19 132.890000
20 132.480000
21 148.360000
22 152.260000
23 140.510000
24 99.120000
File 2
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0.121933
10 1.81455
11 2.25622
12 2.67994
13 2.87834
14 2.53149
15 1.29541
16 0.57571
17 0.0883007
18 0
19 0
20 0
21 0
22 0
23 0
24 0
You can use the filledcurves plotting style. For that all data must be contained in one file. You can combine the files on-the-fly e.g. with paste. For a platform-independent solution with python look e.g. this answer.
With filledcurves you can also distinguish between above and below in order to use different colors:
set key right top
set autoscale xfix
set xtics 4
plot "< paste traffic.txt solar.txt" using 1:2:($4*100) with filledcurves below lc rgb "#ffaaaa" t '', \
"" using 1:2:($4*100) with filledcurves above lc rgb "#aaaaff" t '',\
"traffic.txt" using 1:2 title "traffic" with lp pt 7 ps 1 lc rgb "red", \
"solar.txt" using 1:($2*100) title "solar" with lp pt 9 ps 1 lc rgb "blue"
The result with 4.6.4 is:
To shade only the region where the two curves overlap, you need a workardound. First shade the region between the narrower curve and the x1 axis, and then overwrite parts of that shading with white:
set autoscale xfix
set xtics 4
plot "< paste traffic.txt solar.txt" using 1:($4*100) with filledcurves x1 lc rgb "#ffaaaa" t '', \
"" using 1:2:($4*100) with filledcurves below lc rgb "white" t '',\
"traffic.txt" using 1:2 title "traffic" with lp pt 7 ps 1 lc rgb "red", \
"solar.txt" using 1:($2*100) title "solar" with lp pt 9 ps 1 lc rgb "blue"
This gives:

Resources