필터 지우기
필터 지우기

Assign multiple custom labels in one plot using a for loop

조회 수: 1 (최근 30일)
Florian
Florian 2020년 5월 10일
댓글: Florian 2020년 5월 10일
stats=[3.0000 26.7150 43.0300 53.6300;
4.0000 17.5450 33.4050 80.7700;
5.0000 32.3300 39.6800 109.7800;
15.0000 179.5300 179.5300 179.5300;
16.0000 21.5550 96.1550 156.43003]; % the row size of this matrix changes depending on previous calculations
facnrs=stats(:,1);
P10=stats(:,2);
P50=stats(:,3);
P90=stats(:,4);
neg=P50-P10;
pos=P90-P50;
% plot the results
errorbar(stats(:,1),P50,neg,pos,'o')
xsmin=min(stats(:,1))-1;
xsmax=max(stats(:,1))+1;
set(gca,'xlim',[xsmin xsmax])
set(gca,'ydir','reverse');
xlabel('Facies')
ylabel('Depth (m) from top for P10, P50 & P90')
ylh = get(gca,'ylabel');
ylp = get(ylh, 'Position');
set(ylh, 'Rotation',270, 'Position',ylp, 'VerticalAlignment','middle', 'HorizontalAlignment','center')
% label the number of occurrences for each facies on top of errorbars;
% start with counting the nr of occurrences per facies
for i=1:length(facnrs)
x=find(facies==facnrs(i));
noccur(i)=length(x);
end
noccur=noccur';
% compile string for number of occurrences
bartext=compose('n = %d',noccur);
for m=1:length(facnrs)
textXpos(m)=facnrs(m)-1;
end
textXpos=textXpos';
for j=1:length(facnrs)
textYpos(j)=P10(j)-10;
end
textYpos=textYpos';
% label the bars
for k = 1:length(facnrs)
txt = text(textXpos(k),textYpos(k),'$\stackrel{bartext(k)}{\downarrow}$','interpreter','latex','fontsize',20);
end
The above code gives me the plot inserted below. Instead of putting the actual strings for bartext(k) it shows bartext(k) as the string in each label. The reason is because k is not recognized as a step in the last for loop.
I modified the script and tried the following:
% label the bars
for k = 1:length(facnrs)
txt = text(textXpos(k),textYpos(k),'$\stackrel{bartext',(k),'}{\downarrow}$','interpreter','latex','fontsize',20);
end
However, then Matlab returns the following error:
"Error using text. Too many non-property/value arguments."
Does anybody have an idea how I can achieve the right labeling?

채택된 답변

Florian
Florian 2020년 5월 10일
Here is how I was able to solve it in the end (if anybody wants to do a similar display):
stats=[3.0000 26.7150 43.0300 53.6300;
4.0000 17.5450 33.4050 80.7700;
5.0000 32.3300 39.6800 109.7800;
15.0000 179.5300 179.5300 179.5300;
16.0000 21.5550 96.1550 156.43003]; % the row size of this matrix changes depending on previous calculations
facnrs=stats(:,1);
P10=stats(:,2);
P50=stats(:,3);
P90=stats(:,4);
neg=P50-P10;
pos=P90-P50;
% plot the results
errorbar(stats(:,1),P50,neg,pos,'o')
xsmin=min(stats(:,1))-1;
xsmax=max(stats(:,1))+1;
set(gca,'xlim',[xsmin xsmax])
set(gca,'ydir','reverse');
xlabel('Facies')
ylabel('Depth (m) from top for P10, P50 & P90')
ylh = get(gca,'ylabel');
ylp = get(ylh, 'Position');
set(ylh, 'Rotation',270, 'Position',ylp, 'VerticalAlignment','middle', 'HorizontalAlignment','center')
% label the number of occurrences for each facies on top of errorbars;
% start with counting the nr of occurrences per facies
for i=1:length(facnrs)
x=find(facies==facnrs(i));
noccur(i)=length(x);
end
noccur=noccur';
% compile string for number of occurrences
for k = 1:length(facnrs)
bartext(k) = compose('$\\stackrel{n = %d}{\\downarrow}$', noccur(k));
end
for m=1:length(facnrs)
textXpos(m)=facnrs(m)-0.5;
end
textXpos=textXpos';
for j=1:length(facnrs)
textYpos(j)=P10(j)-10;
end
textYpos=textYpos';
% label the bars
text(textXpos,textYpos,bartext,'interpreter','latex','fontsize',20);
Plot looks like this:
  댓글 수: 2
Star Strider
Star Strider 2020년 5월 10일
I deleted my Answer because you (erroneously) accepted yours.
When I attempt to run your code (both the original and in your Answer), I get:
Unrecognized function or variable 'facies'.
Error in <filename> (line ###)
x=find(facies==facnrs(i));
So all the necessary variables are not supplied in the code you posted.
Florian
Florian 2020년 5월 10일
Yes, you're right. Sorry for that.

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

추가 답변 (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