How do I put gridlines on the bottom layer and axes on the top layer?
Show older comments
I would like to wedge data between gridlines and axes. Using ax.Layer = 'top', gridlines are plotted over the data along with the axes (figure 1). But using ax.Layer = 'bottom' results in data over the axes (figure 2). See the two examples below.
Accepted Answer
More Answers (1)
Michael G. Baker
on 27 Aug 2019
Edited: Michael G. Baker
on 27 Aug 2019
You can do this by editing the hidden grid and axes structures. Note that this isn't supported behavior, and may be changed in a future release.
AX = axes;
jnk = imagesc(x_axis, y_axis, data);
jnk.AlphaData = data ~= 0; % Set null data to transparent. See caveat below!!
% Turn your full box on and force it to the front.
AX.Box = 'On';
AX.Layer = 'Top';
AX_ = struct(AX);
% Force grid lines to bottom layer.
% These also exist for Minor grids. It's unclear to me what the BackMajorEdge does.
AX_.XGridHandle.FrontMajorEdge.Layer = 'back';
AX_.YGridHandle.FrontMajorEdge.Layer = 'back';
Example results attached.
Caveat : This method won't work if you export to a format that has trouble with transparency. PNG and PDF work, but EPS will set the imagesc layer to opaque and your grids will disappear. Not sure if there's a work-around for this, or if it's a problem with any other plotting routines.
There's all sorts of other hidden settings in the axes structure that can be played with, as well, that make customizing plots way easier.
2 Comments
Lukas
on 3 Apr 2020
Had the same problem as OP and this worked nicely and easily, thanks.
Let's hope that by when this option becomes obsolote, Matlab will give an official option for lowering the grid while keeping the axes on top.
Michael Xie
on 7 Apr 2020
Nice hack. Exactly what I needed.
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!