It seems a simple task cannot be realized although gnuplot offers several options:
How to bring the grid in front of the key/legend?
(let's aside whether the grid through the legend helps readability)
(triggered by this question: https://stackoverflow.com/q/79357685)
most obvious: set grid front
The legend does still white out the grid around the area of the legend
additionally: set key noopaque
key still isn't transparent, what you would expect
additionally: set term pngcairo transparent background
Ususally, key box is filled with background color, but setting transparent background color doesn't seem to work
set key fc rgb 0xff123456
transparent color to fill the legend box doesn't seem to work
Tested with Windows10 gnuplot 6.0.2
Just for better visibility of the issue: set grid lt -1
Script:
### how to bring grid to front?
reset session
set xtics 1
set mytics 2
set grid x,y,mx,my front lt -1
set key font ",16" noopaque
plot sin(x), cos(x)
### end of script
1) 2)
The key area is intentionally reserved so that the grid will not obscure the key contents. When grid lines are drawn, they are clipped against the key area. So it doesn't matter whether the grid is in front or in back; either way it will not obscure the key. That was a design decision made sometime last century, prior to version 3.7.
3)
You can use the terminal background as a fillcolor for at least some terminal types, as in the sequence below.
It may be true that the pngcairo terminal doesn't store an alpha channel for the background color (I haven't looked into that) but if so that doesn't really have anything to do with the key area.
4)
The command is
set key opaque fillcolor <whatever>Thanks, Ethan! So, in summary: it is not intended to put the grid in front of the legend. Actually, it is prevented by design.