Assign multiple custom labels in one plot using a for loop

2 vues (au cours des 30 derniers jours)
Florian
Florian le 10 Mai 2020
Commenté : Florian le 10 Mai 2020
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?

Réponse acceptée

Florian
Florian le 10 Mai 2020
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 commentaires
Star Strider
Star Strider le 10 Mai 2020
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 le 10 Mai 2020
Yes, you're right. Sorry for that.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by