gnuplot histogram bars empty space - gnuplot

I am trying a very easy gnuplot graph.
Data is
Average 0.177 0.167
Median 0.179 0.173
and graph code:
set style fill solid border 0
set boxwidth 1.5
set style histogram clustered
set style data histograms
plot "PDR.txt" using 2:xtic(1) lt rgb "#406090",\"" 0" using 3 lt rgb "#40FF0
problem is that the graph produced has way too much space everywhere, in the middle, but especially left and right. How can I reduce those?

You should set the xrange to the desired length, e.g. set xrange [-0.25:1.5] looks alright on my computer

Related

gnuplot histogram chart with overlap

I would like to plot a bar chart or histogram like this in gnuplot.
I tried set style histogram rowstacked which is a start but it adds the columns on top of each other while I need them overlapped. Next is the issue of transparent color shading.
Thanks for your feedback.
UPDATE: user8153 asked for additional data.
The set style histogram clustered gap 0.0 is doing the cluster mode of the histogram bars. If you blur the eye it sort-of shows what I want but with overlap and transparent shading.
The only other histogram modes given in the docs are rowstacked and columnstacked. I never got a plot out of columnstacked so I discarded it. Now rowstacked stacks the histogram bars.
The overlay appearance is there but it is wrong. I don't want the stacked appearance. The histograms have to overlay.
Code :
set boxwidth 1.0 absolute
set style fill solid 0.5 noborder
set style data histogram
set style histogram clustered gap 0.0
#set style histogram rowstacked gap 0.0
set xtics in rotate by 90 offset first +0.5,0 right
set yrange [0:8000]
set xrange [90:180]
plot 'dat1.raw' using 3 lc rgb 'orange', \
'dat2.raw' using 3 lc rgb 'blue', \
'dat3.raw' using 3 lc rgb 'magenta'
Thanks for your feedback.
Given a sample datafile test.dat
-10 4.5399929762484854e-05
-9 0.0003035391380788668
-8 0.001661557273173934
-7 0.007446583070924338
-6 0.02732372244729256
-5 0.0820849986238988
-4 0.20189651799465538
-3 0.4065696597405991
-2 0.6703200460356393
-1 0.9048374180359595
0 1.0
1 0.9048374180359595
2 0.6703200460356393
3 0.4065696597405991
4 0.20189651799465538
5 0.0820849986238988
6 0.02732372244729256
7 0.007446583070924338
8 0.001661557273173934
9 0.0003035391380788668
10 4.5399929762484854e-05
you can use the following commands
set style fill transparent solid 0.7
plot "test.dat" with boxes, \
"test.dat" u ($1+4):2 with boxes
to get the following result (using the pngcairo terminal):
Using transparency as in user8153's solution is certainly the easiest way to visualize an overlap of two histograms.
This works even if the two histogram do not have identical bins or x-data-ranges.
However, the color of the overlap is pretty much bound to the colors of the two histogram and the level of transparency. Furthermore, if you want to show the overlap in the key you have to do it "manually".
Here is a solution where you can choose an independent color for the overlap area.
The overlap is basically the minimum y-value from both histograms for each x-value.
For this you need to compare the y-values for each x-value. This can be done in gnuplot with some "trick" by merging the two files line by line. This requires the data in a datablock (how to get it there from a file). Since this merging procedure is using indexing of datablock lines, it requires gnuplot>=5.2.0.
This assumes that you have the same x-range and bins for each histogram. If this is not the case, you have to implement some further steps.
Script: (works with gnuplot>=5.2.0, Sept. 2017)
### plot overlap of two histograms
reset session
# create some random test data
set samples 21
f(x,a,b) = 1./(a*(x-b)**4+1)
set table $Data1
plot '+' u 1:(f(x,0.01,-2)) w table
set table $Data2
plot '+' u 1:(f(x,0.02,4)) w table
unset table
set boxwidth 1.0
set grid y
set ytics 0.2
set multiplot layout 2,1
set style fill transparent solid 0.3
plot $Data1 u 1:2 w boxes lc 1 ti "Data1", \
$Data2 u 1:2 w boxes lc 2 ti "Data2"
set print $Overlap
do for [i=1:|$Data1|] { print $Data1[i].$Data2[i] }
set print
set style fill solid 0.3
plot $Data1 u 1:2 w boxes lc 1 ti "Data1", \
$Data2 u 1:2 w boxes lc 2 ti "Data2", \
$Overlap u 1:($2>$4?$4:$2) w boxes lc "red" ti "Overlap"
unset multiplot
### end of script
Result:

plotting two different types of histogram plots in gnuplot on top of each other

