How to plot an histogram and a constant line from different files - plot

I'm new to gnuplot and I have a problem:
I have 2 different text files, the first one (file1.txt) is something like this:
Switch,Port,BPS
S1,1,5464091.33
S1,3,5465677.33
S2,2,5463298.00
S2,3,5462729.67
S3,1,5461340.67
S3,3,5461772.33
and I plot "file1.txt" with an histogram:
plot avg_file using 3:xticlabels(stringcolumn(1)."-".stringcolumn(2)) title columnheader
this works fine.
Now I have file2.txt which contains a single value:
AVG_BPS
4844714.81
I would like to plot this value as a constant horizontal line over the previous graph (histogram).
This is my complete script:
# Terminal definition (PNG image)
set term png
# Settings
set datafile separator ","
set output 'myplot.png'
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set boxwidth 1
# Graph and Axis titles
set title "BPS"
set xlabel "Switch-Port"
set ylabel "bits-per-second"
# Plot
plot "file1.txt" using 3:xticlabels(stringcolumn(1)."-".stringcolumn(2)) title columnheader
Here is myplot.png:
Thanks

probably the best is to do a stats:
stats 'file2.txt' u 1
hor_value=STATS_min
and then add to your plot:
plot "file1.txt" using 3:xticlabels(stringcolumn(1)."-".stringcolumn(2)) title columnheader, hor_value
or alternatively put a line on top (before the last plot):
set arrow nohead from graph 0, hor_value to graph 1, hor_value front

Related

rangelimited x-axis gnuplot disappears

I am trying to make a figure in gnuplot, similar to the bottom one on this page. Specifically, I only want an x- and y-axis on my figure at the places where there is data plotted.
For the y-axis, this works very well using the ytics rangelimited option. However, when I use xtics rangelimited on the x-axis, the x-axis border disappears:
Even more strangely, when I remove the set border option, there appears a diagonal line on my graph:
The minimal working example to make these figures is:
set terminal epslatex standalone size 8.0 cm, 8.0 cm
set output "test.tex"
# Define function
f(x) = abs(x)< 1 ? x**2 : NaN
set border 1+2 front #comment this out to get the diagonal line
set xrange [-1.5:1.5]
set xtics (-1.0,0,1.0) rangelimited nomirror
set yrange [-0.25: 1.25]
set ytics rangelimited nomirror
# plot line
plot f(x) with lines
I would like to get for the x-axis a similar result as for the y-axis. Does anyone know how to solve this problem?
As a side-question: I would like to have the 0 displayed on the y-axis as well. Is there any automatic way to do this? Or should I always explicitly tell gnuplot to put a tic at 0?
The rangelimited x axis is only drawn to span data points. You have no data points because you are plotting a function. In order to have the plot behave as a data plot rather than as a function plot you can use the following convention:
plot '+' using 1:(f($1)) with lines
See gnuplot documentation for "plot special-filenames"

Gnuplot: how to plot single points with custom xlabels

my problem is the following, I have a text file like that:
#X,#Y
1,1.00182349
2,4.024570000000001
3,8.73499
What I want to get is a plot like that:
where, basically, each y value is plotted as distinct point and on the x-axis my label are N,L,H.
I made that picture with the plot command in octave, but I'm forced to do that with gnuplot.
Please, can someone tell me how to do that?
Thanks.
You can specify arbitrary label for the tic labels:
set datafile separator comma
set logscale y
set xtics ("N" 1, "L" 2, "H" 3)
plot "test.dat" using 1:2:1 notitle with points linecolor variable pointtype 5 pointsize 2

gnuplot: Row stacked bar graph with error bar

