Plotting a histogram, wrong alignment of bars and labelling - gnuplot

I'm having trouble with a gnuplot, somehow the x labelling shifts to the right (or the bars to the left) when defining
set style data histograms
The Plotscript:
set boxwidth 0.9 absolute
set style fill solid 1.00 border lt -1
set key inside left top vertical Right noreverse noenhanced autotitles nobox
set style histogram clustered gap 5 title offset character 0, 0, 0
set datafile missing '-'
set style data histograms
set xtics border in scale 0,0 nomirror rotate by -45 offset character 0, 0, 0
set xtics norangelimit font ",8"
set xlabel "Scaling Factor"
set ylabel "Execution time [ms]"
set title "Database performance db4o vs Sqlite - Testcase 1 [indexed]"
set logscale y
set terminal pdf
set output 'db4o_vs_sqlite_indexed.pdf'
plot 'db4o_sb1-5idx.dat' using 3:xtic(1) title 'db4o', 'sqlite_sb1-5idx.dat' using 3 title 'Sqlite'
The data:
"Scaling Factor" "Testcase 1" "Testcase 2" "Testcase 3" "Testcase 4"
1 0 34 28 11
2 1 136 91 65
3 0 297 198 101
4 0 487 297 214
10 0 3124 1896 1567
20 0 13000 7907 7628

Related

Plotting histogram+linespoints in Gnuplot

I would like to connect selected bars in a histogram with linespoints. Now
I have created the histogram using the following code in Gnuplot:
set terminal postscript eps enhanced colour font 'Times-Roman,12' size 6in,4in
set output "LS_Figure_Histogram_New_2019_III.eps"
set xtics out scale 1.5
set ytics out scale 1.5
set y2tics out scale 1.5
set ytics nomirror
set yrange [-3.50:-3.70]
set y2range [0:45]
set key off
set tics font ", 14"
set xlabel "Experiment No." font ",16"
set ylabel "Li^{+}|Li vs. Cl^{-}|Cl_{2} (V)" font ",16"
set y2label "Residual Current Density (mA/cm^{2})" font ",16"
set key autotitle columnheader
set key inside left horizontal
set style fill solid 1.00 noborder
set style histogram clustered gap 1 title textcolor lt -1
set style data histograms
plot 'LS_Histogram_Comp_2019.dat' using 2:xtic(1) ls 2 ti col axis x1y1, '' u 3:xticlabels(1) ls 4 ti col axis x1y2
The data file is as given below
# Histogram_Comparison of onset potential & Residual current density
Experimental "Li^{+}|Li (V)" "Res. Current Density (mA/cm^{2})"
1 -3.64469 31.3
2 -3.65257 11.2
3 -3.66159 32.39
4 -3.66536 13.41
5 -3.66314 16.3
6 -3.64556 10.3
7 -3.64383 10.08
8 -3.63042 9.96
9 -3.65344 9.59
10 -3.64383 8.44
11 -3.64859 11.1
12 -3.63919 27.6
13 -3.64204 9.78
14 -3.62692 30.96
15 -3.64348 18.22
16 -3.6362 32.05
17 -3.62831 20.1
18 -3.62984 42.32
19 -3.63764 24.8
I wish to connect bars corresponding to Experimental 12, 14, 16 and 18 (using col 1 and 3 data) and bars 13, 15, 17 and 19 (using col 1 and 3 data) using linespoints.
Is there a way to do it in Gnuplot?
Try this
set terminal postscript eps enhanced colour font 'Times-Roman,12' size 6in,4in
set output "LS_Figure_Histogram_New_2019_III.eps"
set xtics out scale 1.5
set ytics out scale 1.5
set y2tics out scale 1.5
set ytics nomirror
set yrange [-3.50:-3.70]
set y2range [0:45]
set key off
set tics font ", 14"
set xlabel "Experiment No." font ",16"
set ylabel "Li^{+}|Li vs. Cl^{-}|Cl_{2} (V)" font ",16"
set y2label "Residual Current Density (mA/cm^{2})" font ",16"
set key autotitle columnheader
set key inside left horizontal
set style fill solid 1.00 noborder
set style histogram clustered gap 1 title textcolor lt -1
# ----------------------------------------
set rmargin screen 0.9
set lmargin screen 0.1
set bmargin screen 0.1
set multiplot
# ----------------------------------------
set style data histograms
plot 'LS_Histogram_Comp_2019.dat' using 2:xtic(1) ls 2 ti col axis x1y1, '' u 3:xticlabels(1) ls 4 ti col axis x1y2
# ----------------------------------------
unset tics
unset xlabel
unset ylabel
unset y2label
set xrange [0:20]
set yrange [0:45]
plot 'LS_Histogram_Comp_2019.dat' \
skip 12 every 2 using 1:3 w linespoints ls 1 lw 4 not,\
'' skip 13 every 2 using 1:3 w linespoints ls 3 lw 4 not
The result

