How to plot 3 columns in GNUplot? - gnuplot

I have files looking like this:
Number Data1 Data2
1 9.10 4.022
2 15.27 3.996
3 21.92 4.004
4 21.19 4.026
5 20.67 4.022
6 20.99 4.000
7 19.80 4.004
8 20.01 3.931
9 20.18 4.004
10 19.78 4.007
I want to plot Number in X axes, Data1 in leftY and Data2 in rightY, but I can not figure out how to do it.
Thanks

Just a brief annotated sample, using your data saved in a file so.dat:
# Set ticks for 2nd y axis
set y2tics
# We don't want to see the left ticks on the right axis
set ytics nomirror
# Set ranges so that the data points are not on the axis
set xrange [0:11]
set yrange [8:23]
set y2range[3.95:4.05]
# use first line of the file for labels
set key autotitle columnhead
# display key in least busy area
set key bottom right
# Title and axis labels
set title "Nice Try"
set xlabel "Number"
set ylabel "Data1"
set y2label "Data2"
plot "so.dat" using 1:2 axes x1y1 with points pointsize 2,\
"" u 1:3 axes x1y2 w p ps 2 pointtype 6
One can do a lot more decoration etc. but I think this is the essence of what you want. The graph produced:

Related

Show error bars in a multiaxis plot in Gnuplot

I have a dataset (show-errorbar.dat) containing:
Model# DE IE Error
Apple -4.6 -128.9538 4.0
Huawei -5.2 -176.6343 5.3
One-Pro -5.2 -118.1106 3.2
#!/usr/bin/gnuplot
#set terminal pdfcairo enhanced color font 'Helvetica,12' linewidth 0.8
set terminal png
set output 'BrandError.png'
set boxwidth 1.0 relative
set bmargin 5
set style fill solid border -1
set xtic rotate by -45 scale 0
#set auto x
set style line 81 lt 0 lc rgb "#808080" lw 0.5
set grid xtics
set grid ytics
set grid mxtics
set grid mytics
set grid back ls 81
set arrow from graph 0,first -4.6 to graph 1, first -4.6 nohead lw 2 lc rgb "#000000" front
set border 11
set border lw 2.0
set xtics font ",11"
set ytics font ",14"
set tics out
set ytics nomirror
set y2tics
set y2tics font ",14"
set mxtics 10
set mytics 2
set my2tics 2
set yrange [-10:0]
set y2range [-260:0]
set key left bottom
set y2label offset -2
set ylabel offset 2
set ylabel 'DE' tc rgb "red"
set y2label 'IE' tc rgb "green"
set style data histograms
set style histogram cluster gap 2
set linetype 2 lc rgb 'red'
set linetype 3 lc rgb 'yellow'
set linetype 4 lc rgb 'green'
plot 'show-errorbars.dat' using 2 ti 'DE' lc 2 axis x1y1, '' u 3:xticlabels(1) ti 'IE' lc 4 axis x1y2
set output
enter image description here
I would like to plot a histogram comparing DE vs IE and also show error bars (data in column 4) for the IE values.
Please any help on how to go about it.
There is a variant histogram style for exactly that purpose
set style histogram errorbars gap 2 {lw W}.
Here is the help section from the docs:
The `errorbars` style is very similar to the `clustered` style, except that it
requires additional columns of input for each entry. The first column holds
the height (y value) of that box, exactly as for the `clustered` style.
2 columns: y yerr bar extends from y-yerr to y+err
3 columns: y ymin ymax bar extends from ymin to ymax
The appearance of the error bars is controlled by the current value of
`set errorbars` and by the optional <linewidth> specification.
Updated answer
Notes:
You can't mix axis choice within a single histogram. So I have removed the axes x1y1 and axes x1y2 from the plot command. Since you have explicitly given the range for both y1 and y2, the plot border and labels are not affected.
However since the green bars are now being plotted against y1, we have to scale them so that the y2 axis labels apply. So the column 3 and column 4 values will be divided by 26, which is (y2 range) / (y1 range)
In "histogram errorbars" mode each plot component looks for an extra column of data to determine the size of the errorbar. Since your column 2 data has no corresponding column of errors, we dummy it up to use all a constant not-a-number (no data) value: (NaN)
Your data contains a line of columnheaders, which could confuse the program if it thinks this is a line of data. There are a number of ways you can tell the program to skip this line; I have used set key autotitle columnhead for convenience and because it is supported by old versions of gnuplot. If you have a current version it would be better to use instead set datafile columnheaders.
I have kept all of your commands except that the plot command is replaced by the following 3 lines:
set style histogram errorbars gap 2 lw 1.5
set key autotitle columnhead
plot 'show-errorbars.dat' using 2:(NaN) ti 'DE' lc 2, '' u ($3/26.):($4/26.):xticlabels(1) ti 'IE' lc 4

