Gnuplot: Multiplot size and scaling of one plot - gnuplot

I have this multiplot:
Generated by the following code:
set terminal pdf size 15,10
set output "graph.pdf"
set key off
set multiplot layout 5, 1 scale 1, 1
set style data histograms
set style histogram
set style fill solid border -1
set boxwidth 0.75
set yrange [0:178]
set ytics 50
set xtics offset 0,graph 0.01
set xtics font ",20"
set x2tics font ",20"
set ytics font ",20"
set tmargin 3
set bmargin 3
set style line 1 linetype -1 linewidth 3
set grid mxtics
set xtics (0.5 1,1.5 1,2.5 1,3.5 1,4.5 1,5.5 1,6.5 1,7.5 1,8.5 1,9.5 1,10.5 1,11.5 1,12.5 1,13.5 1,14.5 1,15.5 1,16.5 1,17.5 1,18.5 1,19.5 1,20.5 1,21.5 1,22.5 1,23.5 1,24.5 1,25.5 1,26.5 1,27.5 1,28.5 1,29.5 1,30.5 1 )
set tics out
plot 'outil1' every ::0::30 u 7:xtic(2) notitle lt rgb "blue" fillstyle pattern 2, 'ppm' every ::0::30 u 7 notitle lt rgb "red" fillstyle pattern 2, \
newhistogram at 0, 'outil1' every ::0::30 u 6:x2tic(1) notitle lt rgb "blue", 'ppm' every ::0::30 u 6 notitle lt rgb "red"
plot 'outil1' every ::30::60 u 7:xtic(2) notitle lt rgb "blue" fillstyle pattern 2, 'ppm' every ::30::60 u 7 notitle lt rgb "red" fillstyle pattern 2, \
newhistogram at 0, 'outil1' every ::30::60 u 6:x2tic(1) notitle lt rgb "blue", 'ppm' every ::30::60 u 6 notitle lt rgb "red"
plot 'outil1' every ::60::90 u 7:xtic(2) notitle lt rgb "blue" fillstyle pattern 2, 'ppm' every ::60::90 u 7 notitle lt rgb "red" fillstyle pattern 2, \
newhistogram at 0, 'outil1' every ::60::90 u 6:x2tic(1) notitle lt rgb "blue", 'ppm' every ::60::90 u 6 notitle lt rgb "red"
plot 'outil1' every ::90::120 u 7:xtic(2) notitle lt rgb "blue" fillstyle pattern 2, 'ppm' every ::90::120 u 7 notitle lt rgb "red" fillstyle pattern 2, \
newhistogram at 0, 'outil1' every ::90::120 u 6:x2tic(1) notitle lt rgb "blue", 'ppm' every ::90::120 u 6 notitle lt rgb "red"
plot 'outil1' every ::120::150 u 7:xtic(2) notitle lt rgb "blue" fillstyle pattern 2, 'ppm' every ::120::150 u 7 notitle lt rgb "red" fillstyle pattern 2, \
newhistogram at 0, 'outil1' every ::120::150 u 6:x2tic(1) notitle lt rgb "blue", 'ppm' every ::120::150 u 6 notitle lt rgb "red"
To generate the plot you need outil1 data file and ppm data file
The problem that I have with this is the plot is that the last one is not equal in size with the first 4 plots. The distance between the tics is bigger in the last plot in order to make it fit the entire canvas.
Is there a way to make the last plot aligned with the other? For example have the 126 tic align with the 96 tic from the 4th plot.
Thank you.

