Plotting histogram+linespoints in Gnuplot - 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

Related

How to use gnuplot to plot history graph with labels

I have two small datafiles
one
11 365.4
12 659.2
and one
11 432.1
12 882.4
I try to plot those as a histogram with labels
gnuplot <<EOF
set output 'house-energy.png'
set terminal png size 800,400 font "Arial,10"
set boxwidth 0.7 relative
set grid ytics linestyle 0
set style fill solid 0.20 border
set style data histogram
#set style histogram columnstacked
#set style histogram rowstacked
set title "Energy"
set xlabel "Month"
set ylabel "kWh"
set yrange [0:2000]
plot 'file1.dat' u 2: xtic(1) with histogram lc rgb "#0045FF" title "Energy house total", \
'' using 1:(\$2):(\$2) with labels notitle font ",10" , \
'file2.dat' u 2: xtic(1) with histogram lc rgb "#004500" title "Labb", \
'' using 1:(\$2):(\$2) with labels notitle font ",10"
EOF
But the labels are way off
Image showing plot
// GH
The histogram plotting style is implicitly using the pseudocolumn 0 (check help pseudocolumns) as x-coordinate. So, you have to place your label at column(0) or ($0) with some x-offset, e.g. ($0-0.15) in the one and other direction. And some offset in y-direction, e.g. via offset 0,0.7.
Script:
### histogram with labels
reset session
$Data1 <<EOD
11 365.4
12 659.2
EOD
$Data2 <<EOD
11 432.1
12 882.4
EOD
set boxwidth 0.7 relative
set grid ytics linestyle 0
set style fill solid 0.20 border
set style data histogram
set title "Energy"
set xlabel "Month"
set ylabel "kWh"
set yrange [0:2000]
set key noautotitle
plot $Data1 u 2:xtic(1) w histogram lc rgb "#0045FF" title "Energy house total", \
'' u ($0-0.15):2:2 w labels font ",10" offset 0,0.7, \
$Data2 u 2 w histogram lc rgb "#004500" title "Labb", \
'' u ($0+0.15):2:2 w labels font ",10" offset 0,0.7
### end of script
Result:

How to plot several Y axis in the left side using gnuplot

I am plotting same plot as from here How to plot multiple y-axes?. But also i would like them to look like in the image below. With additional lines and tics, how can i do it?
As mentioned in the answer you linked, you can plot many plots on top of each other in the multiplot environment.
So, here you are plotting 6 plots on top of each other
plot 1, 2, 3 for the data/functions
plot 4, 5, 6 for the colored axes
and plot 6 as well for the key on top of everything
In order to get the same scale for the plot and the "external" axes I would recommend to store the ranges and step sizes in the variables y10,y11,y1d, ...., such that you have to change the values only at one location.
Script:
reset session
set key opaque box noautotitle
set multiplot
set margins screen 0.3, screen 0.95, screen 0.95, screen 0.1 # l,r,t,b
# first plot
set xrange[0:10]
set xtics 2
y10 = -1.2
y11 = 1.2
y1d = 0.4
set yrange [y10:y11]
set ytics y1d
set format y '' # no ytic labels
set grid x,y
plot sin(x) lc "red"
# second plot
set border 0 # no border
unset tics # no tics
unset grid # no grid
y20 = -3.0
y21 = 3.0
y2d = 1.0
set yrange [y20:y21]
plot 3*cos(x) lc "green"
# third plot
y30 = -1.5
y31 = 1.5
y3d = 0.5
set yrange [y30:y31]
plot 3*sin(x)*cos(x) lc "blue"
# plot for axis 1
set lmargin screen 0.27
set border 2 lc "red" lw 2
set ylabel "Voltage" tc "red" offset 1.5,0
set yrange [y10:y11]
set format y
set ytics y1d nomirror
plot NaN
# plot for axis 2
set lmargin screen 0.17
set border 2 lc "green" lw 2
set ylabel "Current" tc "green" offset 0.5,0
set yrange [y20:y21]
set ytics y2d
plot NaN
# plot for axis 3 and key
set lmargin screen 0.08
set border 2 lc "blue" lw 2
set ylabel "Power" tc "blue" offset 1,0
set yrange [y30:y31]
set ytics y3d
plot NaN w l lc "red" ti "sin(x)", \
NaN w l lc "green" ti "3*cos(x)", \
NaN w l lc "blue" ti "3*sin(x)*cos(x)"
unset multiplot
### end of script
Result:

