How do I put gridlines on the bottom layer and axes on the top layer?

76 views (last 30 days)
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

Star Strider
Star Strider on 10 Aug 2019
Perhaps overplotting the left and right axes (and the tick lines if you want the tick lines) will do what you want:
x = linspace(0, 1, 1000); % Create Data
y = rand(1, 1000); % Create Data
figure
plot(x, y)
ax = gca;
ax.Layer = 'bottom';
tl = ax.TickLength(1);
yaxtl = diff(xlim)*tl;
yt = ax.YTick;
hold on
plot([1 1]*min(xlim), ylim, '-k') % Left Vertical Axis
plot([1 1]*max(xlim), ylim, '-k') % Left Vertical Axis
plot([0; yaxtl]*ones(size(yt)), [yt; yt], '-k') % Left Y-Ticks
plot([max(xlim); max(xlim)-yaxtl]*ones(size(yt)), [yt; yt], '-k') % Right Y-Ticks
hold off
grid
This overplots the vertical axes and the tick lines, so neither will be hidden. If you don’t want the tick lines, don’t plot them.
  2 Comments
Manan Vyas
Manan Vyas on 12 Aug 2019
Moved: Voss on 23 Apr 2024 at 14:51
I was hoping for a more elegant solution, but this approach worked. One may have to play with how ax.TickLength distance gets used depending on the plot type.
Also, ax.XTick and ax.YTick may need to be explicitly define before using them via xticks and yticks commands.
Star Strider
Star Strider on 12 Aug 2019
Moved: Voss on 23 Apr 2024 at 14:51
Thank you.
There are two tick length ratios, the first is for 2D plots and the second is for 3D plots (according to the documentation).
Use:
tl = ax.TickLength(2);
for 3D plots.
It is relatively straightforward to change the tick labels as desired, however the tick values must always be within the original tick range for their respective axes. With that constraint, they can be anywhere along the axis.

Sign in to comment.

More Answers (1)

Michael G. Baker
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
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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!