GNUPLOT: Merge key entries with multiplot - colors

I would like to plot several data colmuns of a datfile in one graph. For each data columnI would like to use a black (differently dashed) line and a coloured point. I found out how to do it in general (by plotting first the line (with lines) and then the points (with points) and afterwards shifting the legend entries on top of each other). This is explained for example in this post:
Merge key entries in gnuplot
But it is not fully working in my case. I have three problems:
First: I would like to have a box around the legend. But this doesnot work when I shift the legend entries on top of each other...
Second: I would like to include a rectangle object. Somehow this always is on top of the plotted lines except the last one...
And the third problem: The xticlabels are plotted for each plot on each other. That is why they seem to be bold which they should not. I found out that I should "hide" the tics (like I do with the border and the labels) but it doesnot work for the tics somehow...
Do you have some hints for me?
Best regards,
Sebastian
#ewcz
#dataset.dat
"\\footnotesize r/R" "\\footnotesize OP1" "\\footnotesize OP2"
0.132 1.018 0.872
0.162 0.940 0.796
0.191 1.014 0.848
0.221 1.043 0.934
0.250 1.010 0.935
0.279 0.987 0.938
0.309 0.962 0.930
0.338 0.929 0.921
0.368 0.897 0.922
0.397 0.876 0.932
0.426 0.831 0.919
0.456 0.795 0.884
#Start terminal
set terminal epslatex size 7.8cm, 6.1cm font ",10"
#Legend settings
pointSize = 1
yticsScale =1
keySpacing = pointSize*yticsScale*1.25
keyY = 15.5
keyX = 0.975
set key vertical Left reverse width -0.5 height +0 font ",16"
set key opaque
set key autotitle columnheader
set key bottom right spacing -1
#Hide border & labels
set border 0
set xlabel " "
set ylabel " "
#Format of axis numbers
set format xy '$\%g$'
set format x '\footnotesize \%10.1f'
set format y '\footnotesize \%10.1f'
#Format tics
set xtics 0,0.1 out nomirror
set xtics offset -0.2,0
set mxtics 5
set ytics 0.6,0.1 out nomirror
set ytics offset 0.4,0
set mytics 5
#Background grid setting
set grid
show grid
set object 1 rectangle from 0.132, graph 0 to 0.456, graph 1 fillcolor rgb "#A9A9A9" fs pattern 1 noborder behind
#Margins
set lmargin 5.9
set rmargin 0.5
set bmargin 3.5
#Axis range settings
set xrange [0:0.535]
set yrange [0.6:1.2]
#Format lines, boxes...
set style line 4 lt 1 lc rgb 'black' lw 2 pt 13 ps 1.25 dt 4
set style line 5 lt 1 lc rgb 'black' lw 2 pt 4 ps 1.0 dt 5
#Multiplot
set multiplot
set origin 0,0
set size 1,1
#Plots
set key at graph keyX, character keyY
plot 'dataset.dat' using 1:2 with lines ls 4, \
'dataset.dat' using 1:2 with points ls 4 lc rgb "#71da71" title " "
#Label settings
set border
set xlabel '\small $r/D_T\;[-]$' offset 0,+0
set ylabel '\small $c_{m2} \cdot A_{T}/Q_T\;[-]$' offset +10.5,+0
#Last Plot
keyY = keyY - keySpacing
set key at graph keyX, character keyY
plot 'dataset.dat' using 1:3 with lines ls 5, \
'dataset.dat' using 1:3 with points ls 5 lc rgb "#4da6ff" title " "
#End of code
unset multiplot

