gnuplot: Row stacked bar graph with error bar - plot

I am new to gnuplot. I want to generate graph from data points with three components and standard deviation.
My data looks like this:
TYPE1 15 20 65 5
TYPE2 20 20 60 4
TYPE3 10 30 60 6
TYPE4 30 30 40 5
I want to plot a rowstacked bar for each TYPE with the 3 components stacked and an errobar at the top.
I wrote the following script to do this:
set terminal png
set output "sample.png"
set boxwidth 0.75 relative
set style fill pattern 0 border
set style histogram rowstacked
set style data histograms
set xtics 1000 nomirror
set ytics 100 nomirror
set noytics
set mxtics 2
set mytics 2
set ytics 100
set yrange [0:150]
set ylabel "Y"
set xlabel "X"
set title "Sample graph"
plot 'data.dat' using (100*column(2)/(column(2)+column(3)+column(4))) t "A" , '' using (100*column(3)/(column(2)+column(3)+column(4))) t "B" , '' using (100*column(4)/(column(2)+column(3)+column(4))):xtic(1) t "C"
This produced a graph which looks like this: .
But I am not able to get the errorbar on the top of each bar with deviation values in column 5. I tried different ways using rowstacked and errorbar styled bar graphs but had no luck.

For this you must know, that with the histogram style the boxes are placed at the x-positions 0, 1, etc. i.e. at the row number.
So for the errorbars you must use column(0) as x-coordinate:
set terminal pngcairo
set output "sample.png"
set boxwidth 0.75 relative
set style fill pattern 0 border
set style histogram rowstacked
set style data histograms
set yrange [0:150]
set macros
scale = '100/(column(2)+column(3)+column(4))'
set bars 2.0
plot 'data.dat' using ($2 * #scale):xtic(1) t "A" , \
'' using ($3 * #scale) t "B" , \
'' using ($4 * #scale) t "C",\
'' using 0:(100):5 with errorbars notitle lw 2 lt -1
The result with 4.6.3 is:
For convenience I used a macro scale. The macros work as follows: You define a string, like scale = '...' in the script above. That can be used later in any expression as #scale (you must have set macros enabled). The content of the scale string is then replaced before the respective command is executed.

Related

Can I space a gnuplot grid differently than tics?

How can I add an X grid with different spacing than the tics? My plot is a histogram showing # of patents (of a certain type) granted per year, and the year range is large (1807-1971). I'd like to tic/label each year but only add X grid lines every decade (and also use a different color for the matching decade labels).
I've been searching for an answer and trying things for hours and getting nowhere. Are either of these possible?
My current plot (with no X grid) looks like:
And the script is:
set style data histograms
set style histogram gap 1
set style fill solid
set title "Number of Prism Glass Patents Granted" font "fixed, 24" offset 0,-0.9
set xlabel "Year" font "fixed,18" offset 0,0.8
set nokey
set xtics out nomirror rotate font "fixed, 8" offset 0,0.4
set grid y
plot 'frequency.dat' using 2:xtic(1) linecolor 'blue'
I assume your data consists out of two columns: Year and number of patents.
Why do you use xitc(1), is it necessary to label every single year?
What about using minor and major xtics? I would use plotstyle with boxes.
Code:
### major and minor xtics
reset session
# generate some random data
set print $Data
do for [i=1807:1971] {
print sprintf("%d %d", i, int(rand(0)*100))
}
set print
set xlabel "Year"
set xtics out nomirror
unset x2tics
set xtics 10
set mxtics 10
set grid ytics
set grid xtics
set boxwidth 0.5
plot $Data u 1:2 with boxes fill solid 1.0 lc rgb "blue" notitle
### end of code
Result:
Addition:
Another version with grid every 10 years and label with different color. Labels are only shown when number of patents>0. Instead of using xtics it is done by plotting with labels.
Code:
### major and minor xtics
reset session
set term pngcairo size 1600,360
set output "tbGrid.png"
xmin = 1807
xmax = 1971
# generate some random data
set print $Data
do for [i=xmin:xmax] {
print sprintf("%d %d", i, int(rand(0)+0.4)*(int(rand(0)*100)))
}
set print
set xlabel "Year" offset 0,-1.5
set xrange[xmin-1:xmax+1]
set xtics 10 format "" out nomirror
set mxtics 10
set bmargin 5
set grid ytics
set grid xtics
set boxwidth 0.5
myTic(n,p) = p==0 ? "" : sprintf("%d",n)
myColor(n) = int(n)%10==0 ? 0xff0000 : 0x000000
plot $Data u 1:2 with boxes fill solid 1.0 lc rgb "blue" notitle, \
'' u 1:(0):(myTic($1,$2)):(myColor($1)) with labels \
tc rgb var rotate offset 0,-1.5 font ",8" notitle
set output
### end of code
Result:
The grid for each axis is generated from the tics for that same axis, so yes they always match. However if your plot uses only the x1 axis, you could define the range and tics for the x2 axis also and turn on the grid only for x2 and not for x1.
Recent gnuplot versions have a command set link x2 that ensures the x1 and x2 axes agree on the range and scale. If your version does not support this you can still set them to match explicitly:
set xrange [min:max]
set x2range [min:max]
set xtics <whatever> # these will label the actual plot
set x2tics <something else> # these will be used only for grid lines
set x2tics scale 0.0 format "" # show no x2 tics or labels on the plot
set grid x2 nox
plot ...
Thank you both-- here's my final plot:
...and its script:
# histogram of # of prism glass patents granted per year
###
set term png size 1800,600
xmin = 1807
xmax = 1971
set title sprintf("Prism Glass Patents Granted %d-%d", xmin, xmax) \
font "fixed, 24" offset 0,-0.5
set xlabel "Year" font "fixed,24" offset 0,-2
# x tic for each year
set xrange [xmin-1:xmax+1]
set xtics 1 out nomirror format ""
# x2 tic and gridline for each decade
set x2tics 1800,10,1970
set x2tics out font "fixed, 12" offset 0,-0.6
set grid x2 nox
set grid y # y axis is count
set boxwidth 0.5
set bmargin 5
set nokey
myTic(y,n) = n==0 ? "" : sprintf("%d",y) # only label year if count>0
myColor(y) = y==1897 ? 0x0000FF : 0x000000 # highlight 1897 (biggest year)
plot 'frequency.dat' using 1:2 with boxes fill solid lc rgb "blue", \
'' using 1:(0):(myTic($1,$2)):(myColor($1)) with labels \
tc rgb var rotate offset 0.1,-1.2 font "fixed,8"

How to fix xtics label overlapping in gnuplot

I'm using GNUPlot to draw the graph of five real-valued functions. I think it is easy to solve, but I am a beginner.
My x-axis has problems with its label. All the xtics numbers are overlapping, and I have no idea why.
My .plt plots 3 files: an .eps, an pb .eps and a .png. It receives a .100 file, which is 6 columns of numbers. The first column represents values for t and the other five columns represent values for each function at respective t.
My .plt file is:
reset
set terminal windows
set style line 1 lt 1 linewidth 3
set style line 2 lt 2 linewidth 3
set style line 3 lt 3 linewidth 3
set style line 4 lt 4 linewidth 3
set style line 5 lt 5 linewidth 3
set style line 6 lt 6 linewidth 3
set border linewidth 3
set xzeroaxis
set yzeroaxis
set xlabel '{/Helvetica-Oblique t (dias)}' enhanced font ',28'
set key center top
set key center right
set key top right
set key box
set tics scale 1.5
set grid ytics
set grid xtics
set xtics 0,250,3500
set ytics 0,0.1,1
set title 'Simulacao' font ',26'
set samples 100
plot "./fort.100" using 1:2 with lines title 'Ms' linestyle 1, \
"./fort.100" using 1:3 with lines title 'Mi' linestyle 2, \
"./fort.100" using 1:4 with lines title 'A ' linestyle 3, \
"./fort.100" using 1:5 with lines title 'H ' linestyle 4, \
"./fort.100" using 1:6 with lines title 'I ' linestyle 5
pause -1
set terminal postscript eps enhanced
set termoption enhanced
set output "xsol-pb.eps"
replot
set terminal postscript eps enhanced color font ',22'
set termoption enhanced
set output "xsol.eps"
replot
set terminal png giant size 900,600 enhanced
set termoption enhanced
set output "xsol.png"
replot
Link for my .eps graph:
How can I fix this problem?
I am also having a problem with the table, at the right top corner of the image. How to fix that?
Two possibilities to avoid "collisions" of data and key:
adjust your scale such that the curve is below the key, in your case e.g. set yrange[0:1.5]
shift the positon of your key, e.g. set key center right or set key at graph 0.8, graph 0.8. Check help key for more information.

GNUPLOT: GPVAL_Y_MIN and GPVAL_Y_MAX in MULTIPLOT environment

This being my first question here makes me a litte nervous. Here I go.
What I want to achieve is to plot four different data files in a multiplot (2x2, maybe more). So far I succesfully plotted this disregarding the different ranges in the y-axis on each, the x-axis is, however, homogenous to all subplots.
So, what I would like is to set to each y-axis 10 "ytics" with a number of minor tics (and, by extension, the x-axis as well). I believe this is done with GPVAL_X (or GPVAL_DATA_X) and GPVAL_Y (or GPVAL_DATA_Y) _MIN/_MAX. So far so good.
This however, does not achieve the result I was aiming for: http://postimg.org/image/uyavbtxn5/
On panel A) the y-axis is correctly divided in 10 segments, not so the x-axis.
Panel B) display correctly both axes.
Panel C) Display the x-axis correctly, whereas the y-axis no.
Panel D) Similar to C), but on the y-axis the tics appear disordered.
This poses the following questions:
1) How can I produce correctly this multiplot using GPVAL (or GPVAL_DATA)?
2) Why the plotted function appears in front of the corresponding label if it specified to otherwise?
This figure was produced with the following code:
reset
# Term
GNUTERM = "x11"
## Output
set t epslatex standalone color solid size 20in,12in font "ptm, 20"
set o "test.tex"
# Axes & Labels
set style line 11 lc rgb '#2F4F4F' lt 1
set border 3 back ls 11
set tics nomirror out
set format y "\\tiny $%2.2t \\cdot 10^{%S}$"
set format x "\\tiny $%1.1t$"
set xlabel "$\\rho$"
unset ylabel
unset key
# Grid
set style line 12 lc rgb'#2F4F4F' lt 0 lw 1.5
set grid back ls 12
# Border
set border 31 linewidth .75 # thin border
# Multiplot Files
set multiplot layout 2,2 columnsfirst
## UL
## Labels
LABEL = "A) $sin(x)$"
set obj 10 rect fill at graph 0.3, graph 0.75 size char strlen(LABEL), char 2
set label 10 at graph 0.3, graph 0.75 LABEL front center
plot sin(x)
## Tics
set xtics add GPVAL_X_MIN,(GPVAL_X_MAX-GPVAL_X_MIN)/10.,GPVAL_X_MAX
set ytics add GPVAL_Y_MIN,(GPVAL_Y_MAX-GPVAL_Y_MIN)/10.,GPVAL_Y_MAX
set mxtics 5
set mytics 5
## LL
## Labels
LABEL = "B) $cos(x)$"
set obj 10 rect fill at graph 0.3, graph 0.75 size char strlen(LABEL), char 2
set label 10 at graph 0.3, graph 0.75 LABEL front center
plot cos(x)
## Tics
set xtics add GPVAL_X_MIN,(GPVAL_X_MAX-GPVAL_X_MIN)/10.,GPVAL_X_MAX
set ytics add GPVAL_Y_MIN,(GPVAL_Y_MAX-GPVAL_Y_MIN)/10.,GPVAL_Y_MAX
set mxtics 5
set mytics 5
## UR
## Labels
LABEL = "C) $cos(x) sin(x)$"
set obj 10 rect fill at graph 0.3, graph 0.75 size char strlen(LABEL), char 2
set label 10 at graph 0.3, graph 0.75 LABEL front center
plot cos(x)*sin(x)
## Tics
set xtics add GPVAL_X_MIN,(GPVAL_X_MAX-GPVAL_X_MIN)/10.,GPVAL_X_MAX
set ytics add GPVAL_Y_MIN,(GPVAL_Y_MAX-GPVAL_Y_MIN)/10.,GPVAL_Y_MAX
set mxtics 5
set mytics 5
## LR
## Labels
LABEL = "D) $cos(x) / sin(x)$"
set obj 10 rect fill at graph 0.3, graph 0.75 size char strlen(LABEL), char 2
set label 10 at graph 0.3, graph 0.75 LABEL front center
plot cos(x)/sin(x)
## Tics
set xtics add GPVAL_X_MIN,(GPVAL_X_MAX-GPVAL_X_MIN)/10.,GPVAL_X_MAX
set ytics add GPVAL_Y_MIN,(GPVAL_Y_MAX-GPVAL_Y_MIN)/10.,GPVAL_Y_MAX
set mxtics 5
set mytics 5
unset multiplot
# Clean-up
set o
set t x11
reset
exit 0

