gnuplot: How to align multiplots relative to axes coordinates? - alignment

From this question it turned out that in some cases it might be desirable to align a subplot relative to another's plot axes coordinates.
If you check the gnuplot documentation, you will find that labels, arrows, objects (rectangles, polygons, circles, ...) can be positioned in different coordinate systems, i.e. axes, graph and screen (see help coordinates).
Subplots in a multiplot environment, however, (as far as I know) can only be aligned and sized relative to the screen, see help origin and help size.
Of course, you could always fiddle around with screen coordinates to find the desired positions, but when the size of the terminal or plot, axis labels or automargins might change you will have to start over again.
Question: How to align a subplot relative to the coordinates of another plot?

Aligning a subplot relative to another's plot coordinates is basically a "simple" transformation of coordinates, however, it's not too obvious how to do this in gnuplot.
I haven't found an example on the gnuplot homepage or elsewhere (this shouldn't mean it doesn't maybe exist somewhere), so I would like to share a way which should make life easier and somebody else might find it useful not to have to "reinvent the wheel".
Code:
### multiplots relative to other plots' axes coordinates
reset session
# function to store the current terminal values for later use
GetPlotParams(n) = sprintf("%g %g %g %g %g %g %g %g", \
GPVAL_X_MIN, \
GPVAL_X_MAX-GPVAL_X_MIN, \
GPVAL_Y_MIN, \
GPVAL_Y_MAX-GPVAL_Y_MIN, \
real(GPVAL_TERM_XMIN)/GPVAL_TERM_XSIZE*GPVAL_TERM_SCALE, \
real(GPVAL_TERM_YMIN)/GPVAL_TERM_YSIZE*GPVAL_TERM_SCALE, \
real(GPVAL_TERM_XMAX-GPVAL_TERM_XMIN)/GPVAL_TERM_XSIZE*GPVAL_TERM_SCALE, \
real(GPVAL_TERM_YMAX-GPVAL_TERM_YMIN)/GPVAL_TERM_YSIZE*GPVAL_TERM_SCALE ) \
# real() in order to avoid integer division
PosX(s,x) = word(s,5)+word(s,7)*(x-word(s,1))/word(s,2) # screen position of the subplot
PosY(s,y) = word(s,6)+word(s,8)*(y-word(s,3))/word(s,4)
SizeX(s,dx) = word(s,7)*dx/word(s,2) # screen size of a subplot
SizeY(s,dy) = word(s,8)*dy/word(s,4)
set multiplot
set title "First plot"
set xtics 1
set yrange[-100:100]
set ytics 20
set grid xtics, ytics
plot 25*sin(2*x)/x+10 w l lw 2 notitle
mp1 = GetPlotParams(0) # store parameters of 1st plot
# set white background for future plots
set object 1 rect from graph 0, graph 0 to graph 1, graph 1 behind fc "white"
set margins 0,0,0,0
unset xlabel
set format x ""
unset ylabel
set format y ""
set grid x2tics, ytics
# second plot
set title "2^{nd} plot at -9,30 rel. to first plot" offset 0,-0.5
set origin PosX(mp1,-9),PosY(mp1,30) # relative to plot1
set size SizeX(mp1,5),SizeY(mp1,50) # relative to plot1
plot 50*sin(x) w l lc "red" notitle
mp2 = GetPlotParams(0) # store parameters of 2nd plot
# third plot
set title "3^{rd} plot at 4,30 rel. to first"
set origin PosX(mp1,4),PosY(mp1,30) # relative to plot1
set size SizeX(mp1,5),SizeY(mp1,50) # relative to plot1
set format y ""
plot 50*cos(x)+20*sin(2*x) w l lc "magenta" notitle
mp3 = GetPlotParams(0) # store parameters of 3rd plot
# fourth plot
set title "4^{th} plot from 2^{nd} x=0 to 3^{nd} x=0"
set origin PosX(mp2,0),PosY(mp1,-80) # relative to plot1 and plot2
set size PosX(mp3,0)-PosX(mp2,0),SizeY(mp1,60) # relative to plot1, plot2 and plot3
set xrange [*:*]
set format x
set yrange[*:*]
set format y
plot x**2 w l lc "green" notitle
mp4 = GetPlotParams(0) # store parameters of 4th plot
# fifth plot
set title "5^{th} plot" offset 0,-1
set origin PosX(mp4,-4),PosY(mp4,20) # relative to plot4
set size SizeX(mp1,5),SizeY(mp4,60) # relative to plot1 and plot4
set format x ""
set format y ""
plot (int(2*x)%5)*8 w l lc "blue" notitle
mp5 = GetPlotParams(0)
unset multiplot
### end of code
Result: (actually, a nonsense plot just for illustration. Terminal: set term wxt size 650,480)

