gnuplot: bar charts visualization - gnuplot

My gnuplot script plot a bar graph for 2D data either in monochrome or color format:
set term pngcairo size 800,600
set termoption noenhanced
set tics font "Helvetica,10"
#set xtics noenhanced
set ylabel "Fraction, %"
set xlabel "H-bond donor/aceptor, residue"
set yrange [0:1]
set ytics 0.1
set grid y
set key off
set boxwidth 0.9
set style fill solid 0.5
# TWO OPTIONS FOR BAR VISUALISATIONS!! NB: ADD HERE TRIGGER FROM COLOR_DATA TRIGGER
# 1 - use it with non-colored bars"
#plot "\$data" using 0:2:xtic(1) with boxes, "" using 0:2:2 with labels offset 0,1
# 2 - or use it with colored bars:
plot \$data using 0:2:3:xtic(1) with boxes lc rgb var, \
'' using 0:2:2 with labels offset 0,1
The problem when I have just one bar, one the graph it occupiers all the graph on X:
Would it be possible to set some minimum dimension for the bars to make the dimensions of a single bar similar for a situations with two bars, for instance:

My understanding is the following: if you have only one box:
maybe gnuplot tries to autoscale and the automatic boxwidth is small relative to the autorange (hence just a thin line).
if you set a certain boxwidth, autoscale with scale to the given boxwidth (hence the graph filled with the box).
you could set a fixed xrange, but then you are loosing the benefits of autoscale. Instead you can use set offets (check help offsets).
if you have more than 1 box autoscale will work.
Script:
### boxwidth with boxes style
reset session
$Data1 <<EOD
1 Abc
EOD
$Data2 <<EOD
1 Abc
2 Xyz
EOD
set style fill solid 0.3
set key out
set rmargin screen 0.7
set yrange[0:]
set ytics 0.5
set multiplot layout 4,1
plot $Data1 u 0:1:xtic(2) w boxes ti "No special settings"
set boxwidth 0.9
plot $Data1 u 0:1:xtic(2) w boxes ti "set boxwidth"
set offsets 1,1,0,0
plot $Data1 u 0:1:xtic(2) w boxes ti "set offsets"
set offsets 0,0,0,0
plot $Data2 u 0:1:xtic(2) w boxes ti "more than 1 box"
unset multiplot
### end of script
Result:

Related

How to move and manage overlapping of x ticks with axis in Gnuplot

I am trying to plot an inlet graph which is shown in Figure. Being an inlet graph it is needed to show x tics and y ticks of relatively big sizes for clear visibility. But when I increase the fonts as,
set xtics font ", 40"
tics overlaps with axis. I increased the plot size set term png size 1000, 1000 but still the issue persists. Kindly suggest if there is a way to move the tics below or to a desired position in graph.
Edit:
The gnuplot script looks like this,
set term png size 1000, 1000
set output "b_vs_N.png"
set style fill solid
set style circle radius 0.001
FIT_LIMIT=1.e-14
set yrange [0.15:0.25]
set style fill solid
set style circle radius 0.001
set xtics 10
set ytics 0.03
set border 15 back lw 6
set xtics font ", 40"
set ytics font ", 22"
set ylabel "b" enhanced font "1 {,40}"
set xlabel "N_i" font "1 {,40}"
set lmargin 12
set bmargin 4
set palette model HSV
set palette rgb 3,2,2
set palette maxcolors 12
set view map
AA(x)=a+b*x+c*x**2
fit AA(x) "data.txt" using 1:2 via c, b, a
plot "data.txt" using 1:2 lt 1 pt 11 ps 2.0 lc rgb "black" notitle, AA(x) w lines lw 2 lc rgb "sienna1" notitle
Your example is uncomplete without code and therefore difficult to reproduce. Please check help xtics. There is the option offset.
Maybe the following example helps to solve your issue.
In the example below no special offset seems to be necessary, i.e. offset 0,0, but you can shift and adjust the labels in x and y direction.
Code:
### tic label offset
reset session
set multiplot
plot sin(x)/x
set origin 0.07, 0.6
set size 0.3,0.3
set xrange [0:10]
set xtics 5 out font ",20" offset 0,0
plot x**2
unset multiplot
### end of code
Result:

Customizing gnuplot clustered histogram x-axis labels