GNUPLOT: saving data from smooth cumulative

I make this simple cumulative and histogram plot of a uniform random distribution of real numbers (n=1000):
http://www.filedropper.com/random1_1: random1.dat
And the macro is:
unset key
clear
reset
n=120 #number of intervals
max=4. #max value
min=1. #min value
width=(max-min)/n #interval width
#function used to map a value to the intervals
bin(x,width)=width*floor(x/width)+width/2.0 # cosi viene centrato in mezzo
set xtics min,(max-min)/10,max
set boxwidth width
set style fill solid 0.5 border
set ylabel 'Frequency'
set y2label 'Cumulative frequency'
set y2tics 0,100,1000
set ytics nomirror
set xrange [0.9:4.1]
set yrange [0:25]
set terminal pngcairo size 800,500 enhanced font 'Verdana,14'
set output "testCum.png"
plot 'random1.dat' using (bin($1,width)):(1.0) smooth frequency with boxes title 'histogram',\
'' using (bin($1,width)):(1.0) smooth cumulative axis x1y2 w l lt 2 lw 2 lc rgb 'green' title 'cumul'
Now the output.png is:
How I can tell to Gnuplot I want to take not only the cumulative plot but also the numbers coming from it saved in a particular file.dat ?
You can save the data after applying smooth with set table .... In the simplest case, if you need only the cumulative data, just use:
set table 'random1-smoothed.dat'
plot 'random1.dat' using (bin($1,width)):(1.0) smooth cumulative
unset table
For better inclusion in your script you could also wrap the whole, existing plot command in set table:
...
set table 'random1-smoothed.dat'
plot 'random1.dat' using (bin($1,width)):(1.0) smooth frequency with boxes title 'histogram',\
'' using (bin($1,width)):(1.0) smooth cumulative axis x1y2 w l lt 2 lw 2 lc rgb 'green' title 'cumul'
unset table
set terminal pngcairo size 800,500 enhanced font 'Verdana,14'
set output "testCum.png"
replot