Setting the size of title in gnuplot

I have the following script to place in a bash script:
#!/bin/bash
set -e
gnuplot -persist <<-EOFMarker
set pixmap 1 "pathToimage/fig.png" at screen 0.85, 0 width screen 0.15 behind
set rmargin at screen 0.85
# Border line definition
set border lw 1
# Major and Minor grid definition
set style line 100 lt 1 lc rgb "gray" lw 2
set style line 101 lt 0.5 lc rgb "gray" lw 1
set grid mytics ytics ls 100, ls 101
set grid mxtics xtics ls 100, ls 101
set title 'myTitle' font ", 30"
set ylabel 'y [m]' font "Helvetica, 12"
set xlabel 'x [m]' font "Helvetica, 12"
set tics font "Helvetica,10"
plot [-10:10] sin(x),atan(x),cos(atan(x))
EOFMarker
The script is intended to do some plotting and place a logo on the corner of the plot. However, I am not able to change the size of the fonts... I am using gnuplot 5.4.3.
Any advice?
Have you tried to set a terminal? Maybe your default terminal does not support changing the title size.
Using this script, I can set the title size:
set pixmap 1 "linux.png" at screen 0.85, 0 width screen 0.15 behind
set rmargin at screen 0.85
# Border line definition
set border lw 1
# Major and Minor grid definition
set style line 100 lt 1 lc rgb "gray" lw 2
set style line 101 lt 0.5 lc rgb "gray" lw 1
set grid mytics ytics ls 100, ls 101
set grid mxtics xtics ls 100, ls 101
set term png # new
set o "output.png" # new
set title 'myTitle' font ", 30"
set ylabel 'y [m]' font ", 12" # removed font spec
set xlabel 'x [m]' font ", 12" # removed font spec
set tics font ",10"
plot [-10:10] sin(x),atan(x),cos(atan(x))
set o
The output is
If I set the font size to 12, I get

Shifting of axis title and sub-title in Gnuplot histogram

Mr data file is
# test.dat
# Sample No. Phenol Red Neutral Red Bromophenol Blue Bromocresol Green Mixed Indicator
1 80 69 223 222 130
2 89 62 321 267 160
3 92 67 248 276 147
Gnuplot code is
#
#
###
reset session
###
set terminal postscript eps enhanced colour font 'Times-Roman,12' size 6in,4in
set output "test.eps"
#------------------------------------------------------------------------------
set style line 1 lt 4 lw 1 lc rgb "#4169E1"
set style line 2 lt 4 lw 1 lc rgb "#2E8B57"
set style line 3 lt 5 lw 1 lc rgb "#FF4500"
set style line 4 lt 4 lw 1 lc rgb "#FAD02C"
set style line 5 lt 4 lw 1 lc rgb "#FF0080"
#------------------------------------------------------------------------------
set xtics out scale 1.5
set ytics out scale 1.5
set ytics nomirror
set yrange [0:350]
set key off
set tics font ", 16"
set xlabel "Sample No. " font ",16"
set ylabel "Moisture Content ({/Symbol \155}g of H_{2}O/g of melt)" font ",16"
set key samplen 2 spacing 1.65 font ",14" width -1.0
set key inside left horizontal
set style fill solid border
set style histogram clustered gap 1 title textcolor lt -1
# ----------------------------------------
set rmargin screen 0.9
set lmargin screen 0.1
set bmargin screen 0.4
# ----------------------------------------
set style data histogram
plot \
newhistogram "Direct titration", \
'test.dat' using 2:xtic(1) ls 4 axes x1y1 title "Phenol Red", \
'' u 3 ls 5 axes x1y1 title "Neutral Red", \
newhistogram "Back-titration", \
'test.dat.dat' using 4:xtic(1) ls 1 axes x1y1 title "Bromophenol Blue", \
'' u 5 ls 2 axes x1y1 title "Bromocresol Green", \
'' u 6 ls 3 axes x1y1 title "Mixed Indicator"
# ----------------------------------------
##
Script works fine. I would like to provide more spacing between the tick labels (1,2,3) and the sub-title (Direct titration and Back-titration) as well as spacing between the sub-titles and axis title. How can it be done?
set xlabel offset 0, -1 will shift both the subtitles and the axis title downward by one character height.
set xlabel "\nTEXT" will create a two-line axis label and place TEXT on the second line.
Modifying your xlabel command to be
set xlabel "\nSample No. " font ",16" offset 0,-1
will make both those changes.

