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.
Related
I am trying to plot a data file similar to the sample given below albeit much bigger.
1.0 12.0 0.03
2.0 14 0.586
10.0 16 0.798
50.0 18 1.023
250.0 28.9 1.124
1000.0 30.2 1.456
I want to plot column 1 with 3 while column 2 will be my upper axis ticks. The code the given below:
reset
set logscale x
set logscale y
set xtics nomirror
set xtics (1 ,100, 1000)
set x2tics (1, 100, 1000)#(x2tics is not changing)
set autoscale xfix
set autoscale x2fix
set xlabel 'x'
set ylabel 'y'
set x2label 'm'
plot 'data_test.txt' using 1:3:x2tic(2) with linespoints ps 2 lw 2 title 'y wrt x'
The initial ticks are all overlapping and I want to reduce the ticks. However, changing x2tics is not fixing the issue. Kindly help.
You are missing a set logscale x2.
Full code:
reset
set logscale x
set logscale y
set logscale x2
set xtics nomirror
set xtics (1 ,100, 1000)
# The next line would plot a confusing 100 between 18 and 28.9,
# so I commented it out
# set x2tics (1, 100, 1000)#(x2tics is not changing)
# But we have to enable x2tics
set x2tics
set autoscale xfix
set autoscale x2fix
set xlabel 'x'
set ylabel 'y'
set x2label 'm'
plot 'data_test.txt' using 1:3:x2tic(2) with linespoints ps 2 lw 2 title 'y wrt x'
The result:
Update
To reduce the number of x2tics, I would try two plots: The first plots the data points, the second one only plots the x2tics:
plot 'data_test.txt' using 1:3 with linespoints ps 2 lw 2 title 'y wrt x', \
'data_test.txt' using 1:(NaN):x2tic(2) every 2 notitle
Here, NaN "hides" the data points, and every 2 skips every 2nd line.
I am creating a seemingly duplicate question posted by Matt in 2015, however, the answer posted by Christoph doesn't work at the moment for me, since I'm using Gnuplot 5.2.
When I use Matt's input date and Christoph's script, I end up with this result:
Picture here
As can be seen, the bottom half (3rd & 4th quadrant) of the plot is plotted, yet it should not be.
reset
set terminal pngcairo font ',10'
set polar
set angle degrees
set size ratio 1
set lmargin 8
set style line 11 lc rgb 'gray80' lt -1
set grid polar ls 11
unset border
unset tics
set xrange [-1:1]
set yrange [0:1]
set size ratio -1
r = 1
set rtics 0.166 format '' scale 0
set label '0°' center at first 0, first r*1.05
set label '-90°' right at first -r*1.05, 0
set label '+90°' left at first r*1.05, 0
set for [i=1:5] label at first r*0.02, first r*((i/6.0) + 0.03) sprintf("%d dB", -30+(i*5))
unset raxis
set key outside top right
set output 'polar.png'
plot 'norm_polar_1000.txt' w lp ls 1 t '1k'
Data:
180 0.657067
172.5 0.6832
165 0.717767
157.5 0.7461
150 0.7747
142.5 0.806167
135 0.835633
127.5 0.865167
120 0.890533
112.5 0.918133
105 0.929633
97.5 0.9566
90 0.9632
82.5 0.9566
75 0.929633
67.5 0.918133
60 0.890533
52.5 0.865167
45 0.835633
37.5 0.806167
30 0.7747
22.5 0.7461
15 0.717767
7.5 0.6832
0 0.657067
To get to my own problem, I would like to get a very similar plot but with only 2nd and 3rd quadrant instead.
My code:
reset
set terminal pngcairo font ',12'
set polar
set angle degrees
set size ratio -1
set tmargin 3
set bmargin 3
set style line 11 lc rgb 'gray80' lt -1
set grid polar ls 11
unset border
unset tics
unset key
r=1
set rrange [0:r]
set xrange [-1:0]
set yrange [-1:1]
rOffset = 1.1
set rtics 0.166 format '' scale 0
set label '0°' center at first 0, first r*rOffset
set label '90°' right at first -r*rOffset, 0
set label '180°' center at first 0, first -r*rOffset
set output 'TestPolar.png'
plot 'exampleData.txt' u ($1+90):2
exampleData.txt:
10 0.1
30 0.2
50 0.3
70 0.4
90 0.5
110 0.6
130 0.7
150 0.8
170 0.9
Current resulting picture here
Any ideas?
It looks to me that gnuplot's trange in 5.2 is always in radians rather than degrees, even if the data itself is in degrees. That seems like a bug but you can work around it.
In 5.2 do not set xrange or yrange to limit data in polar mode; use rrange and trange. xrange and yrange affect the entire plot layout, not just the data.
Also note that you can move the origin on theta to the top using command set theta top. If you do this you need not add 90° to each data point.
Assuming that by "only the 2nd and 3rd quadrant" you mean 90 < theta < 270, your script becomes
set polar
set angle degrees
set size ratio -1
set tmargin 3
set bmargin 3
set style line 11 lc rgb 'gray80' lt -1
set grid polar ls 11
unset border
unset tics
unset key
r=1
set rrange [0:r]
set trange [ pi/2 : 3*pi/2 ]
set theta top
set rtics 0.166 format '' scale 0
set ttics (0,90,180) format "%g°"
plot 'exampleData.txt' using ($1):2 with lines
Note that I set trange in radians rather than degrees to work around the bug.
Output using gnuplot 5.2.4 is attached
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'
Given dataset:
0 t1 0.52
1 t2 0.66
2 t3 0.58
3 t4 0.57
4 t5 0.68
5 t6 0.61
6 t7 0.55
7 t8 0.52
8 t9 0.58
9 t10 0.50
10 t11 0.59
I cannot manage to get the colors of the bars to change. What I'm trying below is to color green the highest score which happens on line 4. Please have a look over the below:
set terminal postscript eps enhanced 20
set output "edscore2_joint.eps"
set style line 1 lc rgb "#5F9EA0"
set style line 2 lc rgb "#DC143C"
set style line 3 lc rgb "green"
set yrange [0:1]
set ylabel "ed_{score}"
set xlabel "Technique"
set style fill solid
set boxwidth 0.5
set xtics rotate by -45
plot "edscore2_joint.dat" using 1:3:xtic(2) with boxes ls 1 fillstyle pattern 1 notitle ,\
"edscore2_joint.dat" every ::4::4 using 1:3:xtic(2) with boxes ls 3 fillstyle pattern 2 notitle ,\
"edscore2_joint.dat" using 1:($3+0.05):3 with labels notitle
The terminal postscript is monochrome by default. Use either the option color, or, if you have a recent enough gnuplot version, use the terminal epscairo.
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!