如何让matlab在​画图时,保持左右y轴​与上下x轴都有轴线,​但是仅有左y和下x有​刻度线?

23 views (last 30 days)
远帅
远帅 on 8 Jul 2025
Answered: Sameer on 11 Jul 2025
在画图时,我无法单独控制不同x轴和y轴,只能同时设置

Accepted Answer

Sameer
Sameer on 11 Jul 2025
The axes box and ticks are controlled with properties like Box, XAxisLocation, YAxisLocation, and the Tick properties. By default, the ticks appear on the axes at the bottom (x) and left (y), but when you turn the box on (with box on), the axis lines also appear at the top and right—with ticks on all sides.
To achieve axis lines on all four sides but ticks only on the left and bottom, you can use a trick: overlay two axes objects. The bottom one will have box on, but no ticks; the top one will have ticks only on the left and bottom, but no box.
Here's an example:
x = 0:0.1:10;
y = sin(x);
% First axes: box and all lines, but no ticks
ax1 = axes('Position',[0.13 0.11 0.775 0.815]);
plot(x, y, 'b');
set(ax1, 'Box', 'on', ...
'XTick', [], ...
'YTick', [], ...
'Color', 'none');
% Second axes: only left and bottom ticks, no box, on top
ax2 = axes('Position', get(ax1,'Position'), ...
'Color', 'none', ...
'XAxisLocation', 'bottom', ...
'YAxisLocation', 'left', ...
'Box', 'off');
hold(ax2, 'on');
plot(ax2, x, y, 'b');
set(ax2, 'Box', 'off');
% Link axes so zooming/panning works together
linkaxes([ax1 ax2]);
% Send the first axes to the back
uistack(ax1, 'bottom');
Hope this helps!

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!