How to display y-labels on top of histogram bars on gnuplot

I desgined a histogram in gnuplot however the y-scale needs to be in log2 due to huge difference in values. Therefore, to improve readability of the plot I pretend to display the concrete values on top of each bar. The values represent bytes and so I would like for this values also be in log2 and to be formated to display kb, Mb, ... as is being done in the y-axis.
How can I achieve this?
This is the comands I'm currently using:
set terminal postscript eps enhanced dash color "" 13
reset
set datafile separator ","
set title "Bytes per Protocol"
set xlabel "Protocol"
set ylabel "Bytes" rotate by 90
set yrange [0:1342177280]
set logscale y 2
set format y '%.0s%cB'
set style data histogram
set boxwidth 0.5
set style fill solid
set xtics format ""
set grid ytics
set style data histogram
set style histogram clustered gap 2
set grid ytics
set tic scale 0
set size 1,0.9
set size ratio 0.5
set key autotitle columnhead
set output "ex_a_1_BIG.eps"
plot "ex_a_1_BIG.csv" using ($3):xtic(1) title "IN", \
'' using ($5):xtic(1) title "OUT", \
'' using 0:($3):($3) with labels center offset -2,1 notitle, \
'' using 0:($5):($5) with labels center offset 2,1 notitle
This is the content of the csv I want to plot (I only want the bytes in and out):
protocol,packets in,bytes in,packets out,bytes out
ICMP,1833,141562,979,60334
IGMP,0,0,283,14006
TCP,158214,129221151,130101,47734355
UDP,68476,9571677,72530,24310734
Check help format_specifiers and help gprintf. And the example below.
What is a bit unfortunate, that in gnuplot apparently the prefix for 1 to 999 is a single space instead of an empty string.
For example, with the format '%.1s %cB' this leads to two spaces for 1-999 B and one space for the others, e.g. 1 kB. However, if you use '%.1s%cB' this leads to one space for 1-999 B and no space for the others e.g. 100kB. As far as I know, correct would be one space between the number and the units. I'm not sure whether there is an easy fix for this.
Code:
### prefixes
reset session
$Data <<EOD
1 1
2 12
3 123
4 1234
5 12345
6 123456
7 1234567
8 12345678
9 123456789
10 1234567890
11 12345678901
12 123456789012
13 1234567890123
EOD
set boxwidth 0.7
set style fill solid 1.0
set xtics 1
set yrange [0.5:8e13]
set multiplot layout 2,1
set logscale y # base of 10
set format y '%.0s %cB'
plot $Data u 1:2 w boxes lc rgb "green" notitle, \
'' u 1:2:(gprintf('%.1s %cB',$2)) w labels offset 0,1 not
set logscale y 2 # base of 2
set format y '%.0b %BB'
plot $Data u 1:2 w boxes lc rgb "red" notitle, \
'' u 1:2:(gprintf('%.1b %BB',$2)) w labels offset 0,1 not
unset multiplot
### end of code
Result:
Addition:
a workaround for number/unit space issue at least for the labels in the graph would be:
myFmt(c) = column(c)>=1 && column(c)<1000 ? \
gprintf('%.1s%cB',column(c)) : gprintf('%.1s %cB',column(c))
and
plot $Data u 1:2 w boxes lc rgb "green" notitle, \
'' u 1:2:(myFmt(2)) w labels offset 0,1 not
But for the ytics labels I still don't have an idea.

gnuplot: string values xticlabel & adjusting fontsize

