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.
Related
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'm trying to use gnuplot to plot a stacked histogram of some data but it skips the first bin (the first row of the data file).
The data is:
1 0.2512 0.0103 0.9679
2 0.4730 0.2432 0.8468
3 0.6669 0.2826 0.6895
4 0.6304 0.2268 0.7424
And the plot code is
set title "Data"
set key invert reverse Left outside
set key autotitle columnheader
set style data histogram
set style histogram rowstacked
set style fill solid border -1
#set boxwidth 0.75
plot 'data.dat' using 2:xtic(1) title 'X', '' using 3 title 'Y', '' using 4 title 'Z'
The output is. I checked it and it correctly displays the data of the 2nd, 3rd and 4th rows of the data file. Why am I missing the first bin..?
Thanks a lot!
I already checked this with no help: Using gnuplot for stacked histograms
As it turns out, it was a very simple mistake, that I've fixed mostly thanks to Azad comment about the titles.
The new code is:
set title "Position error along the three axis"
set key invert reverse Left outside
#set key autotitle columnheader
set style data histogram
set style histogram rowstacked
set style fill solid border -1
#set boxwidth 0.75
plot 'data.dat' using 2:xtic(1), '' using 3, '' using 4
Titles have been removed from the code. Gnuplot was taking the first row (which should have been the first bin) as the titles and then it was overwritten by the title 'X' etc.
The new data looks like this:
0 X Y Z
1 0.2512 0.0103 0.9679
2 0.4730 0.2432 0.8468
3 0.6669 0.2826 0.6895
4 0.6304 0.2268 0.7424
This fixed the problem, now all the bins are correctly displayed!
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.
I'd like to plot the received powers over different wireless channels. For each channel, I have three values, and I want to plot them stacked.
Actually, this is my script:
set boxwidth 0.6 relative
set ylabel "dBm"
set xlabel "Channel"
set style fill solid
set key off
plot "RR1" using 1:2 with boxes, "RR2" using 1:2 with boxes, "RR3" using 1:2 with boxes
The problem is that since they are negative values (dBm), it plots from 0 to the value it finds, and thus the highest power is on the top. I'd like to plot a somewhat reverse image, with the blue box starting from the bottom up to the value it reaches, and the same for the other two values.
Any idea?
My data looks like this
21.0 -93.9207
22.0 -92.241
23.0 -93.452
One possibity is to use the boxxyerrorbars plotting style:
reset
set ylabel "dBm"
set xlabel "Channel"
set style fill solid
set key off
set style data boxxyerrorbars
set xtics 1
set autoscale xfix
set offset 0.5,0.5,0,0
ylow = -100
plot for [i=3:1:-1] sprintf("RR%d", i) using 1:(0.5*($2+ylow)):(0.3):(0.5*($2-ylow)) lt i
Here, I used a fixed lower y-value, but you could also extract it from the data file with stats and do some other tweaking.
In the using statement, the second column gives the box center, which is the mean of actual y-value and the lower boundary, the third column is x-delta (half of the actual box width), and the fourth column is y-delta.
With some more data values, this gives:
When running the following script, I get an error message:
set terminal postscript enhanced color
set output '| ps2pdf - histogram_categorie.pdf'
set auto x
set key off
set yrange [0:20]
set style fill solid border -1
set boxwidth 5
unset border
unset ytic
set xtics nomirror
plot "categorie.dat" using 1:2 ti col with boxes
The error message that I get is
smeik:plots nvcleemp$ gnuplot categorie.gnuplot
plot "categorie.dat" using 1:2 ti col with boxes
^
"categorie.gnuplot", line 13: x range is invalid
The content of the file categorie.dat is
categorie aantal
poussin 13
pupil 9
miniem 15
cadet 15
junior 6
senior 5
veteraan 8
I understand that the problem is that I haven't defined an x range. How can I make him use the first column as values for the x range? Or do I need to take the row numbers as x range and let him use the first column as labels? I'm using Gnuplot 4.4.
I'm ultimately trying to get a plot that looks the same as the plot I made before this one. That one worked fine, but had numerical data on the x axis.
set terminal postscript enhanced color
set output '| ps2pdf - histogram_geboorte.pdf'
set auto x
set key off
set yrange [0:40]
set xrange [1935:2005]
set style fill solid border -1
set boxwidth 5
unset border
unset ytic
set xtics nomirror
plot "geboorte.dat" using 1:2 ti col with boxes,\
"geboorte.dat" using 1:($2+2):2 with labels
and the content of the file geboorte.dat is
decennium aantal
1940 2
1950 1
1960 3
1970 2
1980 3
1990 29
2000 30
the boxes style expects that the x-values are numeric. That's an easy one, we can give it the pseudo-column 0 which is essentially the script's line number:
plot "categorie.dat" using (column(0)):2 ti col with boxes
Now you probably want the information in the first column on the plot somehow. I'll assume you want those strings to become the x-tics:
plot "categorie.dat" using (column(0)):2:xtic(1) ti col with boxes
*careful here, this might not work with your current boxwidth settings. You might want to consider set boxwidth 1 or plot ... with (5*column(0)):2:xtic(1) ....
EDIT -- Taking your datafiles posted above, I've tested both of the above changes to get the boxwidth correct, and both seemed to work.