Related

Plot multiple axis aligned with gnuplot

I am trying to plot a time series on the left, and its histogram on the right. I have 10000 samples. The files ts.dat and hist.dat contains point to plot in the correct form. How can I plot the two axis aligned? With the code below, gnuplot aligns the canvas, and as the x labels are of different length the axis y length is different.
set term pngcairo font "Times New Roman, 8" size 640, 240
set out 'mp.png'
set multiplot
set tics out
set tics scale 0.5
unset key
set size .66, 1
set yrange[-6:6]
set xtics rotate by 45 right
set xlabel "Iteration no."
set ylabel "USD"
plot 'ts.dat' with lines
set origin .66, 0
set size .33, 1
set yrange [-6:6]
set format y ""
set style fill transparent solid 0.3
set xlabel "No. of occurences"
unset ylabel
plot 'hist.dat' using 2:1 with filledcurves
unset multiplot
Check help margins.
If you set a fixed bottom margin manually, e.g. set bmargin 3 or set bmargin at screen 0.1, the bottom x-axes should be aligned in both plots.

Gnuplot multiplot positions all text incorrectly in epslatex terminal

I am trying to use the multiplot feature of gnuplot to make a inset graphic on the main plot. I can generate the plot exactly as I want with term='wxt' except for the axis labels, which require LaTeX formatting for generating the desired symbols. When I submit the same commands to term='epslatex', the plot is fine, but all text (axis and tic mark labels) is positioned incorrectly.
I thought using the set size & origin commands might have confused the epslatex terminal output, so I attempted to use the layout command and make the plots side by side just to see if the text would print correctly. It did not.
I'm using gnuplot 4.6 patch 4, and Linux Mint 17.
My script is below. The commented sections indicate the original script that used set size and origin commands to manually place the second plot as a inset, rather than side by side.
set term epslatex color font ",16"
unset key
set termoption dash
set style line 1 lc rgb 'blue' lw 2 lt 1
set style line 2 lc rgb 'red' lw 2 lt 3
set style line 3 lc rgb 'green' lw 2 lt 5
set style line 4 lc rgb 'magenta' lw 2 lt 7
set style line 5 lc rgb 'black' lw 1 lt 0
set output "gr-thresholds.tex"
#set size 1,1
# set multiplot
set multiplot layout 1,2
# bigger plot
set autoscale
set ytics scale default autofreq
set xrange[0:14]
set yrange[0:1.7]
set xlabel 'r (\AA)'
set ylabel '$g(r)$'
#set size 1,1
#set origin 0,0
plot "foo1.csv" w l ls 2, \
"foo2.csv" w l ls 3 , \
"foo3.csv" w l ls 1, \
"foo4.csv" w l ls 4
#small inset
#set size 0.4, 0.4
#set origin 0.5,0.15
set xrange[1.2:2.2]
set yrange[0:0.8]
set ytics 0, 0.2, 2
set xlabel ""
set ylabel ""
plot "foo1.csv" w l ls 2, \
"foo2.csv" w l ls 3 , \
"foo3.csv" w l ls 1, \
"foo4.csv" w l ls 4
unset multiplot
set output
The figure that was generated:
It might be a problem with the way you generate a pdf. The two commands dvipdfm and dvipdf produce different outcomes.
If I take your code, but plot sin(x) instead, and use the following in the terminal:
$ latex file.tex
$ dvipdfm file.dvi
I also get a mismatch between the axes and the plots.
If I use dvipdf however everything looks fine:
$ dvipdf file.dvi
Ok,
Per Tom Fenech's suggestion, I made a minimum code sample to reproduce the error, and the issue that arose is a machine state problem. To generate my graphs, I had run the script twice, once using term wxt and then again using term epslatex.
The problem is that somewhere the state of the gnuplot environment is changed and is not reset by this script. Specifically, the first time through, the default placement of the text labels is fine. The second time through, the range and labels are still attached to the size and origin from the last plot, which is the inset. I thought this was due to the order of the commands set origin/size relative to x/ylabel and x/y range, but simply running the below code twice without restarting gnuplot will generate two different plots. The first time is exactly what I wanted, the second time will skew the labels as shown above.
So I have a "solution", but it is fragile. I would appreciate if someone could explain what I need to do to make this script run multiple times without restarting each time.
Cheers,
--Jim
set term epslatex color font ",16"
unset key
f(x) = sin(x)
set output "sin.tex"
set multiplot
set size 1,1
set origin 0,0
set xrange[0:14]
set yrange[0:6]
set xlabel 'r (\AA)'
set ylabel '$g(r)$'
plot f(x)
#small inset
set size 0.4, 0.4
set origin 0.5,0.15
set xrange[1:3]
set yrange[0:4]
set ytics 0, 0.2, 2
set xlabel ""
set ylabel ""
plot f(x)
unset multiplot
set output

