Y-value on bar graph in gnuplot? - 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

Related

gnuplot - adding median to plot with errorbars AND logscale'd x-axis

So I have some data files in format
x y ymin ymax
That I'm plotting with yerrorbars.
Now how would I best add a median of the y values to the plot running over the whole range of x?
UPDATE
I'm also plotting the x axis in logscale which seems to prevent using STATS.
Suppose that your data looks like this:
1 8 6 9
2 6 5 7
3 5 4 8
4 6 5 8
We can use the stats command to find the median. The use is similar to the plot command. Here, we only need to do analysis of the second column, so we will only specify the second column:
stats datafile u 2 nooutput
The nooutput option tells the command not to print the results. If we wish to see the full analysis, we simply omit that specification. By default, the stats command stores its results in variables of the form STATS_*. We can use a different prefix if desired. See help stats for more details.
At this point, we have a variable STATS_median that stores the median of the y values (which is 6 for the sample data). We can now add the median to the graph in one of two ways. First we can simply add a plot specification to the existing plot command:
plot datafile u 1:2:3:4 with yerrorbars, STATS_median
or we can add a line with the set arrow command and then plot just the yerrorbars:
set arrow 1 from graph 0, first STATS_median to graph 1, first STATS_median nohead
plot datafile u 1:2:3:4 with yerrorbars
Here we give the x coordinate in graph units which range from 0 (the left side) to 1 (the right side) and the y coordinate in the first coordinate system which corresponds to the y1 axis. Specifying nohead says to not draw an arrow head. The 1 immediately following set arrow tags this arrow as arrow 1 so that we can change or remove it easily later.
Other options are available. See help arrow for more details.

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 transform x axis data with in the plot

I have data in this format
apple 1
bananna 3
ornage 5
fig 4
I want the x axis data to appear in the plot and not in the axis as such. Moreover they are words and not numbers.
How can I do it please?
The question is a bit unclear. Where do you want the labels to show up? Here's an example where I'll put the label "apple" at the point (0,1) and the label "banana" at the point (1,3), etc. (each label is moved 1 unit further down the x axis):
plot 'data.dat' using (column(0)):2:1 with labels
You can also put points associated with it:
plot 'data.dat' using (column(0)):2 w p,\
'' using (column(0)):2:1 w labels
Here the labels sit directly on top of the points. That may not be what you want -- You can add an offset:
plot 'test.dat' u (column(0)):2:1 w labels offset character 0,1,\
'' u (column(0)):2 w p
From the documentation (help labels), it appears that the appearance of your labels should be able to be modified by pretty much any option that you can pass to set label.
The font, color, rotation angle and other properties of the printed text
may be specified as additional command options (see `set label`).

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.

Resources