Discussion:
Legend behavior would multiplot
(too old to reply)
Darby
2020-04-14 20:43:55 UTC
Permalink
I've run into a problem with controlling the legend when using multiplot. I want the legend to stay in 1 column on the right side of the plot. With just a single plot per page, this works. But if I use multiplot (4 plots per page, layout 2,2), the legend wants to split into multiple columns, even if I try to force it to one column with "maxcols 1". In this case, the legend is too wide and obscures some of the plots. However, a multiple with two tall plots side by side seems to do the right thing. So it seems like this is tied to the size of the plot, not the page. I've recreated this with some simple examples below. I'm using gnuplot 4.6 patchlevel 2 on CentOS 7.7.






set xrange [-pi:pi]
set key at screen 1,1

# ----------

# The legend for a single plot seems to respect the space of the plot
# and only uses 1 column.

set size 0.9,1.0
plot for [i=1:5] 10*sin(i*x) w l t 'sin',\
for [i=1:5] 10*cos(i*x) w l t 'cos',\
for [i=1:5] i*x w l t 'line',\
for [i=1:5] -i*x w l t 'line'
pause -1

# ----------

# The legend for a multiplot seems NOT to respect the space of the plots
# and uses 2 columns, despite the explicit request for one column.

set key maxcols 1
set multiplot layout 2,2 scale 0.8, 1.0

replot
replot
replot
replot

unset multiplot

pause -1

# ----------

# But a "layout 1,2" multiplot will do one column. Is it based on the height of the
# plot?

set multiplot layout 1,2 scale 0.8, 1.0

replot
replot

unset multiplot

pause -1
# ----------
Darby
2020-04-14 20:47:32 UTC
Permalink
Arg, subject was supposed to be "Legend behavior with multiplot"...
Jörg Buchholz
2020-04-16 11:22:56 UTC
Permalink
Post by Darby
I've run into a problem with controlling the legend when using
multiplot. I want the legend to stay in 1 column on the right side
of the plot. With just a single plot per page, this works. But if I
use multiplot (4 plots per page, layout 2,2), the legend wants to
split into multiple columns, even if I try to force it to one column
with "maxcols 1". In this case, the legend is too wide and obscures
some of the plots. However, a multiple with two tall plots side by
side seems to do the right thing. So it seems like this is tied to
the size of the plot, not the page. I've recreated this with some
simple examples below. I'm using gnuplot 4.6 patchlevel 2 on CentOS
7.7.
In your example you generate a legend for every plot at the same place.
A legend can't be longer or higher than the plot itself.

You can specify the place of every key entry separately in the plot
command. Than you can place the key entry where ever you want.

I don't know if this works with gnuplot 4.6, I tested it with gnuplot 5.2



set xrange [-pi:pi]
set key at screen 1,1
set key maxcols 1
set multiplot layout 2,3 columnsfirst scale 1.0, 1.0
plot 10*sin(1*x) w l t 'sin' at 0.9,0.9,\
10*sin(2*x) w l t 'sin' at 0.9,0.86,\
10*sin(1*x) w l t 'sin' at 0.9,0.82,\
10*sin(2*x) w l t 'sin' at 0.9,0.78,\
10*sin(2*x) w l t 'sin' at 0.9,0.74,\
10*cos(1*x) w l t 'cos' at 0.9,0.70,\
10*cos(2*x) w l t 'cos' at 0.9,0.66,\
10*cos(1*x) w l t 'cos' at 0.9,0.62,\
10*cos(2*x) w l t 'cos' at 0.9,0.58,\
10*cos(2*x) w l t 'cos' at 0.9,0.54,\
1*x w l t 'line' at 0.9,0.50,\
2*x w l t 'line' at 0.9,0.46,\
3*x w l t 'line' at 0.9,0.42,\
4*x w l t 'line' at 0.9,0.38,\
5*x w l t 'line' at 0.9,0.34,\
-1*x w l t 'line' at 0.9,0.30,\
-2*x w l t 'line' at 0.9,0.26,\
-3*x w l t 'line' at 0.9,0.22,\
-4*x w l t 'line' at 0.9,0.18,\
-5*x w l t 'line' at 0.9,0.14
unset key
replot
replot
replot
unset multiplot


Jörg
Jörg Buchholz
2020-04-20 12:31:06 UTC
Permalink
Post by Darby
I've run into a problem with controlling the legend when using
multiplot. I want the legend to stay in 1 column on the right side
of the plot. With just a single plot per page, this works. But if I
use multiplot (4 plots per page, layout 2,2), the legend wants to
split into multiple columns, even if I try to force it to one column
with "maxcols 1". In this case, the legend is too wide and obscures
some of the plots. However, a multiple with two tall plots side by
side seems to do the right thing. So it seems like this is tied to
the size of the plot, not the page. I've recreated this with some
simple examples below. I'm using gnuplot 4.6 patchlevel 2 on CentOS
7.7.
You can do a "dummyplot" only for the key. This works with older
versions than 5.2

reset
unset key
set xrange [-pi:pi]
set multiplot
set size 0.4,0.5
set origin 0.0,0.0
plot for [i=1:5] 10*sin(i*x) w l t 'sin',\
for [i=1:5] 10*cos(i*x) w l t 'cos',\
for [i=1:5] i*x w l t 'line',\
for [i=1:5] -i*x w l t 'line'

set size 0.4,0.5
set origin 0.0,0.5
replot

set size 0.4,0.5
set origin 0.4,0.0
replot

set size 0.4,0.5
set origin 0.4,0.5
replot

#---dummyplot--------
set key left
set size 0.2,1.0
set origin 0.8,0.0
unset grid
unset tics
unset border
set xrange [0:1]
set yrange [0:1]
plot for [i=1:5] (10*sin(i*x))-10 w l t 'sin',\
for [i=1:5] (10*cos(i*x))-10 w l t 'cos',\
for [i=1:5] (i*x)-10 w l t 'line',\
for [i=1:5] (-i*x)-10 w l t 'line'

unset multiplot


Jörg

Loading...