How do I limit a gnuplot polar to a 180 degree range for Gnuplot 5.2?

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

Show Y Label in groups with Gnuplot

I have this points:
0.00049 1.509
0.00098 1.510
0.00195 1.511
0.00293 1.509
0.00391 1.510
0.00586 1.523
0.00781 1.512
0.01172 1.514
0.01562 1.510
0.02344 1.511
0.03125 1.510
0.04688 7.053
0.06250 7.054
0.09375 7.187
0.125 7.184
0.1875 7.177
0.25 7.207
0.375 16.588
0.5 24.930
0.75 39.394
1 56.615
1.5 77.308
2 84.909
3 89.056
4 88.485
6 88.678
8 89.022
12 88.513
16 88.369
24 88.512
32 88.536
48 87.792
64 87.716
96 87.589
128 87.608
192 87.457
256 87.388
And this gnuplot script:
#! /usr/bin/gnuplot
set terminal png
set output "lat_mem_rd.png"
set title "Memory Latency Benchmark (Stride 512)"
set xlabel "Memory Depth (MB)"
set ylabel "Latency (ns)"
set xtics rotate by 45 offset 0,-1
set xtics font "Times-Roman, 8"
set grid
set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1 # --- blue
plot "lat_mem_rd.dat" using (log($1)):2:xtic(1) smooth unique title "" with linespoints ls 1
which generates this graphic:
But i want to show the y values in the y label with one of the approximated values in those approximations, for example, for all of the values with x values between 3 and 256, the y label is set to just one, maybe 88.513 that corresponds to x=12 or other (or maybe the average of those points if its not very difficult)...
The same for x values between 0 and 0.02344 and for x values between 0.03125 and 0.1875.
This y values will substitute the values 10, 20, ..., 90.
Here is a modification of your script that might do what you want, if I understand you correctly:
set title "Memory Latency Benchmark (Stride 512)"
set xlabel "Memory Depth (MB)"
set ylabel "Latency (ns)"
set xtics rotate by 45 offset 0,-1
set xtics font "Times-Roman, 8"
set grid
a = ""; p = 0; nn = 1; nt = 37; d = 4; s = 0
f(x) = (x>p+d || nn >= nt)?(nn=nn+1, p=x, a=a." ".sprintf("%5.2f", s/n), n=1, s=x):(nn=nn+1, p=x, s=s+x, n=n+1)
plot "lat_mem_rd.dat" using 1:(f($2)) # Just to set array "a"
set ytics 0,0,0
set yrange [0:90]
set for [aa in a] ytics add (aa aa)
set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1 # --- blue
set terminal png
set output "lat_mem_rd.png"
plot "lat_mem_rd.dat" using (log($1)):2:xtic(1) smooth unique title "" with linespoints ls 1
This script produces this plot:
The strategy is to accumulate a sum of Y-values and calculate an average every time the Y-value increases by at least an amount d; these averages are stored in a string variable "a", which is looped over to set the ytic values before the final plot command. This way clusters of closely-spaced Y-values give rise to a ytic at their average value; I think that was what you wanted.

Gnuplot error bars values below 1 doesn't appear

