How do I put gridlines on the bottom layer and axes on the top layer?
45 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Star Strider
2019-8-10
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 个评论
Star Strider
2019-8-12
移动:Voss
2024-4-23
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.
更多回答(1 个)
Michael G. Baker
2019-8-27
编辑:Michael G. Baker
2019-8-27
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 个评论
Lukas
2020-4-3
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!