After you have plotted the four graph, you can fix the left margin to the value it has at this momen, and adapt the right margin according to the different scales from graph four to graph five.
You can calculate the left margin in screen coordinates (i.e. in the range [0:1], with 0 beeing the leftmost canvas edge and 1 the rightmost canvas edge) as follows:
LMARGIN = GPVAL_TERM_SCALE * (0.0 + GPVAL_TERM_XMIN)/GPVAL_TERM_XSIZE
Here, GPVAL_TERM_SCALE is a factor, which is available only in gnuplot 5, but is required to get the correct calculations independent on the used terminal. When using gnuplot 4.6. and the pdfcairo terminal, this value is 20. You can put a small if-clause in the script to check this:
if (exists('GPVAL_TERM_SCALE')) SCALE = GPVAL_TERM_SCALE; else SCALE = 20
So, the last plot command in your script should look as follows:
if (exists('GPVAL_TERM_SCALE')) SCALE = GPVAL_TERM_SCALE; else SCALE = 20
LMARGIN = SCALE * (0.0 + GPVAL_TERM_XMIN)/GPVAL_TERM_XSIZE
RMARGIN = SCALE * (0.0 + GPVAL_TERM_XMAX)/GPVAL_TERM_XSIZE
set lmargin screen LMARGIN
set rmargin screen LMARGIN + (17.0/32.0)*(RMARGIN - LMARGIN)
plot 'outil1' every ::120::150 u 7:xtic(2) notitle lt rgb "blue" fillstyle pattern 2, 'ppm' every ::120::150 u 7 notitle lt rgb "red" fillstyle pattern 2, \
newhistogram at 0, 'outil1' every ::120::150 u 6:x2tic(1) notitle lt rgb "blue", 'ppm' every ::120::150 u 6 notitle lt rgb "red"
I got the factor (17.0/32.0) just by manually counting the ranges you want to plot. The result is

Related

Gnuplot histogram with plot line

I am using Gnuplot to create a histogram with a plot line, however, the plot line is not fit well with bar head, also I would like to put the line a little bit far from the bar head.
set border 3
set boxwidth 0.9
set tics nomirror out scale 0.75
set style fill solid 0.8
plot "03.txt" using 2:xtic(1) lt rgb "#0060ad" notitle, "" using 2 smooth csplines notitle with lines ls 1, "" using 3 lt rgb "#ff6600" notitle, "" using 3 smooth csplines notitle with lines ls 2, "" using 4 lt rgb "#dd181f" notitle, "" using 4 smooth csplines notitle with lines ls 3
Updated:
This is the data file:
500000 25.938 25.938 2
1000000 52.385 52.385 4
1500000 79.749 78.405 6.125
2000000 152.589 100.261 12.479
2500000 224.869 118.364 19.159
This should work for any number of columns, you have to specify them in the variable N, and number them in calls to custom function xbox. This should do for a non-intensive usage. You can offset vertically the curves with the OFFSET variable (in units of y axis)
set border 3
#number of columns to be plotted
N=3
#vertical offset
OFFSET=0
#gapwidth (set to gnuplot's default)
GW=2
xbox(x,i)=x+(i-N*0.5)/(N+GW)
set boxwidth 0.9
set tics nomirror out scale 0.75
set style fill solid 0.8
plot "03.txt" using 2:xtic(1) lt rgb "0060ad" notitle, \
"" using 2 with histogram notitle, \
"" using (xbox($0,1)):($2+OFFSET) smooth csplines notitle with lines ls 1, \
"" using 3 lt rgb "#ff6600" notitle with histogram, \
"" using (xbox($0,2)):($3+OFFSET) smooth csplines notitle with lines ls 2, \
"" using 4 lt rgb "#dd181f" notitle with histogram, \
"" using (xbox($0,3)):($4+OFFSET) smooth csplines notitle with lines ls 3

Set point size independent of line weight