I am working with a histogram in gnuplot and I want to put one of the histogram bars behind the other results. I would like the reference bar (Mörk spennubjögunar > 200 kV) to behind the other histogram bars. I have done this in excel before using different y-axis, is there a nice way to do this in gnuplot?
This is the code I am currently working with.
set terminal pngcairo transparent nocrop enhanced size 3200,2400 font "arial,40"
set output "Harmonic_currents_BRE.png"
set key right
set datafile separator ";"
set style line 12 lc rgb '#808080' lt 0 lw 1
set style line 13 lt 0 lw 3
set grid back ls 12
set xrange [-1:20]
set yrange [0:8]
set style data histogram
set style histogram cluster gap 1
set style fill solid border 0
set border lw 2
set boxwidth 0.7
set ylabel "Hlutfall af nafnspennu [%]"
set xlabel "Tíðni [pu 50 Hz base]"
plot "case0.csv" using 2:xticlabels(1) title 'Tilfelli 0',\
"case1.csv" using 2:xticlabels(1) title 'Tilfelli 1',\
"case2.csv" using 2:xticlabels(1) title 'Tilfelli 2',\
"case3.csv" using 2:xticlabels(1) title 'Tilfelli 3',\
"ref.csv" using 2:xticlabels(1) title 'Mörk spennubjögunar > 200 kV'
unset output
unset zeroaxis
unset terminal
I think, the best way is to first plot the reference with boxes with a fixed boxwidth (I used 0.9), and then the clustered histograms:
set style data histogram
set style histogram cluster gap 1
set style fill solid noborder
set boxwidth 0.7
plot "ref.csv" using 0:2:(0.9):xtic(1) with boxes title 'Mörk spennubjögunar > 200 kV',\
for [i=0:3] sprintf("case%d.csv", i) u 2 title sprintf('Tilfelli %d), i)

Gnuplot won't clip my circle plot. Does set clip works?

I am trying to plot a "fat bands" bandstructure and I am having some trouble with gnuplot.
set terminal pngcairo size 1600,1200 enhanced font "Verdana, 40"
set output 'bandplot.png'
set ylabel 'Energy (eV)'
set yrange [-3:3] # set x range of the plot
set border lw 4
set key font ",25" spacing 1.0 samplen 0 at screen 0.55,0.8
set clip
plot 'bands.dat' u ($1):($2) with lines lt -1 lw 5 notitle,\
'bands.dat' u ($1):($2):(0.0015*$3) with circles lc rgb "blue" fs transparent solid 0.25 noborder title "test"
This data is basically a function in black line and over that there is a similar data with variable sized circles. The thing is set clip is not avoiding some of the circles to be drawn outside the graph. Does anyone know how to do it?
Also, I have seen some bug pages and this might not be possible to be accomplished. Thanks!
Works fine for me even without using set clip (tested with 5.0 and 4.6.6):
set xrange [-pi:pi]
set samples 21
plot '+' using 1:(cos($1)):(0.1) with circles fs solid

How to plot multiple y-axes?

I saw this graph and only for the curiosity sake was wondering whether it was possible to plot figure with multiple y-axis as in the figure
Many thanks!
As andyras wrote, you can use the second y-axis if you only have two datasets. In this case, you also need to to
set ytics nomirror # remove the tickmarks of the left ayis on the right side
set y2tics # make the right y-axis 'visible'
If you want to plot more than one dataset, I would suggest to use multiplot. You can overlay several independent plots and put a unique offset to the y-axis for each of them.
However, you need to take care that the number of y-tics and y-tick positions is the same.
Plot:
(I did not care about the key here, this still needs adjustment)
Code:
set multiplot
set xrange[0:10]
# We need place to the left, so make the left margin 30% of screen
set lmargin screen 0.3
##### first plot
set ytics 0.4
set yrange[-1.2:1.2]
set ylabel "Voltage" textcolor rgb "red"
plot sin(x)
##### Second plot
set ytics 1
set yrange[-3:3]
set ytics offset -8, 0
set ylabel "Current" offset -8, 0 textcolor rgb "green"
plot 3*cos(x) linecolor 2
##### Third plot
set ytics 0.5
set yrange[-1.5:1.5]
set ytics offset -16, 0
set ylabel "Power" offset -16, 0 textcolor rgb "blue"
plot 3*sin(x)*cos(x) linecolor 3
unset multiplot
Yes, you can have two y axes for free, e.g.
plot x, x**2 axes x1y2
The axes specification lets you put things on x1y1, x2y1, etc. If you want more than two things plotted on the same y axes you have to normalize things yourself:
plot 'data1.dat' using 1:($2/MAX_1), \
'data2.dat' using 1:($2/MAX_2), \
'data3.dat' using 1:($s/MAX_3)
The variables MAX_X can be precalculated by using the stats command in gnuplot 4.6+, or you can put them in manually.

Gnuplot histogram x logscale

I'm using gnuplot in a bash script to draw several things.
For this special graphic, I need to print the amount of matrices (y axis) with the matrix size as the x-axis.
As the distribution can be pretty sparsed, I want to use a logscale for x and y. It works great with y, but gnuplot tells me I can't have a logscale for the x-axis when I'm using histogram style.
Any ideas to debug this? or on how to present the results using a similar way?
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set logscale xy
plot '$res/histo-$ld-$lr-$e-$r' using 2:xtic(1) title 'Run'
The error is :
line 0: Log scale on X is incompatible with histogram plots
Thanks in advance.
Edit : btw, I was using gnuplot 4.4 patchlevel 4 and just updated to the newest version (i.e. 4.6 patchlevel 5)
Gnuplot histograms work a bit differently from what you might think. The x-axis isn't numeric. In your case the value in the first row, second column is placed at an x-value of 0 with the y-value taken from the second column and a manual label taken from the first column, first row. The values of the second row are placed at x=1 etc.
You can try using the boxes plotting style, which is used with a 'conventional' x-axis and supports a logscale in x:
set logscale xy
set offset 0,0,1,1
set boxwidth 0.9 relative
set style fill solid noborder
plot 'data.dat' with boxes
With the data file data.dat
1 1000
2 300
5 150
20 10
135 3
this gives the result (with 4.6.5):
In order to have a fixed boxwidth and a varying box distance, you can use a third column to specify a box width as percentage of the x-value:
set logscale xy
set offset 0,0,1,1
set style fill solid noborder
plot 'data.dat' using 1:2:($1*0.5) with boxes
Putting the actual values on the x-axis works as follows:
set logscale xy
set offset 0,0,1,1
set style fill solid noborder
plot 'data.dat' using 1:2:($1*0.5):xtic(1) with boxes

Resources