Latex figure caption overlaps gnuplot figure generated with the epslatex terminal

I have to plot 4 graphs without vertical spaces between them and add a caption in my tex document. The problem is that the caption is overlapped with the x-axis label because of the zero bottom margin, I think.
The gnuplot file is something like this
set term epslatex
set output 'foo.tex'
set xrange [-1:1]
set yrange [-1:1]
set multiplot layout 4,1
set ylabel '$y$'
unset xlabel
unset xtics
set tmargin 0
set bmargin 0
plot x w lines
plot x*x w lines
plot x*x*x w lines
set xlabel '$x$'
set xtics
plot x*x*x*x w lines
unset multiplot
and the tex file is
\documentclass[a4paper,11pt]{toptesi}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\input{foo}
\caption{bla bla}
\end{figure}
\end{document}
and the "bla bla" comes over the x label... I tried to put an set bmargin "something" before the last graph but this have the problem that the last graph hasn't the size of the first three anymore... How to fix the size of plots but allow a bmargin for the tex caption?
Try this code...
set term epslatex size <width> <height>
set output 'foo.tex'
set xrange [-1:1]
set yrange [-1:1]
set multiplot
# setup sizes of single plots
set size 1,.25
set ylabel '$y$'
unset xlabel
unset xtics
set tmargin 0
set bmargin 0
# set bottom left corner of current plot
set origin 0,.75
plot x w lines
set origin 0,.5
plot x*x w lines
set origin 0,.25
plot x*x*x w lines
set xlabel '$x$'
set xtics
# set bmargin back
set bmargin 3 #might be enought for place xlabel, try it...
set origin 0,0
plot x*x*x*x w lines
unset multiplot
set out
set term pop
But... The bottom panel is smaller than others, so you must increase vertical size of bottom panel (using set size 1, a) a>0.25 and decrease vertical size of others (set size 1, b before each plot command) and satisfy that a+3b=1. This fine tuning will depend on size parameter of epslatex terminal.
Much much better solution might be this:
...
set size 0,.25
set tmargin 0
set bmargin 1
set origin .75,0
plot ...
set tmargin -1
set bmargin 2
set origin .5,0
plot ...
set tmargin -2
set bmargin 3
set origin .25,0
plot ...
set tmargin -3
set bmargin 4
set origin .25,0
plot ...
Now we might have enough space for bottom xlabel and all of the panels have same height. Unfotunatelly gnuplot 4.6.3 interprets tmargin -3 as tmargin 0. So we can't use it right now... May be in some newer version.

How to remove unknown slits when using filledcurve in Gnuplot (epslatex)?

