Plots: aligning categorical y-axis labels

조회 수: 9 (최근 30일)
John Wills
John Wills 2023년 12월 4일
댓글: Les Beckham 2023년 12월 5일
Hi Everyone,
I'm trying to plot a set of error bars with corresponding, categorical labels shown on the right-side (Y2) axis:
% create dummy data
y = [1,2,3];
x = [5,10,20];
err = 0.5*ones(size(y));
% draw plot
figure(1)
yyaxis right
errorbar(x,y,err,'.b','horizontal')
xlim([0 30])
ylim([0 4])
% hide Y1 axis
hax = gca;
hax.YAxis(1).Visible='off';
% add categorical labels to Y2 axis
hax.YAxis(2).TickLabels = {'Label one','Label two','Label three'};
Instead of repeated Y2 labels of 'Label one', 'Label two', 'Label three' I would like each label to be used once - opposite each of the error bars. That is at the positions specified by y.
Any help with this would be much appreciated! Many thanks.

채택된 답변

Les Beckham
Les Beckham 2023년 12월 4일
% create dummy data
y = [1,2,3];
x = [5,10,20];
err = 0.5*ones(size(y));
% draw plot
figure(1)
yyaxis right
errorbar(x,y,err,'.b','horizontal')
xlim([0 30])
ylim([0 4])
% hide Y1 axis
hax = gca;
hax.YAxis(1).Visible='off';
% add categorical labels to Y2 axis
hax.YAxis(2).TickValues = y; %<<<< Add this to specify where the ticks (and hence the labels) are located
hax.YAxis(2).TickLabels = {'Label one','Label two','Label three'};
  댓글 수: 2
John Wills
John Wills 2023년 12월 5일
Thanks Les! Really appreciated.
Les Beckham
Les Beckham 2023년 12월 5일
You are quite welcome.

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

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 12월 4일
Set the yticks using the values in y and modify the yticklabels of the right y-axis accordingly -
If the order of labels is expected to be reverse of that is displayed below, flip the variable str to reverse the order.
% create dummy data
y = [1,2,3];
x = [5,10,20];
err = 0.5*ones(size(y));
% draw plot
figure(1)
yyaxis right
errorbar(x,y,err,'.b','horizontal')
yticks(y)
str = {'Label one','Label two','Label three'};
yticklabels(str)
xlim([0 30])
ylim([0 4])
% hide Y1 axis
hax = gca;
hax.YAxis(1).Visible='off';
  댓글 수: 2
John Wills
John Wills 2023년 12월 5일
Thanks Dyuman - really appreciate the help. Best wishes
Dyuman Joshi
Dyuman Joshi 2023년 12월 5일
Glad to have helped!

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

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by