How to change a single xtick (x label)?

조회 수: 7 (최근 30일)
Paul Hinze
Paul Hinze 2023년 1월 30일
댓글: Star Strider 2023년 2월 1일
Dear Community
Here is the code on how i generated my figure:
x = 1:100;
y = rand (100);
plot(x,y)
I would like to change the zero label on the x-axis with 'no stim', at the same time ALL other values need to remain unchanged. Furthermore they need to stay flexible (Without magic numbers). Do you know how to do that?

채택된 답변

Star Strider
Star Strider 2023년 1월 30일
Perhaps something like this —
x = 1:100;
y = rand (100);
figure
plot(x,y)
xt = xticks;
xticklabels([{'No Stim'} compose('%3d',xt(2:end))])
This simply concatenates {'No Stim'} with the cell array created by compose to create the rest of the cell array defining the rest of the xticklabels.
.
  댓글 수: 4
Paul Hinze
Paul Hinze 2023년 2월 1일
i ran into a problem with this concept by using the compose command.
The attached picture shows the same question as before, but there the code does not work perfectly. The label of no stim should be shifted below the green errorbar by one index.
Here, is a snippet of a code i used:
xt = xticks;
% Fix xt
xt(end + 1) = depvars(2) - (depvars(3) - depvars(2)); % Concatenate vectors
xt = circshift(xt,1); % sort vector in risinf fashion
xticklabels([xt(1,{'No Stim'}) compose('%3d', xt(2:end))])
I tried to make the xt variable one index longer that it matches with the length and concept of the xticklabels, but somehow the code produces a faulty output. Can you help me?
Best, Paul
Star Strider
Star Strider 2023년 2월 1일
I get the impression that you are plotting the black line (and what appears to be a 'pchip' interpolant) first, and then the green value and errorbar separately. (I can’t follow the code snippet.)
It would really help to have your data. Lacking it, I did my best to emulate it, so that porting it to your data would be relatively straightforward —
x = 9000:250:12500;
y = (rand(size(x))+0.25)*100;
err = rand(size(x))*25;
figure
errorbar(x+500, y, err, '-k')
xt = xticks; % Get Original 'xticks' Vector
xlim([min(xt)-1000 max(xt)+500]) % Set X-Limits
xticks([9000 xt]) % Re-Define 'xticks' Vector
xticklabels([{'No Stim'} compose('%d',xt(1:end)-500)]) % Set 'xticklabels'
hold on
errorbar(9000, rand*100, rand*25, '-g', 'LineWidth',1.5) % Plot Green Errorbar
hold off
You may still have to tweak this to get it the way you want. This should get you started.
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by