I have the following line styles defined in gnuplot:
set linetype 1 lc rgb "red" lw 3 pt 7
set linetype 3 lc rgb "red" lw 1 pt 7
It appears as though the points derive part of their size from the lineweight. I'm using these styles inside of a plot for loop with linetype cycle using the same style for a cspline and the corresponding points, so I don't see any easy way to just define a separate style for the points.
I get results like this:
The points respond to pointsize but the point in linetype 1 is still slightly larger (presumably from the thicker border).
Is it possible to get the points in these two styles to be the same size?
In response to Miguel's comment, a more complete example of my use case is:
filenames = "A B C D"
set linetype 1 lc rgb "blue" lw 3 pt 7
set linetype 2 lc rgb "red" lw 3 pt 7
set linetype 3 lc rgb "blue" lw 1 pt 7
set linetype 4 lc rgb "red" lw 1 pt 7
set linetype cycle 4
plot for [file in filenames] file.".csv" \
using 1:2
title file \
smooth csplines, \
for [file in filenames] file.".csv" \
u 1:2 with points notitle
linetypes 5-8 get set by the cycling and are used by the last part of the plotting command. Recommendations on another way to do this would be welcome!
For your very specific case you can set more styles, and do it rather automatically with a do for loop (reusing your code):
filenames = "A B C D"
do for [i=0:1] {
set linetype (4*i+1) lc rgb "blue" lw (i == 1 ? 0 : 3) pt 7
set linetype (4*i+2) lc rgb "red" lw (i == 1 ? 0 : 3) pt 7
set linetype (4*i+3) lc rgb "blue" lw (i == 1 ? 0 : 1) pt 7
set linetype (4*i+4) lc rgb "red" lw (i == 1 ? 0 : 1) pt 7
}
set linetype cycle 8
plot for [file in filenames] file.".csv" \
using 1:2 \
title file \
smooth csplines, \
for [file in filenames] file.".csv" \
u 1:2 with points notitle
With some simple data files:
For some terminals the size of filled point types depends on the linewidth because they have a border. This is the case for all cairo-based terminals (pdfcairo, pngcairo, wxt and cairolatex), whereas other terminal like svg, postscript, qt don't show this behaviour.
As test case consider
set linetype 1 lc rgb "red" lw 3 pt 7
set linetype 3 lc rgb "red" lw 1 pt 7
set samples 11
set style function linespoints
plot x lt 1, x + 0.5 lt 3
Considering that you want to have the linepoints samples in the legend, you're best choice is to reduce the point size a bit for the line type with the larger linewidth, like
set linetype 1 lc rgb "red" lw 3 pt 7 ps 0.9
The choice of the scaling factor must be determined manually.

Including solid and dashed contours in gnuplot

i have the following in a gnuplot script:
set pm3d
unset surface
set pm3d map
set style line 1 lt 1 lc rgb "white"
set style line 2 lt 1 lc rgb "white"
set style line 3 lt 1 lc rgb "white"
set style line 4 lt 1 lc rgb "red"
set style line 5 lt 1 lc rgb "blue"
set style line 6 lt 2 lc rgb "white"
set style line 7 lt 2 lc rgb "white"
set style line 8 lt 2 lc rgb "white"
set style line 9 lt 2 lc rgb "white"
set style increment userstyles
set contour base
set cntrparam levels 8
set cntrparam levels discrete -8*0.0004946, -6*0.0004946, -4*0.0004946, -2*0.0004946, -2*0.0004946, 4*0.0004946, 6*0.0004946, 8*0.0004946
set nokey
splot '/data/ltl21/Data/PDB/HDFNI/BlindSurvey/imageresults/spectrum/HDF1map:189.20135-62.20442' u 1:2:3
Such that, i would like to have dashed contours for negative values, and solid contours for positive values. I can change the colour of contours with the above fine, though i want them to all be white, but it doesn't seem to use lt at all, it just picks its own line style and uses that..
Does anyone know how to specify the linetype for individual contours?
Cheers
Same question just appeared to me. Here is a nice workaround I found at http://www.gnuplot.info/faq/faq.html :
gnuplot> # An example. Place your definition in the following line:
gnuplot> f(x,y) = y - x**2 / tan(y)
gnuplot> set contour base
gnuplot> set cntrparam levels discrete 0.0
gnuplot> unset surface
gnuplot> set table ’curve.dat’
gnuplot> splot f(x,y)
gnuplot> unset table
gnuplot> plot ’curve.dat’ w l
Use splot with your standard options. You can save more than one contour in one file. Each contour in the same file will be plotted in the same style.
You can set your plot options as usual: linestyle, linetype etc. That's how I used it:
gnuplot> set cntrparam levels discrete 0,1,2,3
gnuplot> set style line 1 lt 2 lw 4 lc rgb "cyan"
gnuplot> set table 'mycontours.dat'
gnuplot> splot 'mydata.dat' matrix
gnuplot> unset table
gnuplot> plot 'mycontours.dat' ls 1
It returned 4 dashed cyan contours in epslatex. It seems you need to save to different files for different contour styles (or edit the file by hand).
multiplot may be used to address this issue:
set pm3d
unset surface
set pm3d map
set view map
set key center rmargin
set dgrid3d 15,31
set hidden3d
set contour base
set nosurface
set grid lw 1 lc rgb 'gray'
f(x,y) = x*y
set nokey
set cntrparam levels discrete 2, 4, 6, 8
set linetype 1 lc rgb '#0042ad' lw 1.3
set linetype 2 lc rgb '#0060ad' lw 3.1
set linetype 3 lc rgb '#007cad' lw 31
set linetype 4 lc rgb '#0099ad' lw 0.31
set cntrlabel start 25 interval -1 font 'arial, 1'
set style textbox noborder
set multiplot
splot f(x,y) w l title ''
unset pm3d
set linetype 1 lc rgb '#00ada4' lw 2 dashtype 2
# notice how changing 'lw' or 'dashtype' does not have any effect after first use:
set linetype 2 lc rgb '#00ad88' lw 1 dashtype 1
set linetype 3 lc rgb '#00ad6b' lw 3 dashtype 4
set linetype 4 lc rgb '#eeeeee' lw 4 dashtype 3
set cntrparam levels discrete -8, -6, -4, -2
splot f(x,y) w l
unset multiplot
Unsetting pm3d is crucial or it will draw over the lines from the first splot command.

