Align ylabel in gnuplot multiplot - gnuplot

Is there a way to align vertically the y labels of in gnuplot's multiplot so that they are below each other? The problematic example is below (the red line is annotation showing the problem):
set xrange[0:10]
set multiplot layout 2,1 margins 0.1,0.95,0.1,0.95 spacing 0,0
set ylabel "y1\nlabel"
set ytics
set format y "%.2f"
plot -1000*cos(x)**2 w l lt 4 lw 2
set ylabel "y2\nlabel"
set format y "%.1f"
plot cos(x) w l lt 5 lw 2
unset multiplot
which generates:
And I would like to automatically position the labels such, that the red annotated line "touches the same text". Note that I am really interested in automatic way or more correct way that a workaround using a trial and error with set ylabel "lable" offset -x,0

As you already noted, you can set an offset to the x- or y-label (check help xlabel), however, no absolute position.
This you can do with setting your own label. You can set the position relative to the screen and/or relative to the graph. gnuplot keeps these values for the second plot, no need to specify again. Check help label.
Check the following example:
Code:
### align ylabels in multiplot
reset session
set xrange[0:10]
set multiplot layout 2,1 margins 0.15,0.95,0.1,0.95 spacing 0,0
set format x ""
unset ylabel
set label 1 "y1\nlabel" at screen 0.02, graph 0.5 center rotate by 90
set ytics 200
set ytics add ("" -1000) # remove -1000
set format y "%.2f"
set grid x,y
plot -1000*cos(x)**2 w l lt 4 lw 2
set format x
set label 1 "y2\nlabel"
set ytics 0.4
set format y "%.1f"
plot cos(x) w l lt 5 lw 2
unset multiplot
### end of code
Result:

Related

multiplot of isolines and points creates two axis frames