I have a gnuplot script
set terminal qt size 850,500 enhanced font 'Verdana,12' persist
set boxwidth 0.9 absolute
set ylabel "Duration in milliseconds" #font "Arial 14"
set tics font "Monospaced,bold 14"
set style fill solid 1.00 border lt -1
set key inside right top vertical Right noreverse noenhanced autotitle nobox
set style histogram cluster gap 1 title textcolor lt -1
set minussign
set datafile missing '-'
set style data histograms
set xtics border in scale 0,0 nomirror rotate by -45 autojustify
set xtics norangelimit
set xtics ()
set title "Integer d-ary heap performance"
set yrange [0:*]
set style line 1 lt 1 lc rgb "#000000"
set style line 2 lt 2 lc rgb "green"
plot 'integer_heap_benchmark_dary.dat' using 2:xtic(1) linecolor rgb "#5555ff" title col, \
'' using 3:xtic(1) linecolor rgb "#f4dc42" title col, \
'' using 4:xtic(1) title col
which looks like this:
What I want to achieve is:
Move each x-axis label a little bit to the left so that it "points" to the center of each cluster,
Change the font and size of the x-axis labels.
I have put an effort to find the answers on my own, yet without any success.
Try something like
set tics font "Courier,24"
to set the font.
To center the labels, try something like
set xtics offset -3
You'll have to experiment with the values.

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)

Histogram using gnuplot with multiple y-axes

I could not find a solution to the following issue I am facing. All SO questions on multi-axis talk about line plots, but I am looking for histograms.
The y-range for the bars are different, so one set of bars are not really seen because of the scale. Here is the data:
Metric A B
M1 0.613416301 0.543734744
M2 0.000195961 0.000100190
Here is the MWE:
reset
set term postscript eps size 5.5,4.5 enhanced color font 'Arial-Bold' 25
set out 'histplot.eps'
set key right
set style histogram cluster gap 2
set style data histograms
set style fill pattern 1.00 border
set y2range [0.0001:0.0002]
plot 'histplot.dat' using 2 ti col, '' u 3:xticlabels(1) ti col
quit
This is the sample output (one set of bars over M2 is not seen):
What I prefer is to have a second y-axis (to the right side of the plot) with a range appropriate to the second row of my data file. Is this possible? Any help is greatly appreciated.
Normally you can plot severyl histograms beneath each other using newhistogram. However, it seems like this is buggy when using patterns as fillstyle:
reset
set style histogram cluster gap 1
set style data histograms
set style fill pattern 1.00 border
set yrange [0:*]
set ytics nomirror
set y2range [0:*]
set y2tics
set key right autotitle columnheader
plot 'histplot.dat' u 2 every ::::0, '' u 3:xtic(1) every ::::0,\
newhistogram lt 1 at 1,\
'histplot.dat' u 2 every ::1::1 axes x1y2, '' u 3:xtic(1) every ::1::1 axes x1y2
Alternatively you can use multiplot and plot the two histograms directly beneath each other:
reset
set style histogram cluster gap 1
set style data histograms
set style fill pattern 1.00 border
set yrange [0:*]
set ytics nomirror
set multiplot layout 1,2
set rmargin at screen 0.5
set lmargin 9
unset key
plot 'histplot.dat' using 2 every ::::0 ti col, '' u 3:xticlabels(1) every ::::0 ti col
set rmargin 9
set lmargin at screen 0.5
unset ytics
set y2range [0:*]
set y2tics
set key right
plot '' using 2 every ::1::1 axes x1y2 ti col, '' u 3:xtic(1) every ::1::1 axes x1y2 ti col
unset multiplot
If you don't want the separating black line, you can use set border 7 for the first and set border 13 for the second plot.

Gnuplot: Multiplot Plot Just Curve Without Axis Or Title etc