Drawing axis-aligned bounding box (AABB) in gnuplot

I have plotted two vectors (3,3,2), (2,2,1) and want to add an AABB to the first one
to show that the second vector is within the bounds of the first.
Adding set object rectangle from screen 0,0 to screen 3,3 front is not working.
So how can I draw the AABB (or a cuboid with a=3, b=3 and c=2)?
Also how can I put the the scale of the y-axis on the left side.
Still open:
Limit scale of y-axis to left side, and scale of x-axis to bottom front
Add arrows to x-, y- and z-axis
Delete the second x- and y- axis so that there is just one of each left
Gnuscript resource_vec_aabb
set xyplane 0
set grid
set xrange [0:5]
set yrange [0:5]
set zrange [0:5]
splot 'resource_vec_aabb.dat' with vectors filled head lw 2
Data resource_vec_aabb.dat
# Gnuplot .dat file for vectors
0 0 0 3 3 2
0 0 0 2 2 1
UPDATE
set termoption dashed #Needs to be set to draw dashed lines
set border 19 #To set axis (1 + 2 + 16). See: set border help
set xyplane 0
set grid
unset key
my_range = 4
set xrange [0:my_range]
set yrange [0:my_range]
set zrange [0:my_range]
set arrow from graph 0,0,0 to graph 1.05,0,0 size screen 0.025,15,60 \
filled ls 1 linecolor rgb "black"
set arrow from graph 0,0,0 to graph 0,1.05,0 size screen 0.025,15,60 \
filled ls 1 linecolor rgb "black"
set arrow from graph 0,0,0 to graph 0,0,1.05 size screen 0.025,15,60 \
filled ls 1 linecolor rgb "black"
set xtic 1
set ytic 1
set ztic 1
# AABB for 3,3,2
set arrow from 3,0,0 to 3,3,0 nohead linetype 2 linecolor rgb "green"
set arrow from 3,3,0 to 3,3,2 nohead linetype 2 linecolor rgb "green"
set arrow from 0,3,0 to 3,3,0 nohead linetype 2 linecolor rgb "green"
set arrow from 0,3,2 to 3,3,2 nohead linetype 2 linecolor rgb "green"
set arrow from 0,3,0 to 0,3,2 nohead linetype 2 linecolor rgb "green"
set arrow from 0,0,2 to 0,3,2 nohead linetype 2 linecolor rgb "green"
set arrow from 0,0,2 to 3,0,2 nohead linetype 2 linecolor rgb "green"
set arrow from 3,0,0 to 3,0,2 nohead linetype 2 linecolor rgb "green"
set arrow from 3,0,2 to 3,3,2 nohead linetype 2 linecolor rgb "green"
# 2,2,1
set arrow from 2,0,0 to 2,2,0 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,2,0 to 2,2,0 nohead linetype 2 linecolor rgb "blue"
set arrow from 2,2,0 to 2,2,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,0,1 to 2,0,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,0,1 to 0,2,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,2,1 to 2,2,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 2,0,1 to 2,2,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 2,0,0 to 2,0,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,2,0 to 0,2,1 nohead linetype 2 linecolor rgb "blue"
set view 40,44,1
splot 'resource_vec_aabb.dat' with vectors filled head lw 2
new output
Here is a suggestion to draw your bounding box with vectors and tic labels at the back y-axis. The bounding boxes are plotted instead of tediously drawn by arrows. There is still room for fine tuning.
Data: SO17363018.dat
# Gnuplot .dat file for vectors
0 0 0 3 3 2
0 0 0 2 2 1
Script: (works for gnuplot>= 4.6.0, March 2012)
### draw bounding box of vectors
reset
FILE = "SO17363018.dat"
set termoption dashed # required for gnuplot4.6 for dashed lines
set xlabel "x-axis" rotate parallel
set ylabel "y-axis" rotate parallel
set xyplane 0
set grid x,y
set xrange [0:5]
set yrange [0:5]
set border 19
set xtics 1 nomirror
set ytics axis
set for [i=1:3] arrow i from graph 0,0,0 to \
graph (1.12*(i==1)), (1.12*(i==2)), (1.12*(i==3)) \
size screen 0.025,15,60 filled ls 1 lc rgb "black"
x0(i,j,k) = ((i==0 ? 0 : i==1 ? k : j)*3+1)
y0(i,j,k) = ((i==0 ? j : i==1 ? 0 : k)*3+2)
z0(i,j,k) = ((i==0 ? k : i==1 ? j : 0)*3+3)
set key noautotitle
set view 61,49, 1.1
splot FILE u 1:2:3:($4-$1):($5-$2):($6-$3) w vec lc rgb "red" lw 2, \
for [c=0:1] for [i=0:2] for [j=0:1] for [k=0:1] FILE \
u (column(x0(i,j,k))):(column(y0(i,j,k))):(column(z0(i,j,k))): \
((i==0)*($4-$1)):((i==1)*($5-$2)):((i==2)*($6-$3)) \
every ::c::c w vec nohead ls 2 lw 1.5 lc c+2
### end of script
For current gnuplot versions 5.x, the script needs to be adapted slightly and you can be shorten the plot command a bit:
dashtype can be specified directly via dt 2, not via ls 2
one for loop less because in 4.6 lc variable doesn't seem to work together with ls 2.
splot FILE u 1:2:3:($4-$1):($5-$2):($6-$3) w vec lc rgb "red" lw 2, \
for [i=0:2] for [j=0:1] for [k=0:1] FILE \
u (column(x0(i,j,k))):(column(y0(i,j,k))):(column(z0(i,j,k))): \
((i==0)*($4-$1)):((i==1)*($5-$2)):((i==2)*($6-$3)):($0+1) \
w vec nohead lw 1.5 dt 2 lc var
Result: (created with gnuplot 4.6.0)