I would propose the following:
Since the keys in both plots are independent, perhaps the most straightforward solution would be to draw the encompassing box manually (see below) by using set object rectangle (although this might need some manual "tweaking" of the size of the box).
The rectangle is on top of the plotted lines since it is duplicated by the second plot. In a sense, this second copy is behind with respect to the second plot, but since this layer is on top of the first plot, it covers the elements plotted by the first plot. One can get rid of this by deleting the object in the context of the second plot with unset object 1.
It is a similar issue with the tics,labels,etc. In the code below, all the definitions are moved before the first plot command and then unset with respect to the second plot.
With these modifications the script would look like:
#Start terminal
set terminal epslatex size 7.8cm, 6.1cm font ",10"
#Legend settings
pointSize = 1
yticsScale =1
keySpacing = pointSize*yticsScale*1.25
keyY = 15.5
keyX = 0.975
set key vertical Left reverse width -0.5 height +0 font ",16"
set key opaque
set key autotitle columnheader
set key bottom right spacing -1
#Format of axis numbers
set format xy '$\%g$'
set format x '\footnotesize \%10.1f'
set format y '\footnotesize \%10.1f'
#Format tics
set xtics 0,0.1 out nomirror
set xtics offset -0.2,0
set mxtics 5
set ytics 0.6,0.1 out nomirror
set ytics offset 0.4,0
set mytics 5
#Background grid setting
set grid
show grid
set object 1 rectangle from 0.132, graph 0 to 0.456, graph 1 fillcolor rgb "#A9A9A9" fs pattern 1 noborder behind
#Margins
set lmargin 5.9
set rmargin 0.5
set bmargin 3.5
#Axis range settings
set xrange [0:0.535]
set yrange [0.6:1.2]
#Format lines, boxes...
set style line 4 lt 1 lc rgb 'black' lw 2 pt 13 ps 1.25 dt 4
set style line 5 lt 1 lc rgb 'black' lw 2 pt 4 ps 1.0 dt 5
#Multiplot
set multiplot
set origin 0,0
set size 1,1
#Plots
set key at graph keyX, character keyY
#simulate key box
set object 2 rectangle from graph keyX, character keyY + 0.5*keySpacing to graph 0.65, character keyY - 1.5*keySpacing fillcolor rgb "#FFFFFF" fs pattern 2 border rgb "black"
set xlabel '\small $r/D_T\;[-]$' offset 0,+0
set ylabel '\small $c_{m2} \cdot A_{T}/Q_T\;[-]$' offset +10.5,+0
plot \
'dataset.dat' using 1:2 with lines ls 4, \
'dataset.dat' using 1:2 with points ls 4 lc rgb "#71da71" title " "
#unset these so that they are not duplicated by the following plot command
unset border
unset xtics
unset ytics
unset xlabel
unset ylabel
unset object 1
unset object 2
#Last Plot
keyY = keyY - keySpacing
set key at graph keyX, character keyY
plot \
'dataset.dat' using 1:3 with lines ls 5, \
'dataset.dat' using 1:3 with points ls 5 lc rgb "#4da6ff" title " "
This then produces (I used standalone epslatex terminal. It might be some font issue, but it seems that the ylabel would benefit from slightly larger horizontal offset):

Related

Gnuplot: set angular grid limits in polar plot

I am trying to make a wedge-shaped plot in polar coordinates spanning from 0 to 60 degrees. Something like the following figure: Wedge-plot I want
However, the command "trange" is used for the range of the plot, not of the grid itself, and I always end up with the full-circle grid, like this: Same plot but with full grid.
Is there a simple command to set the limits in the angle variable? Here is the code I used to plot the former figure in gnuplot 5.2
set terminal pngcairo enhanced font "arial,10" fontscale 1.0 size 600, 400
set output 'polar1.png'
unset key
set border 4096 lt black linewidth 1.000 dashtype solid
unset xtics
unset ytics
set size ratio 1 1,1
set raxis
set ttics 0.00000,30 font ":Italic"
set polar
set grid polar 30.0000 lw 1.5
plot cos(4*t) lt 3 lw 2
Thank you in advance!
I guess there is no "intended" way to limit the maximum angle in a polar plot.
So, there is a simpler (but ugly) workaround, which simply covers the unwanted part by a filled polygon.
Note: There will be an issue if your rmax is not an integer multiple of rtic 0.2, i.e. a plot with rmax=1.05 will not look as desired. Therefore, as a workaround an extra rtic at rmax is added.
Script:
### plot only part of polar plot
reset session
rmax = 1.05
amax = 60
set polar
set rrange [0:rmax]
set rtics 0.2 scale 0.5
set rtics add ('' rmax)
set grid r polar 10 lt black lw .3
set trange [0:2*pi]
set ttics 0,10 format "%g°" font ":Italic" scale 0.5,0.25 offset -1,0
set mttics 2
set xlabel "r-label"
set xrange [0:rmax]
unset xtics
set yrange [0:rmax]
unset ytics
set size square
set border 4096
set lmargin 0
set tmargin 0
unset title
unset key
set samples 300
set obj 1 polygon from graph 0,0 to first rmax*cos(pi/180*amax),rmax*sin(pi/180*amax) \
to first rmax*cos(pi/180*amax), screen 1.0 \
to first 0, screen 1 to screen 0,1 to screen 0,0 to graph 0,0 \
fc rgb 0xffffff fs solid 1.0 front
set arrow 1 from graph 0,0 to first rmax*cos(pi/180*amax),rmax*sin(pi/180*amax) lc "black" nohead front
plot cos(4*t) lt 3 lw 2
### end of script
Result:

How to set border setting in different colors using Gnuplot?

I am facing a small issue regarding border setting in multiplot. Though I am following a few examples available but still missing top and bottom lines.
here is the code
**
set multiplot
set border lw 2
set origin 0.05,0.49
set size 0.38,0.45
set yrange [-1.5:1.0]
set xrange [0:2.17106]
set ylabel "E-E_F (eV)" offset 0.5 font "Times-Bold, 35"
set ytics 0.5 font "Times-Bold, 35"
unset xlabel
#set title "Cubic" font "Times-Bold,35"
set label "a)" offset -1,12 font "Times-Bold,40"
plot "bands_cs.dat" using 1:($2--.3095296750) w l lc "black" lw 2 notitle, "bands_cs.dat" every :::32::32 u 1:($2--.3095296750) w l lc rgb "blue" lw 3 notitle,"bands_cs.dat" every :::33::33 u 1:($2--.3095296750) w l lc rgb "blue" lw 3 notitle,"bands_cs.dat" every :::34::34 u 1:($2--.3095296750) w l lc rgb "red" lw 3 notitle,"bands_cs.dat" every :::35::35 u 1:($2--.3095296750) w l lc rgb "red" lw 3 notitle
set origin 0.38,0.49
set size 0.14,0.483
unset arrow
unset xtics
unset label
unset yrange
unset xlabel
unset ylabel
#set xrange[0:2000]
set xtics 1000
set yrange[-1.5:1.0]
set border 1+2+4 lt rgb "black"
set title "{/Symbol s}^{AHE}(10^3 Scm^{-1}) " font "Times-Bold,25"
set key opaque box right samplen 0.8 height 1.2
xmn=-50
xmx=2500
set ytics format "" nomirror
set xtics (" " 1000,\
" " 2000 ) font "Times-Bold, 35"
set arrow from xmn, 0.0 to xmx, 0.0 nohead dt "-"
ymn=0.98
ymx=0
set arrow from 2100,-0.2 to 2100,0 nohead dt "-"
unset xlabel
#set xlabel 0,1,2
set xrange [xmn:xmx]
set y2range [0.95:1.02]
set border 8 lt rgb "dark-green" lw 2
set y2tics 0.1 nomirror textcol rgb "dark-green" font "Times-Bold, 30"
plot "cs_yx.dat" u 2:1 w l notitle ' lc rgb'black' lw 4,"strain_yx.dat" u 3:1 w lp axes x1y2 lc rgb "dark-green" lw 4 pt 7 ps 2 notitle
**
In short, i need to change one color side of the plot.
Assuming you just want to have the right y-axis of the 2nd plot in a different color (without y2tics), but the others borders in black. You can simply remove the right border via set border 5 and add a green line via set arrow.
Check help border and help margins and help arrows.
Script:
### set border in different colors
reset session
set multiplot
set margins 0,0,-1,-1 # l, r, b, t
set origin 0.10,0.10
set size 0.60,0.90
set border lw 2
set grid x,y
plot sin(x)
set origin 0.70,0.10
set size 0.20,0.90
unset ytics
set border 5 # only top and bottom
set arrow 1 from graph 1,0 to graph 1,1 lw 2 lc "green" nohead front # "manual" border
set xrange [0:10]
set xtics add ('' 0) # remove 0 label to avoid overlap with 10 of the 1st plot
plot cos(x) lc "green"
unset multiplot
### end of script
Result:
Addition: if you want to have ytics (actually, y2tics) on the right colored axis, you probably have to add a third dummy plot within the multiplot environment. Check the following example:
Script:
### set border in different colors including tics
reset session
set multiplot
set margins 0,0,-1,-1 # l, r, b, t
set origin 0.10,0.10
set size 0.60,0.90
set border lw 2
set grid x,y
plot sin(x)
set origin 0.70,0.10
set size 0.20,0.90
unset ytics
set border 5 # only top and bottom
set xrange [0:10]
set xtics add ('' 0) # remove 0 label to avoid overlap with 10 of the 1st plot
plot cos(x) lc "green"
set border 8 lw 2 lc "green"
set format x ''
set xtics scale 0
set yrange [GPVAL_Y_MIN:GPVAL_Y_MAX] # yrange from previous plot
set y2tics 0.2 nomirror
set link y2 via y inverse y
set format y2 ' '
plot NaN notitle # dummy plot, plots nothing
unset multiplot
### end of script
Result:

Show error bars in a multiaxis plot in Gnuplot

I have a dataset (show-errorbar.dat) containing:
Model# DE IE Error
Apple -4.6 -128.9538 4.0
Huawei -5.2 -176.6343 5.3
One-Pro -5.2 -118.1106 3.2
#!/usr/bin/gnuplot
#set terminal pdfcairo enhanced color font 'Helvetica,12' linewidth 0.8
set terminal png
set output 'BrandError.png'
set boxwidth 1.0 relative
set bmargin 5
set style fill solid border -1
set xtic rotate by -45 scale 0
#set auto x
set style line 81 lt 0 lc rgb "#808080" lw 0.5
set grid xtics
set grid ytics
set grid mxtics
set grid mytics
set grid back ls 81
set arrow from graph 0,first -4.6 to graph 1, first -4.6 nohead lw 2 lc rgb "#000000" front
set border 11
set border lw 2.0
set xtics font ",11"
set ytics font ",14"
set tics out
set ytics nomirror
set y2tics
set y2tics font ",14"
set mxtics 10
set mytics 2
set my2tics 2
set yrange [-10:0]
set y2range [-260:0]
set key left bottom
set y2label offset -2
set ylabel offset 2
set ylabel 'DE' tc rgb "red"
set y2label 'IE' tc rgb "green"
set style data histograms
set style histogram cluster gap 2
set linetype 2 lc rgb 'red'
set linetype 3 lc rgb 'yellow'
set linetype 4 lc rgb 'green'
plot 'show-errorbars.dat' using 2 ti 'DE' lc 2 axis x1y1, '' u 3:xticlabels(1) ti 'IE' lc 4 axis x1y2
set output
enter image description here
I would like to plot a histogram comparing DE vs IE and also show error bars (data in column 4) for the IE values.
Please any help on how to go about it.
There is a variant histogram style for exactly that purpose
set style histogram errorbars gap 2 {lw W}.
Here is the help section from the docs:
The `errorbars` style is very similar to the `clustered` style, except that it
requires additional columns of input for each entry. The first column holds
the height (y value) of that box, exactly as for the `clustered` style.
2 columns: y yerr bar extends from y-yerr to y+err
3 columns: y ymin ymax bar extends from ymin to ymax
The appearance of the error bars is controlled by the current value of
`set errorbars` and by the optional <linewidth> specification.
Updated answer
Notes:
You can't mix axis choice within a single histogram. So I have removed the axes x1y1 and axes x1y2 from the plot command. Since you have explicitly given the range for both y1 and y2, the plot border and labels are not affected.
However since the green bars are now being plotted against y1, we have to scale them so that the y2 axis labels apply. So the column 3 and column 4 values will be divided by 26, which is (y2 range) / (y1 range)
In "histogram errorbars" mode each plot component looks for an extra column of data to determine the size of the errorbar. Since your column 2 data has no corresponding column of errors, we dummy it up to use all a constant not-a-number (no data) value: (NaN)
Your data contains a line of columnheaders, which could confuse the program if it thinks this is a line of data. There are a number of ways you can tell the program to skip this line; I have used set key autotitle columnhead for convenience and because it is supported by old versions of gnuplot. If you have a current version it would be better to use instead set datafile columnheaders.
I have kept all of your commands except that the plot command is replaced by the following 3 lines:
set style histogram errorbars gap 2 lw 1.5
set key autotitle columnhead
plot 'show-errorbars.dat' using 2:(NaN) ti 'DE' lc 2, '' u ($3/26.):($4/26.):xticlabels(1) ti 'IE' lc 4

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:

Gnuplot Thick Curve Exceeds Plot Boundary

I've plotted the following in Gnuplot. My issue is that the curves exceed the boundary (so we can see that the purple and blue curves go beyond the y-axis). Any way to solve this problem? I'm hoping there is something that restricts drawing to inside the plotting area. Sure I can just plot less of the curve but that then looks weird. Ideally, I want Gnuplot to go around the frame of the curve and remove any bits of the curve that are there.
I've made the purple curve abnormally fat just to illustrate the problem. The problem is also there with the blue curve though.
The code to produce the above plot is:
#!/usr/bin/env gnuplot
### n: change this parameter to equal the number of data sets to be plotted
n = 2
# t: top margin in pixels
t = 25.0
# b: key height in pixels (bottom margin)
b = 25.0
# h: height of output in pixels
h = 150.0*n + t + b
### define functions to help set top/bottom margins
top(i,n,h,t,b) = 1.0 - (t+(h-t-b)*(i-1)/n)/h
bot(i,n,h,t,b) = 1.0 - (t+(h-t-b)*i/n)/h
### first set up some basic plot parameters
#set term cairolatex size 15cm,15cm
#set output 'DifferentialAmplifierPlot.tex'
set term pdfcairo size 15cm,15cm
set output 'DifferentialAmplifierPlot.pdf'
set border lw 4
set grid mxtics mytics xtics ytics ls '-1' ls '-1' lc rgb 'gray70', lc rgb 'gray90'
set mxtics
set mytics
# Make yrange > ytics > function to get padding.
set yrange [-1.5:1.5]
set ytics ("" -1.5, -1.25 1, -1.0, -0.75 1, -0.5, -0.25 1, 0.0, 0.25 1, 0.5, 0.75 1, 1.0, 1.25 1, "" -1.5)
set xtics 0,1,5
set xrange [0:5]
set xtics
set mxtics
set mytics
set format x ""
set grid xtics ytics mxtics mytics ls -1 ls -1 lc rgb 'gray60', lc rgb '#C0E5E5E5''
set multiplot layout (n+1),1 #font ",14" title 'Input And Output Voltages Of Differential Amplifier'
### First plot
# change only plot command here
currentplot = 1
set tmargin at screen top(currentplot,n,h,t,b)
set bmargin at screen bot(currentplot,n,h,t,b)
unset key
unset xlabel
set title 'Input (Bottom) And Output (Top) Voltages Of The Differential Amplifier'
set ylabel 'Voltage [V]'
plot 'DifferentialAmplfier.dat' using (1000*$1):2 with lines lw 20 lc rgb 'dark-magenta'
### Last plot
# change only plot command here
currentplot = currentplot + 1
set tmargin at screen top(currentplot,n,h,t,b)
set bmargin at screen bot(currentplot,n,h,t,b)
set format x
unset title
set xlabel 'Time [ms]'
set ylabel 'Voltage [mV]'
plot 'DifferentialAmplfier.dat' using (1000*$1):(1000*$3) with lines lw 10 lc rgb 'navy'
unset multiplot
set term x11
Questionable/Dodgy fix...
There is a way to do this but it is cumbersome. You would use a multiplot, plot your data, then through judicious use of the graph and screen coordinate systems plot white rectangles around your plot, then replot an empty plot to redraw the borders (because the rectangles will have overwritten your labels and half of the border line thickness). Here is an MWE:
#!/usr/bin/gnuplot -persist
reset
f(x) = sin(x)
xl=0; xh=20; yl=-1; yh=1;
set xrange [xl:xh]
set yrange [yl:yh]
set multiplot
plot f(x) not w l lt 3 lw 12
## overdraw borders on left, right, top, bottom
set object 1 rectangle from screen 0, screen 0 to graph 0, screen 1 behind \
fillstyle solid noborder
set object 2 rectangle from graph 1, screen 0 to screen 1, screen 1 behind \
fillstyle solid noborder
set object 3 rectangle from screen 0, graph 1 to screen 1, screen 1 behind \
fillstyle solid noborder
set object 4 rectangle from screen 0, screen 0 to screen 1, graph 0 behind \
fillstyle solid noborder
plot NaN not
unset multiplot

Resources