Gnuplot put xtics between bars - gnuplot

I need to be able to position the tic marks so that they are between the bars in the graph.
That way it can be read that there were 2 points between 0 and 14, 0 between 15 and 30, and so on, instead of the tics centered underneath the bars. Is there any way to automatically do that based on the box width?
Here is my current script attempt:
set xtics out nomirror
set ytics out nomirror
set tic scale 0
set style fill solid 1.00 border 0
set offset graph 0.1, 0.05, 0.1, 0.0
set boxwidth 15 *.9
set xtics offset -(15 *.9)/2
set term png
set output "graph.png"
plot 'data.dat' using 1:2:xtic(1) with boxes
Here is the .dat:
0 2
15 0
30 0
45 0
60 1
75 1
90 33
EDIT
It appears that the following works consistently, based on the boxwidth:
set bwidth=15*.9
set boxwidth bwidth
set xtics out nomirror offset -bwidth/2 left
There still might be a better way.

With your solution you only shift the tic labels. I would also shift the tics.
Here is my solutions:
set tics out nomirror
set style fill solid 1.00 noborder
set autoscale fix
set offset 5,5,5,0
# extract the first and second x-values
stats 'data.dat' using 1 every ::::1 nooutput
start = STATS_min
width = STATS_max - STATS_min
set boxwidth width *.9
set xtics start, width
set term pngcairo
set output "graph.png"
plot 'data.dat' using ($1+width/2.0):2 with boxes
The first and second data values are extracted automatically (requires version >= 4.6.0). These values are used to
Set the boxwidth
Set the xtics (start value and increment)
Shift the data points by half of the x-value increment.
See e.g. Gnuplot: How to load and display single numeric value from data file for another example for extracting data with the stats command. Instead of loading the data with stats you could of course also use
start = 0
width = 15
The result with 4.6.3 is:

Related

Gnuplot, skipping timedat tics, histogram

So, i need to make histogram of data by dates, but i have problem with xticlabel overlapping, so, i'm trying to find a solution how to skip xtics to avoid overlapping. Considering that dates are not integer tics, i was trying to solve it that way:
the .dat file
Time Dat 1 Dat 2
1 27-12-2016 12 2
2 28-12-2016 13 7
3 29-12-2016 17 2
4 30-12-2016 9 10
....
Is it possible to count xtic by first column, but show values in second column instead of values in first?
my code:
reset
dx=5.
n=2
total_box_width_relative=0.75
gap_width_relative=0.1
d_width=(gap_width_relative+total_box_width_relative)*dx/2.
d_box = total_box_width_relative/n
reset
set term png truecolor font "arial,10" fontscale 1.0 size 800,400
set output "test.png"
set datafile separator "\\t"
set title "Errors"
set print "-"
set xlabel 'x' offset "0", "-1"
set ylabel 'y' offset "1", "-0"
set key invert reverse Left outside
set key autotitle columnheader
set key samplen 4 spacing 1 width 0 height 0
set autoscale yfixmax
set yrange [0: ]
set xtics strftime('%d-%m-%Y', "27-12-2016"), 5, strftime('%m-%d-%Y', "15-01-2017")
set xtics font ", 7"
set ytics auto font ", 9"
set y2tics auto font ", 9"
set grid
set style data histogram
set style histogram cluster gap 1
set style fill transparent solid 0.75 noborder
set boxwidth 0.9 relative
set xtic rotate by -45 scale 0
plot 'datfile' u 3:xtic(strftime('%d-%m-%Y', strptime('%m.%d.%Y', stringcolumn(2)))), '' u 4
Before asking such a vague question, always reduce the script to a bare minimum which is required to reproduce the problem.
After removing all unnecessary stuff and fixing the plot command, here is what I end up with:
reset
set datafile separator "\t"
set yrange [0:*]
set style fill transparent solid 0.75 noborder
set boxwidth 0.9 relative
set xtic rotate by -45 scale 0
set key autotitle columnheader
set style data histogram
set style histogram cluster gap 1
plot 'file.dat' using 3:xtic(2) t col(2), '' using 4
Here, you already see one option to avoid overlapping of longer tic labels by rotating them.
Another possibility is to skip every n-th xticlabel. At this point you must understand how gnuplot creates histograms. Histograms don't use a conventional numerical axis, so you cannot simply use the dates as you normally would do when plotting lines. But gnuplot puts each bar cluster at an integer x-position and with e.g. xtic(2) you label every cluster with the string as given in the second column.
The expression xtic(2) is a short cut for xticlabel(2), which means xticlabel(stringcolumn(2)). Instead of using exactly the string in the second column, you can use here any expression which yields a string, including conditions. To only plot every second label check if the row number is even or odd with int($0) % 2 == 0 and use and empty string or the string from the second column:
plot 'file.dat' using 3:xtic(int($0)%2 == 0 ? stringcolumn(2) : '') t col(2), '' u 4