I have those data:
Length A B C D E F A_err B_err C_err D_err E_err F_err
17 0,51 1,4 0 0 0 0,07 0,11 0,33 0 0 0 0,08
18 1,33 2,49 1,88 0,51 1,21 0,2 0,18 0,43 1,05 0,5 0,5 0,14
19 2,56 3,83 3,75 0,76 4,22 0,81 0,25 0,53 1,47 0,61 0,92 0,28
20 8,28 7,22 3,44 5,46 5,16 9,19 0,44 0,72 1,41 1,59 1,02 0,89
21 29,96 20 15,78 16,65 13,66 62,58 0,74 1,11 2,82 2,6 1,58 1,49
22 34,16 42,3 56,25 31,51 37,14 16 0,76 1,37 3,84 3,25 2,22 1,13
23 14,23 16,59 17,03 29,86 21,28 1,55 0,56 1,03 2,91 3,2 1,88 0,38
24 6,98 4,39 1,72 12,58 9,6 9,54 0,41 0,57 1,01 2,32 1,35 0,9
25 1,23 1,02 0,16 1,65 4,55 0,05 0,18 0,28 0,31 0,89 0,96 0,07
26 0,45 0,44 0 0,89 1,76 0 0,11 0,18 0 0,66 0,6 0
27 0,18 0,1 0 0 1,04 0 0,07 0,09 0 0 0,47 0
With this code, I obtain a nice histogram with error bars:
set terminal pngcairo enhanced font "arial,15" fontscale 2.0 size 1600,900
set output 'length.png'
set style fill solid 0.7 border lt -1
set key inside right top vertical Right noreverse noenhanced autotitles columnhead nobox
set grid ytics
set nokey
set style histogram errorbars linewidth 1 gap 3 title offset character 0, 0, 0
set datafile missing '-'
set style data histograms
set xtics border in scale 1,0.5 nomirror offset character 0, 0, 0 autojustify
set xtics norangelimit font ",12"
set xtics ()
set xlabel "n"
set ylabel "Percentage (%)"
set title "length"
set bars 0.3 front
set datafile separator "\t"
set yrange [ 0 : * ] noreverse nowriteback
plot 'length.dat' using 2:8:xtic(1), '' u 3:9:xtic(1), '' u 4:10:xtic(1), '' u 5:11:xtic(1), '' u 6:12:xtic(1), '' u 7:13:xtic(1)
The obtained image is here: http://i.stack.imgur.com/s88QJ.png
But, some error bars just not appear. I notice that is because the error value is below 1. But why ? I'd like all error bars appearing. Is there a problem in the code that forbid errors below 1 to appear ?
Thanks for your help
To me, it looks like your problem is that you're using , as a decimal point character in your file instead of . that gnuplot understands natively. There are two possible solutions. The first I found from googling, but can't test:
set locale
or
set locale "ja_JP.UTF-8" #gnuplot complains this isn't available for me ...
or perhaps set decimalsign local "..."
The second solution is a little less elegant, but it involves converting all of your , to . in your datafile. That's a trivial thing to do using sed:
sed -e 's/,/\./g' length.dat > length2.dat
Now you can just plot using length2.dat and it should work OK (it did for me). As an aside, I think that's quite a nicely colorful plot. The pngcairo terminal does a good job with it.

How to get a radial(polar) plot using gnu plot?

I want a radial(polar) plot using gnuplot (i.e in circular co-ordinate system(r,theta).
Here I have used the values:
Theta Max-strain
0 3400
60 5300
120 4700
180 3800
240 4100
300 3100
360 3400
How to get such a plot using gnu-plot?
I tried to recreate the plot of your question and this is what I came up with:
unset border
set polar
set angles degrees #set gnuplot on degrees instead of radians
set style line 10 lt 1 lc 0 lw 0.3 #redefine a new line style for the grid
set grid polar 60 #set the grid to be displayed every 60 degrees
set grid ls 10
set xrange [-6000:6000] #make gnuplot to go until 6000
set yrange [-6000:6000]
set xtics axis #disply the xtics on the axis instead of on the border
set ytics axis
set xtics scale 0 #"remove" the tics so that only the y tics are displayed
set xtics ("" 1000, "" 2000, "" 3000, "" 4000, "" 5000, "" 6000) #set the xtics only go from 0 to 6000 with increment of1000 but do not display anything. This has to be done otherwise the grid will not be displayed correctly.
set ytics 0, 1000, 6000 #make the ytics go from the center (0) to 6000 with incrment of 1000
set size square
set key lmargin
set_label(x, text) = sprintf("set label '%s' at (6500*cos(%f)), (6500*sin(%f)) center", text, x, x) #this places a label on the outside
#here all labels are created
eval set_label(0, "0")
eval set_label(60, "60")
eval set_label(120, "120")
eval set_label(180, "180")
eval set_label(240, "240")
eval set_label(300, "300")
set style line 11 lt 1 lw 2 pt 2 ps 2 #set the line style for the plot
#and finally the plot
plot "-" u 1:2 t "Max strain" w lp ls 11
0 3400
60 5300
120 4700
180 3800
240 4100
300 3100
360 3400
e
As you can see, the major difference is that the angle 0 is not on top but on the right (which is mathematically correct). You can change this however by modifying the using deceleration in the plot and the set_label function.
As you can see in the script not everything is really crisp and shiny. If someone finds improvements please let me know!
One last bit of "advice": It is not always reasonable to try to reproduce a plot from some tool with gnuplot. Often the strength of gnuplot is to plot data in the most simple way to be easily reproducible. Maybe you can kick some lines out of that script above and still be happy with it.
Here is a manual http://t16web.lanl.gov/Kawano/gnuplot/polar-e.html
set polar
set angles degrees
and to get circles:
set grid polar
Here is a demo: http://gnuplot.sourceforge.net/demo/polar.html

Resources