gnuplot bins with zero value histogram - gnuplot

I'm trying to create a simple histogram of events per month over a 12 month year in Gnuplot. There are no events in either bin 2 or bin 7 and rather than plotting those bins as 0 it extends the neighboring bins over this space. I am using the following histogram plotting routine, which I've gotten from here: http://gnuplot-surprising.blogspot.com.au/2011/09/statistic-analysis-and-histogram.html
max=12
min=1
binwidth=1
hist(x,width)=x
set style fill solid 0.5
set xrange[0.5:12.5]
plot "file" using (hist($4,binwidth)):(1.0) smooth freq w boxes lc rgb"green"
where a sample row from "file" looks like this:
2008-11-21-08:10:47 29455.0 2008 11 21 19:10:47
The fourth column is the month information I am trying to plot. The resulting histogram is at the link:
https://docs.google.com/drawings/d/12yFs_KIznhrMx62OIwJJFVmlnwIk4TXdHSdGc0yWqwI/edit?usp=sharing
The bins centered on 2 and 7 (that should be 0) are missing and the neighboring bins are extended instead. I'm not sure where my error is and would appreciate any help! Thank you!

You should consult the gnuplot documentation about the boxes plotting style. Use
set boxwidth 1
otherwise the width of each box is caculated automatically so that it touches the adjacent boxes.

Related

plot multiple curves with color based on a color map in GNUPLOT

Here is the problem I am having with GNUPLOT: I have data files with two columns each (one for the voltage the other for the current) and each obtained for a certain temperature (which is specified in their name something like iv_300K.dat, iv_290K.dat etc.).
Now what I want is to plot each data file on the same graph and each plot to have a colour based on the file name (I would like to show you a figure I made with Mathematica but it seems that my reputation is too low...)
So lets say I have iv_300K.dat, iv_250K.dat and iv_160K.dat I would like to have three curves coloured first red, second green-ish and third blue, but based on the temperature information in the file name.
I am thinking something similar to what I did in Mathematica:
ColorData["DarkRainbow"][Rescale[T, {160, 350}]]
Where "DarkRainbow" is a colormap and Rescale[x,{min,max}]
gives x rescaled to run from 0 to 1 over the range min to max (according the Mathematica documentation).
So Rescale[250,{160,350}] = 0.473684
At the moment in GNUPLOT I am using the following for testing purposes:
plot for [i=350:160:-10] 'iv_'.i.'.K.dat' using 1:2 with lines title sprintf("".i." K")
but I can't get the colours to map the temperature.
Any help is appreciated!
Use linecolor palette frac to select a color from a palette based on an value in the range [0:1]:
set cbrange [160:350]
set style data lines
plot for [i=350:160:-10] 'iv_'.i.'.K.dat' using 1:2 linecolor palette frac (i-160.0)/(350.0-160.0) title sprintf("%dK", i)

gnuplot boxes with different color bars

I want to plot a histogram like chart with boxes. And I hope the bars have different colors. I found some previous cases, use lc rgb variable, but it doesn't work for me. My version is limited to gnuplot4.2. Here is my data sheet:
stage 11402.364 100% 1
App1 78.552 0.69% 2
App2 11323.812 99.30% 2
Read 8.469 0.07% 3
Write 41.285 0.04% 3
Repeat 5748.351 50.41% 3
Count 4933.746 43.27% 3
Count_1 3841.355 33.69% 4
Count_2 1092.391 9.59% 4
Here is the code part:
set boxwidth 0.5 relative
set style fill solid 0.5
set xtics rotate
plot 'histogramdata_2.txt' using 2:xtic(1):4 with boxes variable lc rgb variable notitle
I want to use the 4th column to denote the bar color. The document said the third number used in using is just the color variable. But it doesn't work for me, the result is no bar produced.
It seems that the using part is quite flexible. I even find some cases in this site put 4 column numbers after using.
It is related to different versions?
Your plot command seems to be wrong. Try the following:
set boxwidth 0.5 relative
set style fill solid 0.5
set xtics rotate
plot 'histogramdata_2.txt' using 0:2:4:xticlabels(1) with boxes lc variable
It should look like this:
In short about the using 0:2:4:xticlabels(1) part:
0 tells gnuplot to place bars (x value) in the same order as they appear on the file
2 tells gnuplot to take y values from column 2
4 tells gnuplot to take the color variable from the 4th column
xticlabels(1) tells gnuplot to take the text labels for the bars from column 1

gnuplot error: Not enough columns for variable color