How can I divide the axis range? [duplicate]

I have a histogram with some small values and some very big values. How can I break the y-axis in two parts?
EDIT:
gnuplot sample:
set style histogram columnstacked
set style data histograms
set key autotitle columnheader
plot for [i=2:6] 'test.dat' using i
test.dat:
action device1 device2 device3
load 200 203 190 2 4
process 3 9 6 7 3
save 4 2 7 4 3
My answer is based on the example and comment on this website. In essence, you are looking for a broken y-axis, which can be achieved using the multiplot option:
reset
unset key
bm = 0.15
lm = 0.12
rm = 0.95
gap = 0.03
size = 0.75
kk = 0.5 # relative height of bottom plot
y1 = 0.0; y2 = 15.0; y3 = 180.0; y4 = 220.0
set style histogram columnstacked
set style data histograms
set key autotitle columnheader
set style fill solid 1.0 border -1
set multiplot
set border 1+2+8
set xtics nomirror
set ytics nomirror
set lmargin at screen lm
set rmargin at screen rm
set bmargin at screen bm
set tmargin at screen bm + size * kk
set yrange [y1:y2]
plot for [i=2:6] 'test.dat' using i
unset xtics
unset xlabel
set border 2+4+8
set bmargin at screen bm + size * kk + gap
set tmargin at screen bm + size + gap
set yrange [y3:y4]
plot for [i=2:6] 'test.dat' using i
unset multiplot
Remarks:
The first block of code specifies your plot size, as well as the two min/max for the y-axis range.
The second block of code sets your histogram style. I suggest you used filled columns (else, you might get the wrong colors where the plot is cut off by the y-range).
The third block starts you multiplot environment and sets up the lower plot (4th block).
The fifth block of code defines you upper plot.
To customize your plot, change the parameters in the first block (bm, lm, rm, gap, size, kk and yi)
And this is the result:

gnuplot, break y-axis in two parts

I have a histogram with some small values and some very big values. How can I break the y-axis in two parts?
EDIT:
gnuplot sample:
set style histogram columnstacked
set style data histograms
set key autotitle columnheader
plot for [i=2:6] 'test.dat' using i
test.dat:
action device1 device2 device3
load 200 203 190 2 4
process 3 9 6 7 3
save 4 2 7 4 3
My answer is based on the example and comment on this website. In essence, you are looking for a broken y-axis, which can be achieved using the multiplot option:
reset
unset key
bm = 0.15
lm = 0.12
rm = 0.95
gap = 0.03
size = 0.75
kk = 0.5 # relative height of bottom plot
y1 = 0.0; y2 = 15.0; y3 = 180.0; y4 = 220.0
set style histogram columnstacked
set style data histograms
set key autotitle columnheader
set style fill solid 1.0 border -1
set multiplot
set border 1+2+8
set xtics nomirror
set ytics nomirror
set lmargin at screen lm
set rmargin at screen rm
set bmargin at screen bm
set tmargin at screen bm + size * kk
set yrange [y1:y2]
plot for [i=2:6] 'test.dat' using i
unset xtics
unset xlabel
set border 2+4+8
set bmargin at screen bm + size * kk + gap
set tmargin at screen bm + size + gap
set yrange [y3:y4]
plot for [i=2:6] 'test.dat' using i
unset multiplot
Remarks:
The first block of code specifies your plot size, as well as the two min/max for the y-axis range.
The second block of code sets your histogram style. I suggest you used filled columns (else, you might get the wrong colors where the plot is cut off by the y-range).
The third block starts you multiplot environment and sets up the lower plot (4th block).
The fifth block of code defines you upper plot.
To customize your plot, change the parameters in the first block (bm, lm, rm, gap, size, kk and yi)
And this is the result:

Gnuplot : xtics - place strings at tics