Thicker lines in the legend of gnuplot

I'm plotting some data curves with gnuplot, and they look like this:
However, the line samples in the legend are too thin. When you have more curves, it becomes hard to distinguish the colors. You can increase the thickness of the curves using "linewidth", e.g., by adding "lw 3" to the plot command, and you'd get this:
However, this increases the thickness everywhere. Is it possible to make the lines thick in the legend only? I know it can be done "the other way", by postprocessing on the output .png file. But is there a direct approach, using some gnuplot setting/wizardry?
Unfortunately, I don't know a way to control the thickness of the lines in the key, since they correspond to the lines being drawn. You can see what you can change by typing help set key in gnuplot.
Using multiplot, you can draw the plot lines first without the key, then draw the key again for 'ghost lines'. Here is a code sample which would do that:
set terminal png color size 800,600
set output 'plot.png'
set multiplot
unset key
plot '../batteries/9v/carrefour.txt' w lp, \
'../batteries/9v/philips.txt' w lp, \
'../batteries/9v/sony.txt' w lp
set key; unset tics; unset border; unset xlabel; unset ylabel
plot [][0:1] 2 title 'Carrefour' lw 4, \
2 title 'Philips' lw 4, \
2 title 'Sony' lw 4
In the second plot command, the function 2 (a constant) is being plotted with a y range of 0 to 1, so it doesn't show up.
I ran across this post and it gave me a critical idea.
The provided solution does not work in multiplot mode, since the second plot command will trigger the second plot, which is most likely not desired.
as a workaround one can set the original data as "notitle", then plot data outside of range with the same linetype and color in different thickness with the desired title. I'll just leave my current example here. It also includes linestyles that i have declared. So i just use the same linestyle (ls) to get the same color but change the thickness on the second line.
# for pngs
set terminal pngcairo size 1600,600 font ',18' enhanced
set output "pic_multi_kenngr_ana.png
set style line 2 lc rgb '#0ce90b' lt 1 lw 1.5 # --- green
set style line 3 lc rgb '#09e0b3' lt 1 lw 1.5 # .
set style line 4 lc rgb '#065fd8' lt 1 lw 1.5 # .
set style line 5 lc rgb '#4e04cf' lt 1 lw 1.5 # .
set style line 6 lc rgb '#c702a9' lt 1 lw 1.5 # .
set style line 7 lc rgb '#bf000a' lt 1 lw 1.5 # --- red
set multiplot layout 1,2
set xtics rotate
set tmargin 5
set xtics 12
set grid xtics
# set axis labels
set ylabel 'T [K]'
set xlabel 'Zeit [h]'
# select range
set xrange [0:48]
set yrange [290.15:306.15]
set title "(a) Bodentemperatur"
set key top right Right
plot 'par_crank_hom01lvls.04.dat' u 1:3 with lines ls 7 notitle,\
'par_crank_str01lvls.16.dat' u 1:3 with lines ls 2 notitle,\
500 t 'z = 4 cm' ls 7 lw 4,\
500 t 'z = 16 cm' ls 2 lw 4
################################################
set title "(b) Bodenwärmestrom an der Oberfläche"
set ylabel 'G [W m^{-2}]'
set yrange[-110:110]
unset key
plot 'par_crank_str01_ghf.dat' u 1:3 with lines
unset multiplot
I hope this will help someone
An even more simple work-around (imho) is to define the colours explicitly and plot each line twice, once with high lw for the key and also with the title to appear in the key, but adding "every ::0::0" which effectively ends up in plotting nothing, and once the normal way. See the following code snippet:
plot data u 0:1 w l linecolor rgb #1b9e77 lw 2 t "",\
data every ::0::0 u 0:1 w l linecolor rgb #1b9e77 lw 4 t "Title"
To expand on the NaN comment by #Svalorzen, the following will graph two lines of width 1 from some datafile.txt with no titles and create matching blank lines with the specified titles and width 5 for the key only:
plot [][]\
NaN title "Title1" w line lt 1 lc 1 lw 5,\
NaN title "Title2" w line lt 1 lc 2 lw 5,\
"datafile.txt" using 1:2 title "" w line lt 1 lc 1 lw 1,\
"datafile.txt" using 1:3 title "" w line lt 1 lc 2 lw 1
I find an answer for this:
Set key linewidth
in your case, must be:
plot '../batteries/9v/carrefour.txt' w l lw 1 linetype 1 notitle, 0/0 linetype 1 linewidth 5 title 'Carrefour'
rep '../batteries/9v/philips.txt' w l lw 1 linetype 2 notitle, 0/0 linetype 2 linewidth 5 title 'Philips'
rep '../batteries/9v/sony.txt' w l lw 1, linetype 3 notitle, 0/0 linetype 3 linewidth 5 title 'Sony'
Try something like:
plot # ... \
keyentry w l lw 1 lc 2 t "Title" # ...
And remove the old keys.

Resources