How to place tick-labels on the axis, essentially replacing the ticks with the labels?

2 views (last 30 days)
Is there a way to replace the ticks on an axis with the tick labels? I have a figure where 2 subplots are stacked on top of each other and it feels crowded having the ticks labels floating above the ticks. I would like to replace the ticks with the labels if possible.
See below for an example of what I'd like to do:

Answers (2)

Star Strider
Star Strider on 7 May 2023
Perhaps something like this —
x = 5:5:25;
y = randn(size(x));
figure
plot(x, y)
xt = xticks;
xtl = xticklabels;
text(xt, zeros(size(xt))+min(ylim), xtl, 'Vert','middle', 'Horiz','center', 'BackgroundColor','w')
xticklabels([])
Experiment to get the result you want.
.
  2 Comments
Image Analyst
Image Analyst on 7 May 2023
I have no idea why the original poster thinks this is preferable to the way the rest of the world does it. Personally, I don't like it. At the very least I'd add grid lines.

Sign in to comment.


dpb
dpb on 7 May 2023
That's theorectically possible, but would be a pit(proverbial)a(ppendage) to accomplish; MATLAB HG2 has no provision to do any such thing as that...well, on second thought, I guess it might not be too bad after all; you could use a solid background on the text/annotation object to create the illusion of the broken axis line...
% preliminaries...
hL=plot(0:5:30,nan(1,7));
xlim([0 27])
xtk=xticks; xticks(xtk(2:end))
xticklabels([])
hAx=gca; hAx.YAxis.Visible='off';
box off
hF=gcf; hF.Color=hF.Color*0.8;
% down to business...
hTxt=text(xticks,zeros(size(xticks)),string(xticks),'horizontalalign','center','verticalalign','middle', ...
'BackgroundColor',hAx.Color);
There's not an easy way to "split" the background of the text label to avoid the area around the labels that I can think of at the moment, however.
An alternative if using subplots, however, to reduce the clutter if the axes are the same would be to use stackedplot instead and they then share the x axis.

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!