I am executing the following gnuplot script:
set terminal svg
set output "file.svg"
set yrange [0:1]
set style fill solid
set key top left
set style fill transparent solid 0.6 border lt -1
set palette model RGB defined (1 "red", 2 "blue")
set xtics rotate by -45
plot "file.data" using 3:xticlabels(2):1 title "" with boxes palette
and file.data looks like this:
1 name1 0.356877
1 name2 0.643123
2 name3 0.688312
2 name4 0.311688
So I want the boxes with leading 2's in the data file to be blue, and the ones with a 1 in front to be red.
It fails when I add the palette keyword and prints the error message in the title.
Gnuplot is v4.6 patchlevel 4, I am running it on Ubuntu 14.04. I have created coloured plots this way before, so this probably is just a tiny error I am overlooking, but I'm all out of ideas.
Your script has two errors:
incorrect format specifier
According to the gnuplot documentation (? boxes at the command line) if three columns of input are provided to plot with boxes, the third column is a width parameter for the boxes. Using a variable line/fill color requires adding an additional column of input:
plot "file.data" using 0:3:(1):1 with boxes
An explanation of the four columns:
Column 0 provides an index: the first is 0, second is 1 etc. These give the x position (assuming each box is an individual).
Column 3 is your y data.
Column (1) provides the numerical value 1 for the x width of your boxes.
Column 1 provides your color information.
missing lc (linecolor) parameter to plot command
Your whole plot command should look like
plot "file.data" using 0:3:(1):1 title "" with boxes lc palette
also note
Keep in mind that the palette information is rescaled to be between the min/max values of cbrange. What I mean is that if your data is binary (1 or 2 in this case) the palette will be constructed properly, but if you want more specific colors (e.g. for data values 1/2/3) and you set a three-color palette in the same way it may not work the way you expect.

Gnuplot - How to place y-values above bars when using Histogram style?

I am currently using a script to generate histogram plots, e.g., by doing:
set style histogram cluster gap 4
plot for [COL=2:10] 'example.dat' u COL:xticlabels(1) title columnheader(COL)
Now I wish to add the y-values (numbers) above the bars in the histogram but adding w labels gives the 'Not enough columns for this style' error.
plot for [COL=2:10] 'example.dat' u COL:xticlabels(1) title columnheader(COL), \
for [COL=2:10] 'example.dat' u COL title '' w labels
Is it possible to add y-labels using the histogram style?
Note: I know that there are examples for plotting with boxes. I wish to make this work with the histogram style if possible.
Here's a test datafile I came up with:
example.dat
hi world foo bar baz qux
1 2 3 4 5 6
4 5 7 3 6 5
Here's the script I used to plot it:
set yrange [0:*]
GAPSIZE=4
set style histogram cluster gap 4
STARTCOL=2 #Start plotting data in this column (2 for your example)
ENDCOL=6 #Last column of data to plot (10 for your example)
NCOL=ENDCOL-STARTCOL+1 #Number of columns we're plotting
BOXWIDTH=1./(GAPSIZE+NCOL) #Width of each box.
plot for [COL=STARTCOL:ENDCOL] 'example.dat' u COL:xtic(1) w histogram title columnheader(COL), \
for [COL=STARTCOL:ENDCOL] 'example.dat' u (column(0)-1+BOXWIDTH*(COL-STARTCOL+GAPSIZE/2+1)-0.5):COL:COL notitle w labels
Each cluster of histograms takes a total width of 1 unit on the x axis. We know how many widths we need (the number of boxes +4 since that is the gapsize). We can calculate the width of each box (1/(N+4)). We then plot the histograms as normal. (Note that I added with histogram to the plot command).
According to the builtin help, labels require 3 columns of data (x y label). In this case, the y position and the label are the same and can be read directly from the column COL. The x position of the first block is centered 0 (and has a total width of 1). So, the first block is going to be located at x=-0.5+2*BOXWIDTH. The 2 here is because the gap is 4 boxwidths -- two on the left and 2 on the right. The next block is going to be located at -0.5+3*BOXWIDTH, etc. In general, (as a function of COL) we can write this as
-0.5+BOXSIZE*(COL-STARTCOL+1+GAPSIZE/2)
We need to shift this to the right by 1 unit for each additional block we read. Since each block corresponds to 1 line in the data file, we can use pseudo-column 0 (i.e. column(0) or $0) for this since it gets incremented for each "record/line" gnuplot reads. The 0th record holds the titles, the first record holds the first block. Since we want a function which returns 0 for the first record, we use column(0)-1. Putting it all together, we find that the x-position is:
(column(0)-1-0.5+BOXSIZE*(COL-STARTCOL+1+GAPSIZE/2))
which is equivalent to what I have above.

Y-value on bar graph in gnuplot?

Can I get gnuplot to display the exact y-value or height of a data point (plotted using "with boxes") over its bar? I would like the plot to be easy to read so nobody has to line up the top of a bar with the y-axis and guess what the value is.
You can use the labels style and combine it into the plot command with the boxes style. The labels style expects 3 columns of data - the x coordinate, the y coordinate, and the actual label text.
For example, with the following data
1 4
2 6
3 2
4 8
the command (we set the yrange to 0 - 10 and boxwidth to 0.9 and set a solid fill style)
plot datafile u 1:2 with boxes, "" u 1:2:2 with labels offset char 0,1
produces
Normally, the labels would be centered on the specified point (the top edge of the box). By specifying an offset, we can move them up to just above the box. Here we used no offset in the x direction, but a unit of 1 in the y direction. We used the character coordinate system, so this corresponds to moving up by one character unit.
I can only think of putting the values where you want them "manually" like this:
set label "value" at 12,34
The numbers are coordinates according to your x and y ranges.
An automatic way would use "with labels", see e.g.
http://gnuplot.sourceforge.net/demo_4.4/stringvar.html

Resources