Curves superimposed to a surface in gnuplot

I am trying to create a plot with gnuplot where some curves are superimposed to a surface.
This is the code I use
#!/usr/bin/gnuplot
xwidth=16.2
set size 1,1
set terminal epslatex color size xwidth cm,0.59*xwidth font 9 header '\fontsize{10}{13.2} \usepackage[lite,subscriptcorrection,slantedGreek,nofontinfo]{mtpro2}\usepackage{amsmath} \renewcommand{\rmdefault}{ptm}' dashlength 2.0 round standalone
set output "Pernal12.tex"
set border 0 lw 0
set lmargin 0
set rmargin 0
set tmargin 0
set bmargin 0
set multiplot
####
set size 0.55,1.166
set origin 0.02,-0.035
set xrange [0:10]
set yrange [-6:6]
set zrange [0:10]
unset ylabel
unset xlabel
set format y ''
set format x ''
unset xtics
unset ytics
unset label
set view map
set isosamples 400, 400
set style data pm3d
set style function pm3d
set pm3d implicit at b
#set grid noxtics noytics noztics front
#set palette positive nops_allcF maxcolors 0 gamma 1.5 gray
set palette defined (0 "white", 10 "black")
set pm3d interpolate 1,1
spacing 1.1
set cbrange[0:10]
unset colorbox
t={1,0}
e0={1,0}
eta={0.1,0}
i={0.0,1.0}
c(x)=sqrt(16*t**2+x**2)
a(x)=sqrt(2*((4*t/(c(x)-x))**2+1))
A(x)=(1-4*t/(c(x)-x))**2
B(x)=(1+4*t/(c(x)-x))**2
Gbup(x,y)=1/a(x)**2*(A(x)/(y-(e0+t+(c(x)+x)/2)+x/2+1+i*eta)+B(x)/(y-(e0+t-(c(x)-x)/2)+x /2+1+i*eta))
Gaup(x,y)=1/a(x)**2*(B(x)/(y-(e0-t+(c(x)+x)/2)+x/2+1+i*eta)+A(x)/(y-(e0-t-(c(x)-x)/2)+x/2+1+i*eta))
splot -imag(Gbup(x,y)+Gaup(x,y))/1.01 notitle
####
unset border
set border 15 lw 1
set size 0.385,0.83
set origin 0.087,0.11
unset xtics
unset ytics
set xrange [0:10]
set yrange [-6:6]
set zrange [0:20]
set xlabel '$U/t$'
set ylabel '$\omega/t$' offset 1.0,0
set xtics 5.0
set mxtics 5
set label '$n = 1/2$' left at graph 0,1 offset 2.2,-1.6 front
set xtics ("0" 0,"" 1 1,"" 2 1,"" 3 1,"" 4 1, '5' 5,"" 6 1,"" 7 1,"" 8 1,"" 9 1, '10' 10)
set ytics ( '-6' 6,"" 5 1,"-4" 4,"" 3 1,"-2" 2,"" 1 1, "0" 0,"" -1 1,"2" -2,"" -3 1,"4" -4, "" -5 1, "6" -6)
set grid noxtics noytics noztics front
plot 'Pernal12sym.dat' u ($1):(-$8) w l lw 2.5 lc rgb "green" lt 2 notitle
q
I obtain a nice figure but the problem is its dimension, more than 10 Mb.
I could reduce the isosamples value but then the surface map resolution would be too low.
Is there a way to obtain a quite high resolution with a final .ps file whith a dimension under let's say 1Mb?
I also tried to save before the surface map in png and than using this script
#!/usr/bin/gnuplot
xwidth=16.2
set size 1,1
set terminal epslatex color size xwidth cm,0.59*xwidth font 9 header '\fontsize{10}{13.2} \usepackage[lite,subscriptcorrection,slantedGreek,nofontinfo]{mtpro2}\usepackage{amsmath} \renewcommand{\rmdefault}{ptm}\usepackage{graphicx}' dashlength 2.0 round standalone
set output "Pernal12.tex"
set border 0 lw 0
set lmargin 0
set rmargin 0
set tmargin 0
set bmargin 0
set multiplot
####
set size 0.55,1.166
set origin 0.02,-0.035
set xrange [0:10]
set yrange [-6:6]
set zrange [0:10]
unset ylabel
unset xlabel
set format y ''
set format x ''
unset xtics
unset ytics
unset label
plot "Pernal13.png" binary filetype=png w rgbimage
unset border
set border 15 lw 1
set size 0.385,0.83
set origin 0.087,0.11
unset xtics
unset ytics
set xrange [0:10]
set yrange [-6:6]
set zrange [0:20]
set xlabel '$U/t$'
set ylabel '$\omega/t$' offset 1.0,0
set xtics 5.0
set mxtics 5
set grid noxtics noytics noztics front
plot 'Pernal12sym.dat' u ($1):(-$8) w l lw 2.5 lc rgb "green" lt 2 notitle
q
But at the end I don't have my .png image as the background of the .ps file.
For that kind of plots (equidistant sampling in x- and y-directions) it is worth using the image plotting style. Since I don't have the data file to test the whole script, here is the first part using plot '++' ... with image instead of splot ... with pm3d:
xwidth=16.2
set terminal epslatex color size xwidth cm,0.59*xwidth font 9 dashlength 2.0 round standalone
set output "Pernal12.tex"
unset border
set lmargin 0
set rmargin 0
set tmargin 0
set bmargin 0
set multiplot
####
set size 0.55,1.166
set origin 0.02,-0.035
set xrange [0:10]
set yrange [-6:6]
set zrange [0:10]
unset tics
set view map
set samples 1000
set isosamples 1000, 1000
set palette defined (0 "white", 10 "black")
set cbrange[0:10]
unset colorbox
t={1,0}
e0={1,0}
eta={0.1,0}
i={0.0,1.0}
c(x)=sqrt(16*t**2+x**2)
a(x)=sqrt(2*((4*t/(c(x)-x))**2+1))
A(x)=(1-4*t/(c(x)-x))**2
B(x)=(1+4*t/(c(x)-x))**2
Gbup(x,y)=1/a(x)**2*(A(x)/(y-(e0+t+(c(x)+x)/2)+x/2+1+i*eta)+B(x)/(y-(e0+t-(c(x)-x)/2)+x /2+1+i*eta))
Gaup(x,y)=1/a(x)**2*(B(x)/(y-(e0-t+(c(x)+x)/2)+x/2+1+i*eta)+A(x)/(y-(e0-t-(c(x)-x)/2)+x/2+1+i*eta))
plot '++' using 1:2:(-imag(Gbup($1,$2)+Gaup($1,$2))/1.01) with image notitle
The resulting .ps file is 938kB big. If you have the change to use the 5.0rc2 version, you can get down to 106kB with the level3 terminal option.
BTW: It is quite pointless to use set size and set origin and also set all margins with set lmargin, bmargin, tmargin and rmargin. See also Big data surface plots: Call gnuplot from tikz to generate bitmap and include automatically?.

Resources