I've used the following script to generate a plot and the result is shown in the figure below. It is hard to see, but the xlabel, ylabel, title and tic numbers have actually been drawn over and over again each time a plot function was called while in multiplot. In ideas how I can avoid this and just plot the graph without anything else? If I unset the title, tics etc and then plot, then the graph does not plot in the same area as the frame and petrudes into where the left y-axis is.
#set datafile separator ' '
set samples 1000
set term tikz size 17cm,10cm dashed
set out 'MosfetClassAbPower.tex'
unset key
set border lw 2
set style fill transparent solid 0.5 noborder
set title 'MOSFET $\mathrm{I_D}$ Vs Time'
set ylabel 'Drain Current [$\mu$A]'
set xlabel 'Time [ms]'
set xrange [0:4]
set xtics 0,0.5,4
set mxtics 4
set yrange [-50:450]
set mytics 4
set rmargin 5
set label 1 '\SI{60}{\micro\ampere}' at 4.02,60
set multiplot
set grid mxtics mytics lt -1 lc rgb 'gray90'
plot NaN notitle
unset grid
set grid xtics ytics lt -1 lc rgb 'gray70'
plot NaN notitle
unset grid
plot NaN notitle
Id(x) = 347*sin(2*3.14*x) + 60
ID(x) = Id(x) >= 0 ? Id(x) : 0
plot ID(x) w filledcurves above y1=0 lc rgb 'light-blue',\
60 w lines lt 2 lw 3 lc rgb 'gray60',\
ID(x) w lines lt 1 lw 5 lc rgb 'navy'
plot NaN notitle
unset multiplot
set out
My attempt at preventing the curve from protruding over the frame.
Edit:
reset
#set term tikz size 17cm,10cm dashed standalone header '\usepackage{siunitx}'
#set out 'MosfetClassAbPower.tex'
#TSCALE = 1.0
set terminal pdfcairo dashed
set out 'MosfetClassAbPowerFixed.pdf'
TSCALE = 20.0 # use this value for e.g. pdfcairo or cairolatex
TITLE = 'MOSFET $I_D$ Vs Time'
YLABEL = 'Drain Current (in \si{\uA})'
XLABEL = 'Time (in \si{\ms})'
set style fill transparent solid 0.5 noborder
set xrange [0:4]
set xtics 0,0.5,4
set mxtics 4
set yrange [-50:450]
set mytics 4
set rmargin 5
LABEL = '\SI{60}{\uA}'
set label 1 LABEL at graph 1.01, first 60
unset key
set samples 1000
set multiplot
set title TITLE
set ylabel YLABEL
set xlabel XLABEL
unset border
set tics scale 0,0.001
set grid mxtics mytics lt -1 lc rgb 'gray90'
plot NaN
unset grid
# keep the current margins for all following plots
set lmargin at screen TSCALE*GPVAL_TERM_XMIN/(1.0*GPVAL_TERM_XSIZE)
set rmargin at screen TSCALE*GPVAL_TERM_XMAX/(1.0*GPVAL_TERM_XSIZE)
set tmargin at screen TSCALE*GPVAL_TERM_YMAX/(1.0*GPVAL_TERM_YSIZE)
set bmargin at screen TSCALE*GPVAL_TERM_YMIN/(1.0*GPVAL_TERM_YSIZE)
# unset almost everything
unset border
unset label
unset xlabel
unset ylabel
set format x ''
set format y ''
unset title
set grid xtics ytics lt -1 lc rgb 'gray70'
plot NaN
unset grid
Id(x) = 347*sin(2*3.14*x) + 60
ID(x) = Id(x) >= 0 ? Id(x) : 0
plot ID(x) w filledcurves above y1=0 lc rgb 'light-blue',\
60 w lines lt 2 lw 3 lc rgb 'gray60',\
ID(x) w lines lt 1 lw 5 lc rgb 'navy'
# overdraw borders on left, right, top, bottom
set object 1 rectangle from screen 0, screen 0 to graph 0, screen 1 back \
fillstyle solid noborder
set object 2 rectangle from graph 1, screen 0 to screen 1, screen 1 back \
fillstyle solid noborder
set object 3 rectangle from screen 0, graph 1 to screen 1, screen 1 back \
fillstyle solid noborder
set object 4 rectangle from screen 0, screen 0 to screen 1, graph 0 back \
fillstyle solid noborder
plot NaN
unset object 1
unset object 2
unset object 3
unset object 4
set title TITLE
set ylabel YLABEL
set xlabel XLABEL
set label 1 LABEL at graph 1.01, first 60
set format x
set format y
set tics scale 1,0.5 front
set border
set border lw 2
plot NaN
unset multiplot
set out
It is not possible, to set different layers for all plot elements and stack them arbitrarily. You must play around with set and unset for the various elements.
In order to have the tics drawn only once, I set their scale to 0 (this works for the major tics, but not for the minor tics, where I use 0.001).
I fix the margins after the minor grid lines are drawn (see Gnuplot: Store plot area dimensions for later use).
Unset everything, which shouldn't be drawn again (label, object, arrow, tics labels etc). Do not unset tics, because we want to drawn them last, so just use set format x '' to draw the tics, but not their labels.
Set the tics to their default scale, and set the border before the last plot, to have them drawn above the grid lines and above the plot.
reset
set term tikz size 17cm,10cm dashed standalone header '\usepackage{siunitx}'
set out 'MosfetClassAbPower.tex'
TSCALE = 1.0
# set terminal pdfcairo
# TSCALE = 20.0 # use this value for e.g. pdfcairo or cairolatex
set style fill transparent solid 0.5 noborder
set title 'MOSFET $I_D$ Vs Time'
set ylabel 'Drain Current (in \si{\uA})'
set xlabel 'Time (in \si{\ms})'
set xrange [0:4]
set xtics 0,0.5,4
set mxtics 4
set yrange [-50:450]
set mytics 4
set rmargin 5
set label 1 '\SI{60}{\uA}' at graph 1.01, first 60
unset key
set samples 1000
set multiplot
unset border
set tics scale 0,0.001
set grid mxtics mytics lt -1 lc rgb 'gray90'
plot NaN
unset grid
# keep the current margins for all following plots
set lmargin at screen TSCALE*GPVAL_TERM_XMIN/(1.0*GPVAL_TERM_XSIZE)
set rmargin at screen TSCALE*GPVAL_TERM_XMAX/(1.0*GPVAL_TERM_XSIZE)
set tmargin at screen TSCALE*GPVAL_TERM_YMAX/(1.0*GPVAL_TERM_YSIZE)
set bmargin at screen TSCALE*GPVAL_TERM_YMIN/(1.0*GPVAL_TERM_YSIZE)
# unset almost everything
unset border
unset label
unset xlabel
unset ylabel
set format x ''
set format y ''
unset title
set grid xtics ytics lt -1 lc rgb 'gray70'
plot NaN
unset grid
set tics scale 1,0.5 front
set border
set border lw 2
Id(x) = 347*sin(2*3.14*x) + 60
ID(x) = Id(x) >= 0 ? Id(x) : 0
plot ID(x) w filledcurves above y1=0 lc rgb 'light-blue',\
60 w lines lt 2 lw 3 lc rgb 'gray60',\
ID(x) w lines lt 1 lw 5 lc rgb 'navy'
unset multiplot
set out
Result:
Now the ordering is:
minor grid lines
major grid lines
curve
border, tics
Note, that I made some other tiny changes: You can use e.g. graph coordinates to set a label. And some tweaking of the label text.
EDIT:
Cairolatex or epslatex
The proceeding described above works well for any terminal which processes text and graphics together, but not for terminals like cairolatex and epslatex which also in multiplot mode know only two text layer:
front layer, contains all text placed with front keyword.
graphics, contains all graphical elements of all plot commands (also in multiplot mode).
back layer, contains all text placed with back keyword.
This may become a problem, when one wants to cover parts of the graphic (protruding lines) with a white object, but cannot put e.g. the xlabel to the front. Here is an example, which works also with cairolatex:
reset
set terminal cairolatex pdf dashed color standalone header "\\usepackage{siunitx}" size 17cm,10cm
set output 'MosfetClassAbPowerFixed.tex'
TITLE = 'MOSFET $I_D$ Vs Time'
YLABEL = 'Drain Current (in \si{\uA})'
XLABEL = 'Time (in \si{\ms})'
set style fill transparent solid 0.5 noborder
set xrange [0:4]
set xtics 0,0.5,4
set mxtics 4
set yrange [-50:450]
set mytics 4
RMARGIN=0.92
LMARGIN=0.1
set rmargin at screen RMARGIN
set lmargin at screen LMARGIN
set tmargin at screen 0.91
set bmargin at screen 0.11
unset key
set samples 1000
set multiplot
# first plot the minor grid lines
unset border
set tics scale 0,0.001 format ''
set grid mxtics mytics lt -1 lc rgb 'gray90'
plot NaN
# now plot the major grid lines
unset grid
set grid xtics ytics lt -1 lc rgb 'gray70'
plot NaN
unset grid
# plot the actual curve
# overdraw borders on left and right
set object rectangle from graph -0.005, graph 0 to screen LMARGIN, graph 1 front \
fillstyle solid noborder
set object rectangle from screen RMARGIN, graph 0 to graph 1.005, graph 1 front \
fillstyle solid noborder
Id(x) = 347*sin(2*3.14*x) + 60
ID(x) = Id(x) >= 0 ? Id(x) : 0
plot ID(x) w filledcurves above y1=0 lc rgb 'light-blue',\
60 w lines lt 2 lw 3 lc rgb 'gray60',\
ID(x) w lines lt 1 lw 5 lc rgb 'navy'
unset object
# plot all tics and labels
LABEL = '\SI{60}{\uA}'
set label 1 LABEL at graph 1.01, first 60 front
set title TITLE
set ylabel YLABEL
set xlabel XLABEL
set tics scale 1,0.5 format
set border
set border lw 2
plot NaN
unset multiplot
set out
Because of the only three layer, I put thin white rectangles between the plot border and the tic labels. To have the objects drawn outside the plotting area, one needs to use at least one coordinate value in screen coordinates, otherwise they are clipped.
As opposed the the first example, I used fixed margins for the whole plot, which I prefer.
This gives:

Resources