Sorry if this looks like simple question (probably) but I searched around to get some solution with no avail.
I have plot a bar graph as shown (attached) here. My problem is to adjust the spacing between label for each xtic which overlap to one another. If you notice the bar graph I attached here, at the x-axis, the "Third label" and "Fourth label long" overlap to each other. Is there anyway to control the spacing so that the labels are not overlap? Additionally, I need the legends (ring1, ring2 and ring12) to be in italics. Since I am using "terminal pngcairo", is there way to do it in italics?
set terminal pngcairo size 550,350 enhanced dash
set output "xplot_ACF_ring1-ring2-head-plots2.png"
set macro
labelFONT="font 'arial,22'"
scaleFONT="font 'arial,12'"
scaleFONT2="font 'helvetica,13'"
keyFONT="font 'arial,18'"
########################################################################################
set ylabel "Time in (ns)" #labelFONT
set ytic #scaleFONT
set xtic scale 0 #scaleFONT
set size 1.0, 1.0
########################################################################################
ring1 = "#ff0000"; ring2 = "#7FFF00"; ring12 = "#0000FF"
set auto x
set yrange [65:90]
set style data histogram
set style histogram cluster gap 1.5
set style fill solid 1.0 border -1
set boxwidth 0.9 relative
plot 'mal-cel-iso-bcm-ring1-ring2-head-bar-plot2.dat' using 2:xtic(1) ti col fc rgb ring1 ,\
'' u 3 ti col fc rgb ring2 ,\
'' u 4 ti col fc rgb ring12
The data for the above script is
Title "ring1" "ring2" "ring12"
"First label" 70 76 77
"Second label" 68 71 69
"Third label" 76 72 68
"Fourth label long" 75 76 77
Below is the plot I get after executing the script.
The re-edition of this post start here:
I would like to add error bar in this plot. The sample data is below:
Title "ring1" "ring2" "ring12"
"" 77.295326 2.2 74.829245 3.2 78.238016 2.1
"" 77.613533 6.2 74.123269 1.5 79.704782 3.6
"" 76.589653 2.1 71.704465 2.6 78.736618 4.2
"" 75.996256 0.4 73.407460 3.3 77.290057 2.5
The third fifth and seventh columns are actually the error values.
I wish may thanks in advance.
Another way to get around the problem would be to rotate the labels using:
set xtics rotate out
Or if you want to specify the rotation:
set xtics rotate by -45
There isn't an explicit option to prevent overlapping of labels.
In your example it is enough to decrease the white spacing to the left and right plot border a bit with
set offset -0.3,-0.3,0,0
which gives you with version 4.6.3:
Other options are e.g.
Increase the canvas size (set terminal ... size ...). Note, that set size doesn't affect the image size, but only the size of the graph.
For very long labels you can rotate the text e.g. with set xtic rotate ....
Just set the offset:
set offsets <left>, <right>, <top>, <bottom>
you can also find the following commands useful:
unset offsets
show offsets
remember that offsets can be constant or an expression
also remember that offsets are ignored in splots.
Related
When Googling "horizontal gnuplot bar chart", the first result I could find http://www.phyast.pitt.edu/~zov1/gnuplot/html/histogram.html suggests rotating (!) the final bar chart which seems rather baroque. Nonetheless I tried the approach but the labels are cut off.
reset
$heights << EOD
dad 181
mom 170
son 100
daughter 60
EOD
set yrange [0:*] # start at zero, find max from the data
set boxwidth 0.5 # use a fixed width for boxes
unset key # turn off all titles
set style fill solid # solid color boxes
set colors podo
set xtic rotate by 90 scale 0
unset ytics
set y2tics rotate by 90
plot '$heights' using 0:2:($0+1):xtic(1) with boxes lc variable
Is there a better approach?
The link you are referring to is from approx. 2009. gnuplot has developed since then. As #Christoph suggested, check help boxxyerror.
Script: (edit: shortened by using 4-columns syntax for boxxyerror, i.e. x:y:+/-dx:+/-dy)
### horizontal bar graph
reset session
$Data << EOD
dad 181
mom 170
son 100
daughter 60
EOD
set yrange [0:*] # start at zero, find max from the data
set style fill solid # solid color boxes
unset key # turn off all titles
myBoxWidth = 0.8
set offsets 0,0,0.5-myBoxWidth/2.,0.5
plot $Data using (0.5*$2):0:(0.5*$2):(myBoxWidth/2.):($0+1):ytic(1) with boxxy lc var
### end of script
Result:
Addition:
what does
2:0:(0):2:($0-myBoxWidth/2.):($0+myBoxWidth/2.):($0+1):ytic(1) mean?
Well, it looks more complicated than it is. Check help boxxyerror. From the manual:
6 columns: x y xlow xhigh ylow yhigh
So, altogether:
x take value from column 2, but not so relevant here since we will use the xyerror box
y take pseudocolumn 0 which is line number starting from zero, check help pseudocolumns, but not so relevant here as well
xlow (0) means fixed value of zero
xhigh value from column 2
ylow ($0-myBoxWidth/2.), line number minus half of the boxwidth
yhigh ($0+myBoxWidth/2.), line number plus half of the boxwidth
($0+1) together with ... lc var: color depending on line number starting from 1
ytic(1): column 1 as ytic label
For some reason (which I don't know) gnuplot still doesn't seem to have a convenient horizontal histogram plotting style, but at least it offers this boxxyerror workaround.
I have the next plot of a KDE density plot of a dataset in file AAFPStable.dat which has 5 columns.
I only want to use the first for the plot.
set encoding iso_8859_1
set key right top font "Helvetica,17"
set ylabel "Density" font "Helvetica,18"
set xlabel "Minutes" font "Helvetica,18"
set xtics font "Helvetica,16"
set ytics font "Helvetica,16"
set title "Event 1" font "Helvetica, 18"
set size 1, 1.2
set terminal postscript eps enhanced
set grid
set key spacing 1.5
set key box linestyle 1 width 3
lfps = system("cat AAFPStable.dat | wc -l")
set output "event1-gnu.eps"
plot [0:150][] "AAFPStable.dat" using 1:(1) smooth kdensity bandwidth 2.5 lw 2 title "FPS"
The problem is that now the figure shows de KDE density plot but on the y-axis, it shows the number of occurrences. I would like to have the normalized density plot showing probability densities between 0 and 1. The command says that the second column should be 1/(count of points). The value of lfps is the number of rows of the file. I have tasted to change 1:(1) by 1:(1/lfps) but it does not work. I would not like to generate an intermediate file with the first column of AAFPS.dat and add a second with the value of 1/lfps.
How could I indicate gnuplot to plot the normalize KDE density plot?
Thank you for your help
Regards
It works with
plot [0:150][] "AAFPStable.dat" using 1:(1./lfps) smooth kdensity bandwidth 2.5 lw 2 title "FPS"
Thanks
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
I have the following gnuplot script:
set autoscale
unset log
unset label
unset term
unset output
set xtics rotate by -90
set ytic auto
unset title
set xlabel "Survey metadata attribute subset"
set ylabel "Accuracy of classifier (%)"
set boxwidth 0.1
set style fill solid
set term eps
set output "metadata.eps"
plot "metadata.dat" using 1:3:xtic(2) title "PART" with boxes, \
"metadata.dat" using 1:5:xtic(2) title "JRip" with boxes, \
"metadata.dat" using 1:7:xtic(2) title "FURIA" with boxes
However, this draws all 3 sets of bars on top of each other, while I want them side by side, in that order, grouped together. So it should go something like: PARTbar, JRipbar, FURIAbar, gap, PARTbar, JRipbar, FURIAbar, gap, etc. How would I go about doing this?
I guess what you want is set style histogram clustered.
I have taken a minimal dataset (see bottom) graphing it with
set style histogram clustered
set xtics rotate by -90
unset title
set xlabel "Survey metadata attribute subset"
set ylabel "Accuracy of classifier (%)"
set boxwidth 1
set style fill solid
set term png
set output "so.png"
plot [-0.5:2.75][1:17] "so.dat"using 3:xtic(2) title "PART" with histograms, \
"" using 4 title "JRip" with histograms, \
"" using 5 title "FURIA" with histograms
which yields
I think you can take it further from here.
Data file "so.dat":
1 a 10 12 15
2 b 12 14 16
3 c 11 15 14
Suppose your data looks like this
1 a 2 3 4
2 b 1 4 5
3 c 6 7 8
One option is to set the boxwidth smaller and manually adjust the box positions so that they line up.
We can do that with
set boxwidth 0.25
plot datafile using ($1-0.25):3 with boxes t "First Series", \
"" using 1:4:xtic(2) with boxes t "Second Series", \
"" using ($1+0.25):5 with boxes t "Third Series"
This results in the following graph
Notice that I only set the xtics on the second series (the one in the middle), and I subtracted the boxwidth from the first series x coordinate (moving it back by one box unit), and added this to the last series (moving it forward by one box unit). I choose to use a boxwidth of 0.25 instead of 0.33 to allow a little gap between groups. Putting the xtic only on the second series ensures that it is on the one in the middle. With more boxes you will use a different width and will have to determine on which one to set the xtic labels.
An alternative is to use the histogram style. With the default boxwidth of 1, you can do
plot datafile u 3 with histogram t "First Series", \
"" u 4:xtic(2) with histogram t "Second Series", \
"" u 5 with histogram t "Third Series"
In this case, it doesn't matter where you place the xtic specification.
The histogram styles are very complex having lots of options. Essentially it consists of multiple plotting styles that are all invoked with the with histogram specification.
Which one of these methods to choose is mostly a matter of personal preference. The first is how you'd do this before the histogram style was added. The box method gives you more manual control over the final result, but the histogram style automates a lot of the details of getting those boxes just right.
I have gnuplot script like this:
set term pos eps
set style data histogram
set style histogram clustered gap 1
set grid y
set boxwidth 0.8 absolute
set style fill transparent solid 0.8 noborder
#set key outside right top vertical Left
#component stuff
set output 'eps/component.eps'
set title "Component in interesting Issues"
set xlabel "Components"
set xtics nomirror rotate by -270
plot 'dat/!component' using 2:xtic(1) t "Count" lc rgbcolor "blue" lt 1
and data:
Cmp count
AM 167
NM 90
RM 83
JT 53
TT 51
RED 32
MAP 29
COMM 3
SX 6
HS 68
and this is my output:
I see my graph has long spaces between the Y axis and the first bar. How can I spread the bars out evenly?
gnuplot gets confused by the first line in your data file, which is supposed to be the header. Just skip this line with every ::1 and the autoscaling is fine:
plot 'dat/!component' using 2:xtic(1) every ::1 t "Count" lc rgbcolor "blue" lt 1
Result with 4.6.4:
In general you don't need to use histograms for this kind of data, the plotting style with boxes also works fine.