Insert xticks in bold in existing xticks
13 views (last 30 days)
Show older comments
Hi,
I would like to make some change to the xticks of my plot without doing it manually.
So far i'm changing xticks of certain values by a text with :
names = {'text1'; 'text2'; 'text3'};
plot(...
set(gca,'xtick',[position1, position2, position3],'xticklabel',names)
The downside is that now I only have those 3 texts displaying in the x axis. I would like to have the default ticks PLUS those texts at those positions (in bold if possible). And just erase default ticks that are at those 3 positions.
Example if I have on the x axis the labels:
1, 2, 3, 4, 5, 6, 7, 8
I would like to have :
1, 2, text1, 4, 5, text2, 7, text3
How can I do this?
0 Comments
Answers (1)
dpb
on 5 Sep 2018
You've got to write the same number of tick labels as ticks and place the non-automatic text in the locations desired, explicitly. There's not sufficient syntax to write a portion of the 'XTickLabel' array alone.
hAx=axes; % make an axes to play with
xlim([1 8]) % set limits
xlbl=xticklabels; % retrieve default labels
ix=[3 6 8]; % define those to change
for i=1:length(ix) % make up the new ones for those locations...
xlbl(ix(i))={num2str(i,'\\bfText%d')};
end
xticklabels(xlbl) % and write them back.
3 Comments
dpb
on 6 Sep 2018
Edited: dpb
on 6 Sep 2018
doc xticklabels
...
xl = xticklabels returns the x-axis tick labels for the current axes.
...
Introduced in R2016b
If your release is earlier, then you'll have to use
xlbl=hAx.XTickLabel;
or if even earlier than R2014, then
xlbl=get(hAx,'XTickLabel');
Somehow you've got to get the existing tick label array to operate on.
hAx=axes;
just created an axis to demonstrate with; use the handle to the axes object you're trying to add the labels. For the current axes, use
hAx=gca;
to get the handle into a variable instead of calling the function every time; gca can (and will) change which axes is returned if focus is changed, saving the handle to a variable ensures you're writing to the one intended.
See Also
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!