I am new to gnuplot, and am trying to create a stacked histogram for a project. The problem I am running into is, that I am not able to put ticlabels on the x-axis (even if I could, they are not getting formatted in a neat way). My gp file is as follows:
Here is a snapshot of my data file:
CC P1-X P1-Y P2-X P2-Y
1 0.1097586 0.3262812 1.980848 5.9098402
2 0.1010986 0.2988812 0.9966702 5.8378412
3 0.4017474 0.7559452 4.41813 11.7404132
4 0.1028442 0.2974772 1.418744 6.0554552
5 0.1097316 0.3216112 1.967492 5.8007364
6 0.954794 0.3004874 0.9568386 5.778537
And here is my gp file:
set title "GCC compilation option by average execution time as stacked histogram"
set terminal jpeg medium
set output "histosmalldata.jpeg"
set boxwidth 0.9 absolute
set style fill solid 1.00 border -1
set key autotitle columnheader
set key outside right top vertical Left reverse enhanced autotitles columnhead nobox
set key invert samplen 4 spacing 1 width 0 height 0
set style histogram rowstacked title offset character 0, 0, 0
set style data histograms
set xtics border in scale 1,0.5 nomirror rotate by -45 offset character 0, 0, 0
set xtics norangelimit
set xtics ("O2-ffast-math-finline-functions" 1, "O2-funroll-loops-march=barcelona-ffast-math-finline-functions" 2, "GCCFLAGS_O0" 3, "O2-ftree-vectorize-funroll-loops-march=barcelona" 4, "GCCFLAGS_O2" 5, "O2-ftree-vectorize-funroll-loops-ffast-math" 6)
set xtics 1,6 nomirror
set ytics 0,100 nomirror
set ytics 1
set yrange [0:20]
set ylabel "Time"
set xlabel "GCC Compiler Options"
plot 'smalldata' using 2:xtic(1) ti col, '' using 3 ti col, '' using 4 ti col, '' using 5 ti col
This is the image of the graph:
Now, in the x axis, I am having 1,2,3 - 6 which I don't want, instead, I would want "O2-ffast-math-finline-functions" for 1 and so on in a neat formatted way.
I wrote this script after consulting some examples from gnuplot page and do not have a good understanding of some of the verbs, so apart from the solution, any general comments are welcome.
Thank you,
Sayan
You should not overwrite your xtics settings after you specified what you want.
Put all options into a single set command:
set xtics border in scale 1,0.5 nomirror rotate by -45 offset character 0, 0, 0\
norangelimit\
("O2-ffast-math-finline-functions" 1,\
"O2-funroll-loops-march=barcelona-ffast-math-finline-functions" 2,\
"GCCFLAGS_O0" 3, "O2-ftree-vectorize-funroll-loops-march=barcelona" 4,\
"GCCFLAGS_O2" 5, "O2-ftree-vectorize-funroll-loops-ffast-math" 6)
Note that you can escape the newline with a backslash as the last character of the line.
I figured out, that I do not actually need to specify ticlabels because that information is in my source file.
So I modified my source file like:
set title "GCC compilation option by average execution time as stacked histogram"
set terminal png size 1500,1200 font "/Library/Fonts/Times New Roman Bold.ttf, 13"
set output 'hist_gcc_1.png'
set boxwidth 0.9 absolute
set style fill solid 1.00 border -1
set key outside right top vertical Left reverse enhanced autotitles columnhead nobox
set key invert samplen 4 spacing 1 width 0 height 0
set style histogram rowstacked title offset character 0, 0, 0
set datafile missing '-'
set style data histograms
set xtics border in scale 1,0.5 nomirror rotate by -45 offset character 0, 0, 0 norangelimit
set ytics 0,100 nomirror
set ytics 1
set yrange [0:20]
set ylabel "Time"
set xlabel "GCC Compiler Options"
plot 'data' using 2:xticlabels(1) ti col, '' using 3 ti col, '' using 4 ti col, '' using 5 ti col
My dat file looks like:
CC P1-X P1-Y P2-X P2-Y
O2-ffast-math-finline-functions 0.1097586 0.3262812 1.980848 5.9098402
O2-funroll-loops-march=barcelona-ffast-math-finline-functions 0.1010986 0.2988812 0.9966702 5.8378412
GCCFLAGS_O0 0.4017474 0.7559452 4.41813 11.7404132
...

GNUplot: data labels are printed on top of each other when a multiplot is created

I have created a plot made up of four subplots; each subplot is a bar chart. Above the smaller bars I want to print how many units on the y-scale the bar represents. To do this I use 'set label', which works fine if I create individual files for the subplots, but not if I use multiplot. In this case the labels are successively printed on top of each other (i.e. those of the first subfigure also appear in the second, etc.).
Here is a truncated version of my gnuplot script:
set terminal postscript eps size 26cm,16cm font "Helvetica,18"
set out 'all_Figures.eps'
set multiplot
set multiplot layout 2,2
set bars fullwidth
set data style boxes
set boxwidth 0.5
set style fill solid 1.0 border -1
set border 3 front linetype -1 linewidth 1.000
set xtics border in scale 0,0.5 nomirror norotate offset character 0, 0, 0
set ytics border in scale -1,0 nomirror norotate offset character 0, 0, 0
set nogrid
set datafile separator ","
# ** First Plot **
set label "36" at first 2, 130 center
set label "86" at first 3, 160 center
set size .4,.3
plot 'allPDB_perc.csv' using 2:xticlabels(1) notitle
# ** Second Plot **
set size .4,.3
set label "10" at first 3, 236 center
set label "3" at first 4, 236 center
plot 'allPDB_num_dom.csv' using 2:xticlabels(1) notitle
unset multiplot
Is someone able to tell me how to clear the previous subfigure's data labels prior to generation of the current labels? Thanks a lot in advance!
Oh dear >_< I simply had to unset the labels after plotting, like so:
# ** Plot 1 **
set label ...
plot 'datafile.dat'
unset label
# ** Plot 2 **
set label ...

Resources