I'm having this file as data.dat:
Xstep Y1 Y2 Y3 Y4
332 1.22 0.00 0.00 1.43
336 5.95 12.03 6.11 10.41
340 81.05 81.82 81.92 81.05
394 11.76 6.16 10.46 5.87
398 0.00 0.00 1.51 1.25
1036 0.03 0.00 0.00 0.00
I can plot this data as histogram with this script, hist-v1.gplot (using set style data histogram):
set xlabel "X values"
set ylabel "Occurence"
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set term png
set output 'hist-v1.png'
set boxwidth 0.9
# attempt to set xtics so they are positioned numerically on x axis:
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)
# ti col reads the first entry of the column, uses it as title name
plot 'data.dat' using 2:xtic(1) ti col, '' u 3 ti col, '' u 4 ti col, '' u 5 ti col
And by calling:
gnuplot hist-v1.gplot && eog hist-v1.png
this image is generated:
However, you can notice that the X axis is not scaled numerically - it understands the X values as categories (i.e. it is a category axis).
I can get a more numerical X axis with the following script, hist-v2.gplot (using with boxes):
set xlabel "X values"
set ylabel "Occurence"
# in this case, histogram commands have no effect
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set term png
set output 'hist-v2.png'
set boxwidth 0.9
set xr [330:400]
# here, setting xtics makes them positioned numerically on x axis:
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)
# 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"!
plot 'data.dat' using 1:2 ti col smooth frequency with boxes, '' u 1:3 ti col smooth frequency with boxes
And by calling:
gnuplot hist-v2.gplot && eog hist-v2.png
this image is generated:
image hist-v2.png http://img266.imageshack.us/img266/6717/histv2.png
Unfortunately, the bars 'overlap' here, so it is hard to read the graph.
Is there a way to keep the numerical scale X axis as in hist-v2.png, but keep the 'bars' side by side with as in hist-v1.png? This thread, "Re: Histogram with x axis date error" says you cannot:
But it will be hard to pull the x-coordinate date out of the data file, ...
but then, it refers to a different problem...
Thanks,
Cheers!
Ok, after reading the gnuplot help for a bit, it seems that histogram style will ''always'' interpret x axis as sequential entries/categories - so indeed, there seems to be no way to get a numerical axis with a histogram style.
However, it turns out that $ can refer to a column, and those can be used to actually 'reposition' the bars in the second (frequency with boxes style) example; so with this code as hist-v2b.gplot:
set xlabel "X values"
set ylabel "Occurence"
set style fill solid border -1
set term png
set output 'hist-v2.png'
set boxwidth 0.9
set xr [330:400]
# here, setting xtics makes them positioned numerically on x axis:
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)
# 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"!
plot 'data.dat' using ($1-0.5):2 ti col smooth frequency with boxes, '' u ($1-0.25):3 ti col smooth frequency with boxes, '' u ($1+0.25):4 ti col smooth frequency with boxes, '' u ($1+0.5):5 ti col smooth frequency with boxes
And by calling:
gnuplot hist-v2b.gplot && eog hist-v2b.png
this image is generated:
image hist-v2b.png http://img823.imageshack.us/img823/805/histv2b.png
... which is pretty much what I wanted in the first place.
Just a small note - I originally wanted to use the script with inline data; for a setup like this, it would have to be written as
plot '-' using ($1-0.5):2 ti col smooth frequency with boxes, '-' u ($1-0.25):3 ti col smooth frequency with boxes
Xstep Y1 Y2 Y3 Y4
332 1.22 0.00 0.00 1.43
336 5.95 12.03 6.11 10.41
340 81.05 81.82 81.92 81.05
394 11.76 6.16 10.46 5.87
398 0.00 0.00 1.51 1.25
1036 0.03 0.00 0.00 0.00
end
Xstep Y1 Y2 Y3 Y4
332 1.22 0.00 0.00 1.43
336 5.95 12.03 6.11 10.41
340 81.05 81.82 81.92 81.05
394 11.76 6.16 10.46 5.87
398 0.00 0.00 1.51 1.25
1036 0.03 0.00 0.00 0.00
end
... that is, the data would have to be entered multiple times, as it comes in from stdin - this problem is discussed in gnuplot - do multiple plots from data file with built-in commands.
Cheers!
PS: As there is quite a bit of space on the diagram, it would be nice if we could somehow specify separate x-axis ranges; that is discussed in:
Gnuplot tricks
Gnuplot tricks: Broken axis revisited
Setting the box width properly is very important when you plot a histogram using "boxes" plot style. In one of my blog article I have talked about it. If any interest,click here!
Related
I want to indicate on graph y-axis logarithmic grids like SNR plot that enabled dash or dot y-tics. Matlab and similar programs does this, but need to use Gnuplot.
### sum up for certain conditions
reset session
set key right box
set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#ffffff" behind
set datafile separator comma
set grid
set output 'logscaled.png'
set logscale y 10
set ytics
show ytics
set y2tics
show ytics
set xlabel "SNR"
set ylabel "BER"
plot "snr-ber.csv" title 'Random' w l
### end of code
SNR
BER
-5.00
2.8301e-01
-4.00
2.6931e-01
-3.00
2.3582e-01
-2.00
2.2382e-01
-1.00
1.511e-01
0.00
1.4879e-01
1.00
1.2080e-01
2.00
1.1439e-01
3.00
8.5206e-02
4.00
6.6113e-02
5.00
4.7682e-02
6.00
3.3031e-02
7.00
2.2605e-02
8.00
5.61010e-03
9.00
3.6660e-03
10.00
8.5350e-04
11.00
2.7550e-04
12.00
4.8500e-05
13.00
1.6000e-06
14.00
0.0000e+00
set grid x y my will enable grid lines for the major tics along both the x and y axes, and minor tics along the y axis.
In the visual, 2 different graphics are given in a single frame (layout 1, 2).
Both graphs are constrained by y 0: 7.
Data concerning the graph of the column (phonon distribution) "" ZrSbTe.ph_THz "and xmax = 0.885990
The data concerning the 2nd column graph are “ZrSbTe.dos_THz” and 2: 1 (Total), 3: 1 (Zr), 4: 1 (Sb) and 5: 1 (Te) ie the y-axis in the 1st column graph in the file, other columns in the file are taken as the x-axis in the graph.
70% area was used for 1st Chart and 30% for 2nd Chart.
Let's get the same graph by paying attention to the colors.
Let's load the gnuscript and the output graphic by making a 2 page PDF.
x-coordinates of column labels (you specify y coordinates)
Γ = -0.01
X = 0.13
L = 0.20
T = 0.22
W = 0.251
R = 0.34
X1 = 0.38
Z = 0.50
Γ = 0.575
Y = 0.67
S = 0.715
W = 0.835
Your final goal could be split into many different questions and "how-to-dos",... too many without any code and specific question. Keep in mind, for every keyword you can type help <keyword> in the gnuplot console to get some information. Check the gnuplot homepage and the PDF manual in the local doc folder.
I assume you are new to gnuplot... check the following example as a starting point. If you have a specific question, please remember: questions always with code.
Code:
### split graph
reset session
$Labels <<EOD
Γ -0.01
X 0.13
L 0.20
T 0.22
W 0.251
R 0.34
X1 0.38
Z 0.50
Γ 0.575
Y 0.67
S 0.715
W 0.835
EOD
# create some random test data
N=100
set print $PH
do for [i=0:N] {
print sprintf("%g %g %g %g %g",-0.01+i*(0.835+0.01)/N, rand(0)*3, rand(0)*3+1, rand(0)*3+3, rand(0)*3+4)
}
set print $DOS
print "X Total Zr Sb Te"
do for [i=0:N] {
print sprintf("%g %g %g %g %g",-0.01+i*(0.835+0.01)/N, rand(0), rand(0)+1, rand(0)+2, rand(0)+3)
}
set print
# set lines
do for [i=1:|$Labels|] {
set arrow i from word($Labels[i],2),graph 0 to word($Labels[i],2), graph 1 nohead lw 1.5 front
}
set ylabel "Frequency / THz"
set key noautotitle opaque box
set colorsequence classic
set multiplot layout 1,2
set size 0.7,1
set bmargin 3
set rmargin 0
plot for [i=2:5] $PH u 1:i w l lc "dark-grey", \
$Labels u 2:(NaN):xtic(1)
set size 0.3,1
set origin 0.7,0
set lmargin 0
set rmargin -1
unset ylabel
unset tics
unset arrow
set xlabel "DOS"
plot for [i=2:5] $DOS u i:1 w l ti columnhead(i)
unset multiplot
### end of code
Result:
Is it possible to use two positive y-axes with gnuplot? This is just a simple example for the question.
plot.gp:
reset
set style fill solid 1
set boxwidth 0.8 relative
plot 'data1.dat' index 1 using 1:2 with boxes title 'A' ,\
'' index 2 using 1:(-$2) with boxes title 'B'
Instead of using 1:(-$2) I would like to use 1:2 in the last line in plot.gp.
data1.dat:
0.12 0.024
0.15 0.132
0.18 0.241
0.22 0.136
0.12 0.039
0.15 0.219
0.18 0.197
0.22 0.155
From:
To:
Same answer as the one from theoz except that the y2 labels are shifted to the left side of the plot and the set link y2 command is used to generalize the axis inversion.
You can tweak the offset graph -1.03 to superimpose the "0" labels, then delete the duplicate "0" label by changing the y2 tic range to set y2tics 0.1,0.1.
$Data <<EOD
0.12 0.024
0.15 0.132
0.18 0.241
0.22 0.136
0.12 0.039
0.15 0.219
0.18 0.197
0.22 0.155
EOD
set style fill transparent solid 0.5
set boxwidth 0.8 relative
set xzeroaxis ls -1
set yrange[-0.3:0.3]
set ytics 0,0.1
set mytics 2
# Set y2 axis to exact mirror of y1
# Shift tic labels to the left and use right-justified text
set link y2 via -y inv -y
set y2tics 0,0.1
set y2tics offset graph -1.03 right
plot $Data u 1:2 index 0 axis x1y1 w boxes title 'A' ,\
'' u 1:2 index 1 axis x1y2 w boxes title 'B'
Here is a solution with a single plot, however, the y-axis tics are on different sides.
Maybe there is some easy way to get them on the same side.
Code:
### two "positive" y-axes
reset session
$Data <<EOD
0.12 0.024
0.15 0.132
0.18 0.241
0.22 0.136
0.12 0.039
0.15 0.219
0.18 0.197
0.22 0.155
EOD
set style fill transparent solid 0.5
set boxwidth 0.8 relative
set yrange[-0.3:0.3]
set ytics 0,0.1
set mytics 2
set y2range[0.3:-0.3]
set y2tics 0,0.1
set my2tics 2
set y2tics mirror
set xzeroaxis ls -1
plot $Data u 1:2 index 0 axis x1y1 w boxes title 'A' ,\
'' u 1:2 index 1 axis x1y2 w boxes title 'B'
### end of code
Result:
Addition:
Here is a second suggestion with all ytic labels on one side.
A little drawback is that it is not autoscale and you have to set the range and the steps "manually".
It is using the command evaluate (check help evaluate). And real() (check help real) in order to avoid gnuplot's integer division in case the two numbers of the division might be integers.
Code:
### y-axis with two "positive" directions
reset session
$Data <<EOD
0.12 0.024
0.15 0.132
0.18 0.241
0.22 0.136
0.12 0.039
0.15 0.219
0.18 0.197
0.22 0.155
EOD
set style fill transparent solid 0.5
set boxwidth 0.8 relative
set xzeroaxis ls -1
Max = 0.3
Steps = 3
set yrange[-Max:Max]
set ytics () # remove all tics
do for [i=-Steps:Steps] {
myTic = sprintf('set ytics add ("%g" %g)',abs(real(i)/Steps*Max),real(i)/Steps*Max)
eval(myTic)
}
plot $Data u 1:2 index 0 axis x1y1 w boxes lc 3 title 'A' ,\
'' u 1:(-$2) index 1 axis x1y1 w boxes lc 4 title 'B'
### end of code
Result:
I'm using Gnuplot with the 2,2 multiplot environment.
One of my datasets looks like this:
# Avg1. Min1. Max1. Avg2. Min2. Max2.
25 0.049 0.002 0.108 0.051 0.004 0.102
50 0.034 0.005 0.070 0.036 0.004 0.086
100 0.028 0.012 0.044 0.026 0.012 0.054
And I'm using the following script to plot the first graph (I think once I get the first one right I can just repeat the code):
#!/usr/bin/env gnuplot
set term post eps color solid enh
set multiplot layout 2,2 rowsfirst
set grid ytics
set offsets 0.5, 0.5
unset key
set ylabel offset 1,0
set xtics ("25" 1, "50" 2, "100" 3)
### First plot
set tmargin at screen 0.95
set bmargin at screen 0.65
set lmargin at screen 0.10
set rmargin at screen 0.45
set ylabel 'Y-Label Here'
plot 'data.dat' u :2:3:4 w yerrorlines ti 'Title1', \
'' u :5:6:7 w yerrorlines ti 'Title2'
### three other graphs
unset multiplot
And I have three more plots like this. The problem is that my X-axis only shows 25 and 50 (shown below).
I don't know how to fix this. Can anyone help please?
I've tried using 1:2:3:4 instead, but it shows the intermediate X-tics, which I don't want to show.
PlotExample
If you don't specify an explicit column for the x-value, then gnuplot uses the row index, which starts at zero:
set xtics ("25" 0, "50" 1, "100" 2)
plot 'data.dat' u 0:2:3:4 w yerrorlines ti 'Title1'
You can also directly use the values in the first column as xticlabels:
plot 'data.dat' u :2:3:4:xtic(1) w yerrorlines ti 'Title1'
extending the question I had asked earlier which can be found here, plotting multiple (x,y) co-ordinates in a single curve with gnuplot. I am trying to plot the bezier curves in gnuplot using 2 different files. Each (x,y) from the first file forms a bezier curve passing through the points from the second file. The first file has the co-ordinates like the following:
x y
0.0 0.5
0.12 0.1
0.16 0.4
0.2 0.35
0.31 0.8
0.34 0.6
0.38 1.0
0.46 0.2
0.51 0.7
0.69 0.9
The second file has the co-ordinates as the following:
x y
0.00 0.7
0.04 0.74073082208
0.08 0.85926917792
0.12 0.9
0.16 0.9
0.2 0.9
0.24 0.749720623086
0.28 0.874229601255
0.32 0.74073082208
0.36 0.8
0.4 0.721178508605
0.44 0.878821491395
0.48 0.761772990545
0.52 0.700774803388
0.56 0.723771273415
0.6 0.789508073675
0.64 0.864014272269
0.68 0.896743348931
Now, how do I merge these two files to plot a single graph. These two files don't have the same number of rows, but I guess that doesn't matter. The first curve would be between (x1,y1) and (x2,y2) from the first file which would continue till (x10,y10). In the curve between (x1,y1) and (x2,y2); the points (x1,y1), (x2,y2) and (x3,y3) from the second file lie.
I followed this link http://t16web.lanl.gov/Kawano/gnuplot/datafile3-e.html to sort and concatenate the two files but got some weird lines which is totally wrong. These values should actually plot Bezier curves but not getting the graph. I have written the following gnuplot script to plot the concatenated data:
set term x11 persist
set title "Animation curves"
set xlabel "Time (secs.)"
set ylabel "Parameter"
set x2label "Phoneme1" offset -35
set pointsize 2
set key off
set style line 2 lt 0 lc 1 lw 2
set xrange [0.0:1.0]
set yrange [0.0:1.3]
plot [0.0:0.8] "< cat -n file1.dat" u 1:2 smooth csplines ls 1, "" u 1:(0.0):(0):(1.3) w vectors nohead ls 2, "" u ($1+0.005):(1):(sprintf("P %d", $0)) w labels, \
"file1.dat" u 1:2 with points, \
"file2.dat" u 1:2 with points, \
I got the following error:
plot "< cat -n file1.dat" u 1:2 smooth csplines ls 1, "" u 1:(0.0):(0):(1.3) w vectors nohead ls 2, "" u ($1+0.005):(1):(sprintf("P %d", $0)) w labels, "file1.dat" u 1:2 with points, "file2.dat" u 1:2 with points,
^
"plot.gp", line 21: Cannot smooth: no data within fixed xrange!
The script below works on my machine. Maybe this is even what you are looking for...
set term x11 persist
set title "Animation curves"
set xlabel "Time (secs.)"
set ylabel "Parameter"
set x2label "Phoneme1" offset -35
set pointsize 2
set key off
set style line 2 lt 0 lc 1 lw 2
set xrange [0.0:1.0]
set yrange [0.0:1.3]
plot [0.0:0.8] "< cat file1.dat file2.dat | sort -n" u 1:2 smooth csplines ls 1, \
"" u 1:(0):(0):2 w vectors nohead ls 2, \
"" u ($1 + 0.005):(1):(sprintf("P %d", $0)) w labels, \
"file1.dat" u 1:2 w p, \
"file2.dat" u 1:2 w p