How do you plot bar charts in gnuplot?

How do you plot bar charts in gnuplot with text labels?
Simple bar graph:
set boxwidth 0.5
set style fill solid
plot "data.dat" using 1:3:xtic(2) with boxes
data.dat:
0 label 100
1 label2 450
2 "bar label" 75
If you want to style your bars differently, you can do something like:
set style line 1 lc rgb "red"
set style line 2 lc rgb "blue"
set style fill solid
set boxwidth 0.5
plot "data.dat" every ::0::0 using 1:3:xtic(2) with boxes ls 1, \
"data.dat" every ::1::2 using 1:3:xtic(2) with boxes ls 2
If you want to do multiple bars for each entry:
data.dat:
0 5
0.5 6
1.5 3
2 7
3 8
3.5 1
gnuplot:
set xtics ("label" 0.25, "label2" 1.75, "bar label" 3.25,)
set boxwidth 0.5
set style fill solid
plot 'data.dat' every 2 using 1:2 with boxes ls 1,\
'data.dat' every 2::1 using 1:2 with boxes ls 2
If you want to be tricky and use some neat gnuplot tricks:
Gnuplot has psuedo-columns that can be used as the index to color:
plot 'data.dat' using 1:2:0 with boxes lc variable
Further you can use a function to pick the colors you want:
mycolor(x) = ((x*11244898) + 2851770)
plot 'data.dat' using 1:2:(mycolor($0)) with boxes lc rgb variable
Note: you will have to add a couple other basic commands to get the same effect as the sample images.
plot "data.dat" using 2: xtic(1) with histogram
Here data.dat contains data of the form
title 1
title2 3
"long title" 5
I would just like to expand upon the top answer, which uses GNUPlot to create a bar graph, for absolute beginners because I read the answer and was still confused from the deluge of syntax.
We begin by writing a text file of GNUplot commands. Lets call it commands.txt:
set term png
set output "graph.png"
set boxwidth 0.5
set style fill solid
plot "data.dat" using 1:3:xtic(2) with boxes
set term png will set GNUplot to output a .png file and set output "graph.png" is the name of the file it will output to.
The next two lines are rather self explanatory. The fifth line contains a lot of syntax.
plot "data.dat" using 1:3:xtic(2) with boxes
"data.dat" is the data file we are operating on. 1:3 indicates we will be using column 1 of data.dat for the x-coordinates and column 3 of data.dat for the y-coordinates. xtic() is a function that is responsible for numbering/labeling the x-axis. xtic(2), therefore, indicates that we will be using column 2 of data.dat for labels.
"data.dat" looks like this:
0 label 100
1 label2 450
2 "bar label" 75
To plot the graph, enter gnuplot commands.txt in terminal.
I recommend Derek Bruening's bar graph generator Perl script. Available at http://www.burningcutlery.com/derek/bargraph/
You can directly use the style histograms provide by gnuplot. This is an example if you have two file in output:
set style data histograms
set style fill solid
set boxwidth 0.5
plot "file1.dat" using 5 title "Total1" lt rgb "#406090",\
"file2.dat" using 5 title "Total2" lt rgb "#40FF00"

Resources