I'm now trying to use filledcurve in gnuplot 4.6, patchlevel 1. Following shows the sample script:
set term epslatex
set output "figure.tex"
set xlabel "\\huge{x-axis}"
set ylabel "\\huge{y-axis}"
set format xy "\\LARGE{%.0f}"
set xrange [0.0:10.0]
set yrange [0.0:100.0]
set xtics 2.0
set ytics 20.0
set xtics offset 0, -0.3
f1(x) = x**1
f2(x) = x**2
f3(x) = x**3
set nokey
plot '+' using 1:(f2($1)):(f3($1)) with filledcurve lt 1 lc rgb "gray60",\
'+' using 1:(f1($1)):(f2($1)) with filledcurve lt 1 lc rgb "gray40",\
'+' using 1:(0.0):(f1($1)) with filledcurve lt 1 lc rgb "gray20"
I don't known why, but it seems that there are white annoying slits between bars. It cannot be get rid of even if I increase the number of set samples.
Is there any idea to remove these slits?
Unfortunately, this is a viewer problem related to the drawing of adjacent filled polygons, see also problematic Moire pattern in image produced with gnuplot pm3d and pdf output or the bug report #1259 cairolatex pdf fill patterns.
In your case you can use a workaround:
When you have only two columns in the using statement, the area is drawn as closed polygon and doesn't show these artifacts (filledcurves closed). So you must fill the area between each curve and the x1 axis (with filledcurves x1).
Because of a bug in the clipping of curves which exceed the y-range, you must do the clipping of the f3 curve yourself (i.e. use f3($1) > 100 ? 100 : f3($1)). This bug is fixed in the development version.
So you script is:
set term epslatex standalone
set output "figure.tex"
set xlabel "\\huge x-axis"
set ylabel "\\huge y-axis"
set format xy "\\LARGE %.0f"
set xrange [0.0:10.0]
set yrange [0.0:100.0]
set xtics 2.0
set ytics 20.0
set xtics offset 0, -0.3
f1(x) = x**1
f2(x) = x**2
f3(x) = x**3
set nokey
plot '+' using 1:(f3($1) > 100 ? 100 : f3($1)) with filledcurve x1 lt 1 lc rgb "gray60",\
'+' using 1:(f2($1)) with filledcurve x1 lt 1 lc rgb "gray40",\
'+' using 1:(f1($1)) with filledcurve x1 lt 1 lc rgb "gray20"
set output
system('latex figure.tex && dvips figure.dvi && ps2pdf figure.ps')
with the result (using 4.6.1):
Note also, that LaTeX commands like \huge don't take arguments, but are switches. Test e.g. \huge{A}BC, this will make all letter huge. Usually you must limit the scope of \huge with brackets like {\huge ABC}, but if the whole label is affected, it is enough to use set xlabel "\\huge x-axis". That doesn't change anything in your case, but may give you troubles in other circumstances :)

Irregular gnuplot x-values

I have some data (benchmarking results of k-selection algorithms) which has irregular x-values. I have them labeled explicitly (1, 50, 100, 500, 1000, 2500, and 5000).
Because these values are not linearly increasing (or exponentially increasing, although making the x-axis a logscale does improve things a bit, bug leaves a huge gap in between 1 and 50) they are oddly spread out and clumped together. Is there a way to scale the x-axis so that these data points are drawn at an even spacing?
Below is a sample of the grid spacing (labels and legends are not visible in the eps file, these are drawn later by the graphicx package) as well as the gnuplot commands I'm using.
set terminal epslatex
set xlabel 'k'
set ylabel 'seconds'
set tic scale 0
set key Left outside
set xtics rotate
set xtics ('1' 1, '50' 50, '100' 100, '500' 500, '1000' 1000, '2500' 2500, '5000' 5000)
set logscale y
set logscale x
set style data linespoints
set output 'selection.tex'
plot '../data/selection.dat' u 1:($2/1000) t 'Sort', \
'../data/selection.dat' u 1:($3/1000) t 'Partial Sort', \
'../data/selection.dat' u 1:($4/1000) t 'Heap', \
'../data/selection.dat' u 1:($5/1000) t 'Partial Heap', \
'../data/selection.dat' u 1:($6/1000) t 'Order Statistics'
The cleanest way to do it is to use the xticlabels command:
plot '../data/selection.dat' u ($2/1000):xticlabels(1) t 'Sort', \...
This takes the value from the first column and uses it as the x axis labels, while using the second column as data.
This is a bit like using the command
plot 'file' u 2
Which just plots the data from the second column against a dummy index (1,2,3,4...). This gives an even spacing to the data points, which seems to be what you want here.

Resources