2

I'm trying to plot something like this:

set key top left box opaque
set border back
plot sin(x)

but I want the key to have a different background color than the rest of the graph. For example, if the graph is white, I want to the key to have a grey background. Or something like that.

I'm relatively new to Gnuplot, but I have looked in the manual and there doesn't seem to be a direct way to do this.

Can someone suggest a solution?

Thanks.

johnymm
  • 131
  • 6

1 Answers1

1

Yes, if you check help key, there seems to be no option to set the background color of the key box.

A workaround might be the following: With opaque, the key box will be filled with the background color. So, the "trick" is to the change the background color of the terminal to the desired key box color, but then add, e.g. a white rectangle spanning the whole screen. Do not forget to use the option behind. Additionally, to avoid a residual line at the border of the screen, set the coordinates of the rectangle from -0.1,-0.1 to 1.1,1.1. It works with wxt terminal. You need to test whether this works also with other terminals.

Code:

### key background
reset session

set term wxt background rgb "grey"

set object 1 rectangle from screen -0.1,-0.1 to screen 1.1,1.1 fc rgb "white" behind
set key opaque font ",12"

plot sin(x), cos(x)
### end of code

Result:

enter image description here

Addition:

@johnymm, for me it also works fine with pdfcairo terminal.

### key background
reset session

set term pdfcairo background rgb "grey"
set output "KeyBackground.pdf"

set style rect fc rgb "white" fs noborder
set object 1 rectangle from screen 0,0 to screen 1,1 behind

set key opaque font ",12"

plot sin(x), cos(x)
set output
### end of code
theozh
  • 22,244
  • 5
  • 28
  • 72
  • Thanks. It doesn't work as well with other terminals, but I might be able to fiddle with the "object" options to make it work. However, wouldn't it be better to make the regular background white and put a rectangle object just underneath the key? Is that possible ? – johnymm Oct 27 '19 at 16:00
  • Of course, you can place a colored/semi-transparent rectangle behind the key box. But how do you determine the exact size and position? Manually? Tedious! With this workaround everything is automatically, but too bad if this doesn't seem to work on other terminals. Which terminal are you using? – theozh Oct 27 '19 at 17:18
  • I'm using pdfcairo. – johnymm Oct 27 '19 at 18:14
  • I figured out how to add the a box just below the the key. You're right, figuring out positioning is not automatic, but it's not super tedious. Using the graphs coordinate system, it works relatively well. How do I add transparency though? I've been using the "back" option. – johnymm Oct 27 '19 at 18:19
  • in general transparency of a rectangle you can e.g. add with `set obj 1 rectangle from 0,0 to 5,5 fc rgb "0xccff0000"`. Color format is given in "AARRGGBB". But here, I guess transparency does not work when using as terminal background. – theozh Oct 28 '19 at 07:45