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!
Related
I'm new to using gnuplot and I've followed this question which plots the data as I desire. However, I'd very much like to also include error bars. I've tried to do so by adding min and max error columns as follows:
Broswer,Video,min,max,Audio,min,max,Flash,min,max,HTML,min,max,JavaScript,min,max
IE,30%,5,5,10%,5,5,25%,5,5,20%,5,5,15%,5,5
Chrome,20%,5,5,5%,5,5,35%,5,5,30%,5,5,10%,5,5
Which I then try to plot with the script modified as follows:
set terminal pdf enhanced
set output 'bar.pdf'
set style data histogram
set style histogram cluster gap 1
set style fill solid border rgb "black"
set auto x
set yrange [0:*]
set datafile separator ","
plot 'data.dat' using 2:xtic(1) title col with yerrorbars, \
'' using 3:xtic(1) title col with yerrorbars, \
'' using 4:xtic(1) title col with yerrorbars, \
'' using 5:xtic(1) title col with yerrorbars, \
'' using 6:xtic(1) title col with yerrorbars
From what I understand from reading this should also plot errorbars, but I get the error:
"plot2", line 16: Not enough columns for this style
Googling this error informs me that it has something to do with the first column being non-numerical. I've tried a few suggestions including this one, but nothing has worked so far. So, any suggestions? Thanks.
This error tells you, that the yerrorbars plotting style requires more than one column for plotting (the xtic(1) takes a special parts). Looking at the documentation, you can see, that you can use either two, three or four columns. I don't go more into detail, because the with yerrorbars selects a completely new plotting style and you don't get any histogram at all.
In order to plot clustered histograms, you must add errorbars to the histogram's style definition, and of course you must give the column for the yerror values:
set style data histogram
set style histogram cluster gap 1 errorbars
set style fill solid border rgb "black"
set auto x
set yrange [0:*]
set datafile separator ","
plot 'data.dat' using 2:3:xtic(1) title col(2),\
'' using 5:6 title col(5), \
'' using 8:9 title col(8), \
'' using 11:12 title col(11), \
'' using 14:15 title col(14)
Or, in shorter notation
plot for [i=2:14:3] 'data.dat' using i:i+1:xtic(1) title col(i)
If you explicitly need to plot min and max values, than you must add a third column. But then the last two columns are ymin and ymax and not delta values. Judging from you data file error, the values in the data file are deltas, so the plot command should be:
plot for [i=2:14:3] 'data.dat' using i:(column(i) - column(i+1)):(column(i) + column(i+2)):xtic(1) title col(i)
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 a gnuplot script which plots a histogram. I used the following syntax:
set style data histogram
set style histogram cluster gap 2
set style fill solid
set logscale y
rgb(r,g,b) = int(r)*65536 + int(g)*256 + int(b)
plot 'histogram_data' using (column(0)):2:(0.5):(rgb($3,$4,$5)):xticlabels(1) w boxes notitle lc rgb variable
What the last line does is: using column 1 as x labels, column 2 as the height of the histogram bars, 0.5 as box width, and columns 3, 4 and 5 as the rgb values to colour the bars.
Now, the problem is that modifying the gap parameter in line 2 does not change in any way the spacing between bars, even though as far as I understand that is the correct way to adjust such spacing. I am using gnuplot 4.6 patchlevel 4.
I found a way to do this with boxes, though I do not consider it very clean:
plot 'histogram_data' u (column(0)*2+1):2 w boxes notitle lc rgb 'white',\
'histogram_data' u (column(0)*2):2:(rgb($3,$4,$5)):xticlabels(1) w boxes notitle lc rgb variable;
This command is plotting all the data of the main plot on even slots and a white box on odd slots. So the first line in the plot command is plotting the gaps between every box of the plot (the width of these gaps can be specified using the boxwidth property I think but I haven't tested this), while the second line is drawing the actual plot.
I could not find a way to do this with the histogram plotting style, keeping the variable colours specified in the data file.
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:
My data file looks like this
A 20120301 4
A 20120302 3
B 20120301 5
B 20120302 6
C 20120303 5
except there are many more than just A,B,C and I want to create a stacked graph with gnuplot (similar to the "Stacked histograms" from the gnuplot demos)
20120301 = (A:4 + B:5)
20120302 = (A:3 + B:6)
20120303 = (C:5)
So far I could not convince plot to read the data in that format. Do I have re-arrange the data file for this? Or is there a way for gnuplot to read the data in that format?
I think I've managed to beat it into a form that will work (you'll need at least gnuplot 4.3):
set boxwidth 0.75 absolute
set style fill solid 1.00 border lt -1
set datafile missing '-'
set style histogram rowstacked
set style data histograms
set yrange [0:]
plot for [i=2:4] 'test.dat' u i,'' u (0.0):xtic(1) notitle
and here's the datafile test.dat
#date A B C
#missing data is marked by a minus sign
20120301 4 5 -
20120302 3 6 -
20120303 - - 5
Phew! I've never been much good with gnuplot when it comes to histograms. Hopefully this will work for you (Sorry about the change to your datafile).