I have data I would like to plot in a histogram style with a "cumulated" curve on top. I have the following problem:
My data consists of one column with the categories ("discharge") and one column with the quantity of values ("probability") that belong to the respective category. The last value of the category-column is ">100" summarizing all power plants that have a bigger discharge than the last numeric value ("100 m^3/s"). I have not found a solution to plot this last category and the respective values with the command plot 'datafile.dat' using 1:2 with boxes ... because (as I assume) in this case only numerical values are read out for the x-ticlabels, so the last category is missing. If
I plot it with this command plot 'datafile.dat' using 2:xtics(1) with boxes ... I get the last category ">100" plotted just fine.
BUT: if I use the latter command the x-axis labels appear in the normal font size. Even though I have the line set format x '\footnotesize \%10.0f' in my code.
I have read about explicit labels in the plotcommand line that overwrite format style which was set before but was not able to adapt it to my code.
Changing ytic font size in gnuplot epslatex (multiplot)
Do you have an idea how to do this?
Excel screenshot to visualize what I want to achieve
'datafile.dat'
discharge probability cumulated
10 20 20%
20 10 10%
30 5 5%
40 6 6%
50 4 4%
60 12 12%
70 8 8%
80 15 15%
90 20 20%
100 6 6%
>100 4 4%`
[terminal=epslatex,terminaloptions={size 15cm, 8cm font ",10"}]
set xrange [*:*]
set yrange [0:20]
set y2range [0:100]
set xlabel 'Discharge$' offset 0,-1
set ylabel 'No. of power plants' offset 10.5
set y2label 'Cumulated probability' offset -10
set format xy '$\%g$'
set format x '\footnotesize \%10.0f'
set format y '\footnotesize \%10.0f'
set format y2 '\footnotesize \%10.0f'
set xtics rotate by 45 center offset 0,-1
set style fill pattern border -1
set boxwidth 0.3 relative
set style line 1 lt 1 lc rgb 'black' lw 2 pt 6 ps 1 dt 2
plot 'datafile.dat' using 1:2 with boxes axes x1y1 fs pattern 6 lc black notitle, \
'datafile.dat' using 1:3 with linespoints axes x1y2 ls 1 notitle
I am confused by your datafile; the numbers in the third column do not seem to be cumulative, and do not add up to 100%. Here is a solution that uses only the first two columns of your file:
set term epslatex standalone header "\\usepackage[T1]{fontenc}"
set output 'test.tex'
stats "datafile.dat" using 2
total = STATS_sum
set xlabel "Discharge" offset 0, 1.5
set xtics rotate
set ylabel "No. of power plants"
set ytics nomirror
set yrange [0:*]
set y2label "Cumulative probability"
set y2tics
set y2range [0:]
set boxwidth 0.3 relative
set style line 1 lt 1 lc rgb 'black' lw 2 pt 6 ps 1 dt 2
plot \
'datafile.dat' using 2:xtic("\\footnotesize " . stringcolumn(1)) with boxes axes x1y1 fs pattern 6 lc black notitle, \
'datafile.dat' using ($2/total) smooth cumulative with linespoints axes x1y2 ls 1 notitle
set output
The trick is to add the latex command \footnotesize in front of each label in the using command. It also first computes the total number of power plants so that it can compute probabilities, and computes cumulative values with the smooth cumulative option.

gnuplot: standard deviation and average

I have a graph with two lines. Now I want to mark standard deviation and average for the Y-points. Any idea ?
If I use "errorbars" option, it is not marking stddev or average.
Here is my script to plot graph:
#set xrange [0:]
set terminal png linewidth 2
#set output '| display png:-'
set output "result_macs.png"
set ylabel "Flows/sec"
set xlabel "MACS per Switch"
set grid
set format x "%.0f"
set format y "%.0f"
set style line 1 lt 1 lw 3 pt 1
set style line 2 lt 2 lw 3 pt 2
# put the key (legend) outside, right of the plot area,
# # vertically centered (as in your picture)
set key outside right center
set xtics("1000" 0, "10000" 1, "100000" 2, "1000000" 3, "10000000" 4)
plot "floodlight.data" using :2 title "Floodlight" with linespoints, "beacon.data" using :2 title "Beacon" with linespoints

gnuplot: Not enough columns for variable color

I am executing the following gnuplot script:
set title "Efficiency scatter plot"
set xlabel "perf_1"
set ylabel "secondary report"
set log x
set log y
set xrange [0.1:40.0]
set yrange [0.1:40.0]
set terminal png medium
set output "./graph1.png"
set size square
set multiplot
set pointsize 0.3
set style line 6 pt 6
set datafile separator ","
set border 3
set xtics nomirror
set ytics nomirror
plot '/tmp/data.csv' using 3:1 with points pt 1 lt 3 lc var title "perf_20140113131309", \
'/tmp/data.csv' using 3:2 with points pt 1 lt 1 lc var title "perf_1"
plot x notitle
plot 2*x notitle
plot 0.5*x notitle
obtaining the following error message
"script.gnuplot", line 20: Not enough columns for variable color
Could you please guide me in order to find what I am doing wrong.
By the way the gnuplot version is '4.6 patchlevel 3' the data.csv files used is
0.1,0.1,40.0
0.14,0.14,40.0
0.32,0.32,40.0
0.7,0.74,40.0
Thanks in advance!
That means, that you need to specify one more column in your using statement: The first one is the x-coordinate, the second one the y-coordinate. The one for the variable line color is missing.
Use e.g.
plot '/tmp/data.csv' using 3:1:0 with points pt 1 lt 3 lc var
to use the row number (zeroth column) as linetype index. You can also use e.g. linecolor palette so select the color from the currently defined color palette.

Resources