I am new to gnuplot. I want to generate graph from data points with three components and standard deviation.
My data looks like this:
TYPE1 15 20 65 5
TYPE2 20 20 60 4
TYPE3 10 30 60 6
TYPE4 30 30 40 5
I want to plot a rowstacked bar for each TYPE with the 3 components stacked and an errobar at the top.
I wrote the following script to do this:
set terminal png
set output "sample.png"
set boxwidth 0.75 relative
set style fill pattern 0 border
set style histogram rowstacked
set style data histograms
set xtics 1000 nomirror
set ytics 100 nomirror
set noytics
set mxtics 2
set mytics 2
set ytics 100
set yrange [0:150]
set ylabel "Y"
set xlabel "X"
set title "Sample graph"
plot 'data.dat' using (100*column(2)/(column(2)+column(3)+column(4))) t "A" , '' using (100*column(3)/(column(2)+column(3)+column(4))) t "B" , '' using (100*column(4)/(column(2)+column(3)+column(4))):xtic(1) t "C"
This produced a graph which looks like this: .
But I am not able to get the errorbar on the top of each bar with deviation values in column 5. I tried different ways using rowstacked and errorbar styled bar graphs but had no luck.
For this you must know, that with the histogram style the boxes are placed at the x-positions 0, 1, etc. i.e. at the row number.
So for the errorbars you must use column(0) as x-coordinate:
set terminal pngcairo
set output "sample.png"
set boxwidth 0.75 relative
set style fill pattern 0 border
set style histogram rowstacked
set style data histograms
set yrange [0:150]
set macros
scale = '100/(column(2)+column(3)+column(4))'
set bars 2.0
plot 'data.dat' using ($2 * #scale):xtic(1) t "A" , \
'' using ($3 * #scale) t "B" , \
'' using ($4 * #scale) t "C",\
'' using 0:(100):5 with errorbars notitle lw 2 lt -1
The result with 4.6.3 is:
For convenience I used a macro scale. The macros work as follows: You define a string, like scale = '...' in the script above. That can be used later in any expression as #scale (you must have set macros enabled). The content of the scale string is then replaced before the respective command is executed.

Plotting multiple plots on same page using gnuplot; trying to avoid text on the plot

I am trying to plot multiple plots on the same page of gnuplot postscript output. Although, I like the way the plots are looking, what can I do to avoid the text on the plots? (See the code below, and the plot too). If you cannnot see the text on the plot, it says: "'../binsam_Omidi.dat' using 5:($1==1?$6:1/0)"
reset
set term postscript eps color size 10,10
set output "../Results/phasespace_bins.ps"
set multiplot layout 4,5 # engage multiplot mode
set size square
set size ratio -1
plot '../binsam_Omidi.dat' using 5:($1==1?$6:1/0) w p ps 1 pt 7
set size 0.4,0.4 # set the size of the second plot in plot units
set origin 0.15,0.5 # set the origin for the second plot
plot '../binsam_Omidi.dat' using 5:($1==1?$6:1/0) w p ps 1 pt 7
unset multiplot
Thanks!
That is the legend (key) entry of the plot. You can specify your own title with the title option, like
plot x title 'my title'
If you don't specify your own title, the code of the plot command is used. This is what you see.
To have no key entry for a single plot use notitle or title ''. If you don't want to have any key at all use unset key.

How do you plot bar charts in gnuplot?

How do you plot bar charts in gnuplot with text labels?
Simple bar graph:
set boxwidth 0.5
set style fill solid
plot "data.dat" using 1:3:xtic(2) with boxes
data.dat:
0 label 100
1 label2 450
2 "bar label" 75
If you want to style your bars differently, you can do something like:
set style line 1 lc rgb "red"
set style line 2 lc rgb "blue"
set style fill solid
set boxwidth 0.5
plot "data.dat" every ::0::0 using 1:3:xtic(2) with boxes ls 1, \
"data.dat" every ::1::2 using 1:3:xtic(2) with boxes ls 2
If you want to do multiple bars for each entry:
data.dat:
0 5
0.5 6
1.5 3
2 7
3 8
3.5 1
gnuplot:
set xtics ("label" 0.25, "label2" 1.75, "bar label" 3.25,)
set boxwidth 0.5
set style fill solid
plot 'data.dat' every 2 using 1:2 with boxes ls 1,\
'data.dat' every 2::1 using 1:2 with boxes ls 2
If you want to be tricky and use some neat gnuplot tricks:
Gnuplot has psuedo-columns that can be used as the index to color:
plot 'data.dat' using 1:2:0 with boxes lc variable
Further you can use a function to pick the colors you want:
mycolor(x) = ((x*11244898) + 2851770)
plot 'data.dat' using 1:2:(mycolor($0)) with boxes lc rgb variable
Note: you will have to add a couple other basic commands to get the same effect as the sample images.
plot "data.dat" using 2: xtic(1) with histogram
Here data.dat contains data of the form
title 1
title2 3
"long title" 5
I would just like to expand upon the top answer, which uses GNUPlot to create a bar graph, for absolute beginners because I read the answer and was still confused from the deluge of syntax.
We begin by writing a text file of GNUplot commands. Lets call it commands.txt:
set term png
set output "graph.png"
set boxwidth 0.5
set style fill solid
plot "data.dat" using 1:3:xtic(2) with boxes
set term png will set GNUplot to output a .png file and set output "graph.png" is the name of the file it will output to.
The next two lines are rather self explanatory. The fifth line contains a lot of syntax.
plot "data.dat" using 1:3:xtic(2) with boxes
"data.dat" is the data file we are operating on. 1:3 indicates we will be using column 1 of data.dat for the x-coordinates and column 3 of data.dat for the y-coordinates. xtic() is a function that is responsible for numbering/labeling the x-axis. xtic(2), therefore, indicates that we will be using column 2 of data.dat for labels.
"data.dat" looks like this:
0 label 100
1 label2 450
2 "bar label" 75
To plot the graph, enter gnuplot commands.txt in terminal.
I recommend Derek Bruening's bar graph generator Perl script. Available at http://www.burningcutlery.com/derek/bargraph/
You can directly use the style histograms provide by gnuplot. This is an example if you have two file in output:
set style data histograms
set style fill solid
set boxwidth 0.5
plot "file1.dat" using 5 title "Total1" lt rgb "#406090",\
"file2.dat" using 5 title "Total2" lt rgb "#40FF00"

Resources