I've created a multiplot containing an isoline plot f(x,y) and a point, (0,0) on the zero level set of the isoline plot. Unfortunately the point plot appears to create a second axis frames as shown below
f(x,y)=2*x**2 - x + 2*y**2 - y - 2
set multiplot
set xrange [-3:3]
set yrange [-3:3]
set isosamples 250
set contour
unset surface
set view map
set key out
set cntrparam levels incremental 0,1,5
splot f(x,y)
plot "< echo '1 1'"
set nomultiplot
What can I do to solve this problem?
Update
A bit more context. In the larger problem I am using multiplot to superimpose two isoline plots, as hinted below. Both isoline plots share a common axis frame.
...
set cntrparam levels incremental 0,1,5
splot f(x,y)
set cntrparam levels discrete 0
g(x,y)=(x - 1)**2 + y**2 - 1
splot g(x,y)
Unless there are additional requirements that mandate use of multiplot, the simplest solution is to draw your single point as a label instead. The label comes with a point for free. You only want the point, so the label text is an empty string.
If this was a proxy question for a more complicated requirement, please edit the question to give more detail.
f(x,y)=2*x**2 - x + 2*y**2 - y - 2
set label 1 "" at 1,1,0 point pt 7 lc "blue"
set xrange [-3:3]
set yrange [-3:3]
set isosamples 250
set contour
unset surface
set view map
set key out
set cntrparam levels incremental 0,1,5
splot f(x,y) with lines
Edit
The multiplot version:
f(x,y)=2*x**2 - x + 2*y**2 - y - 2
g(x,y)=(x - 1)**2 + y**2 - 1
set label 1 "" at 1,1,0 point pt 7 lc "blue"
set xrange [-3:3]
set yrange [-3:3]
set isosamples 250
set contour
unset surface
set view map
set multiplot
set cntrparam levels incremental 0,1,5
set key out top
splot f(x,y) with lines
set cntrparam levels discrete 0
set key out bottom
splot g(x,y) with lines
unset multiplot
This would be my suggestion:
you don't need multiplot (I would use it only if absolute necessary)
plot the contour data into a table and instead of 3D splot and "set view map" use a 2D plot. Because after set contour you cannot plot a single datapoint as non-grid data.
there are a lot of ways to plot a single data point. My preference would be without system call.
you might want to set size ratio -1, then a circle appears as a circle
Update:
Here is the extended example after you mentioned your second contour function g(x,y).
Just to mention the differences between a multiplot solution and a single plot solution:
Script (multiplot): (with fixed margins and legend box positions relative to screen)
easy handling of the legend entries and boxes (but manual positioning if more than 2)
you need to set fixed margins to get reliable overlap independent of the legend. At least, fixed top/bottom margins are enough if you set size ratio -1. Additionally, you can set the legend to a fixed position relative to the screen.
I recommended to unset border, tics and labels for the second plot, otherwise these elements get a slight "bold" appearance if you plot them several times on top of each other (you will notice if you compare the numbers on Ethan's first and second plot)
zooming is not possible with multiplot in interactive terminals, e.g. wxt, qt
requires another multiplot if you want to plot single/multiple points from another file/datablock
so far, I haven't found out how to freely control the color of the contour lines. But I'm sure there is a way.
### plot several contour plots and add a single point (multiplot)
reset session
f(x,y) = 2*x**2 - x + 2*y**2 - y - 2
g(x,y) = (x-1)**2 + y**2 - 1
set xlabel "x-label"
set xrange [-3:3]
set ylabel "y-label"
set yrange [-3:3]
set samples 100
set isosamples 100
set contour
unset surface
set view map
set size ratio -1
set multiplot
xPos = 0.95
set key at screen xPos,0.9 title "Main title" font ",12"
set bmargin screen 0.15
set tmargin screen 0.92
set cntrparam levels incremental 0,1,5
splot f(x,y) w l
set cntrparam levels discrete 0
unset border
unset tics
unset xlabel
unset ylabel
set key at screen xPos,0.5 notitle
splot g(x,y) w l
set key at screen xPos,0.35 notitle
plot '+' u (1):(1) every ::::0 w p pt 7 lc "red" ti "You are here"
unset multiplot
### end of script
Result:
Script (single plot):
zooming possible in interactive terminals, e.g. wxt, qt
extra tables for contours
a bit more effort with the legend (but auto positioning)
easy customization of contour line color
For the single plot, the extraction of the contour level labels is a bit tricky and needs some explanations.
The contour data in the datablocks looks like this:
# Surface 0 of 1 surfaces
# Curve title: "f(x,y)"
# Contour 0, label: 5
-1 -1.15789 5
-1.15789 -1 5
-1.54386 -0.333333 5
...
-0.333333 -1.54386 5
-1 -1.15789 5
# Contour 1, label: 4
-0.333333 -1.38596 4
...
The data is separated by two empty lines. So, you can address the subblocks by using index (check help index).
So, now, after skipping the first 5 lines, you need to extract the contour label value from each the first line (header) of each (sub)block from the 5th column. For this, you need to tell gnuplot that # is not a comment character anymore (check help datafile commentschars).
Furthermore, the legend entry for the functions names are realized by keyentries (check help keyentry) with a fully transparent point according to the color scheme 0xaarrggbb where alpha is `0xff´.
### plot several contour plots and add a single point (single plot)
reset session
f(x,y) = 2*x**2 - x + 2*y**2 - y - 2
g(x,y) = (x-1)**2 + y**2 - 1
set xlabel "x-label"
set xrange [-3:3]
set ylabel "y-label"
set yrange [-3:3]
set samples 100
set isosamples 100
set contour
unset surface
set table $ContourF
set cntrparam levels incremental 0,1,5
splot f(x,y)
set table $ContourG
set cntrparam levels discrete 0
splot g(x,y)
unset table
set size ratio -1
set key noautotitle left at graph 1.05,1 title "Main title" font ",12"
set datafile commentschar ''
plot keyentry w p lc rgb 0xff123456 ti "f(x,y)", \
for [i=0:*] $ContourF u 1:2:3 skip 5 index i w l lc i ti columnhead(5), \
keyentry w p lc rgb 0xff123456 ti "g(x,y)", \
for [i=0:*] $ContourG u 1:2:3 skip 5 index i w l lc "blue" ti columnhead(5), \
'+' u (1):(1) every ::::0 w p pt 7 lc "red" ti "You are here"
### end of script
Result:

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:

GNUPLOT: Merge key entries with multiplot

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):

Gnuplot scatter with xticlabels and errorbars

I'm trying to do a scatter plot with Y error bars in GNUPLOT, in which the X axis are "names", not numbers.
I'm using this code:
#!/bin/sh
gnuplot -persist <<PLOT
set boxwidth 0.99 relative #ancho relativo de las barras
set border linewidth 1.5
set xlabel "xlabel" font "Verdana,18"
set xlabel offset 0,-1
#set xrange [-5:5]
set xtics font "Verdana,12"
set ylabel "ylabel" font "Verdana,18"
set ylabel offset -2,0
set yrange [-15:15]
set ytics font "Verdana,12"
set key at 4,4 font "Verdana,18"
set style line 1 lc rgb '#0060ad' pt 7 # circle
set xtics rotate by -45 #rota ángulo
plot "file.txt" using 0:2:3:xticlabels(1) with yerrorbars ls 1
quit
PLOT
As a bash script, and then the file.txt is:
Peter 3.06 0.5035
Charles 4.6576 0
Luis -13.1790 0
Where the third column is the Y error bar. However, data appears to initiate exactly in the Origin and not as usual when histogram is used...
Any clues to "shift" or set a range on X with non-numeric values?
Thank you in advance.
If you want to use autoscaling on the x-axis and just add some space to the right and left, then use set offset:
set yrange [-15:15]
set style line 1 lc rgb '#0060ad' pt 7 # circle
set xtics rotate by -45
set offset 0.5,0.5,0,0
plot "file.txt" using 0:2:3:xticlabels(1) with yerrorbars ls 1 notitle

Only 1 y label in GNUplot multiplot

I use gnu plot to draw a multiplot and in my script I set the y label like that:
set ylabel "foobar"
Now every plot in the multiplot has a dedicated y label on their y axis. However, I would like to have only one y label for all the plots in the multiplot and center that label also on the common y axis. How can I do that? The multiplot layout I use is a 7.1 So all the plots have the same y axis.
The simplest way is to make the first plot, then turn off the y label:
set ylabel 'foo'
set multiplot
plot 'data1.dat'
unset ylabel
plot 'data2.dat'
plot ...
unset multiplot
This will make the x-dimension of the first plot different from that of all the other plots, so you may have to play with the margins if you want all the plots the exact same size.
Plot the individual panels of reduced size without labels but with border, tics and title, then define a full-sized panel with labels but without border, tics and title.You may have to plot a dummy function (1/0).
Global label workaround
This is not ideal, but if you desperate like me, you can use a rotated global label + larger left margins:
#!/usr/bin/env gnuplot
label_label_size = 14
set terminal png
set output "gnuplot.png"
set multiplot layout 2,1 title "Multiplot with one ylabel" font ",18"
set lmargin 10
set label "My y-label" at screen 0.05,0.5 center front rotate \
font "," . label_label_size
plot sin(x)
set xlabel 'My x-label' font "," . label_label_size
plot cos(x)
Here is a realistic application that motivated me to do this: Heap vs Binary Search Tree (BST)
Tested in gnuplot 5.2 patchlevel 6, Ubuntu 19.04.
This is basically the suggestion from Fabian Claremont's answer, but (for beginners) put into code and visualized. Actually, Ciro Santilli's solution is even shorter.
Tested with gnuplot 5.2. For gnuplot 4.6 (the time of OP's question), replace reset session with reset and set margin 8,-1,1-1 with set lmargin 8 and set bmargin 1.
Code:
### Multiplot with single y-label
reset session
unset key
set sample 500
set multiplot layout 7,1
unset ylabel
set linetype 1 lc rgb "red"
set margins 8,-1,1,-1 # left, right, bottom, top (-1=auto)
set ytic 1.0
set title "Plot 1" offset 0,-1
plot sin(1*x)
set title "Plot 2" offset 0,-1
plot sin(2*x)
set title "Plot 3" offset 0,-1
plot sin(3*x)
set title "Plot 4" offset 0,-1
plot sin(4*x)
set title "Plot 5" offset 0,-1
plot sin(5*x)
set title "Plot 6" offset 0,-1
plot sin(6*x)
set title "Plot 7" offset 0,-1
plot sin(7*x)
set lmargin -1 # automatic lmargin
unset title
set origin 0,0
set size 1,1
set border 0
unset tics
set ylabel "This is a centered y-label"
plot [][0:1] -1 # plot a dummy line out of range
unset multiplot
### end of code
Result:

Resources