Clear Filters
Clear Filters

How to add legends?

2 views (last 30 days)
Yeasir Mohammad Akib
Yeasir Mohammad Akib on 6 Jun 2022
Commented: Star Strider on 6 Jun 2022
How to add Legends to this code according to the colours? I am getting different legend symbol for this code.
r1=[19.38 65.3 15.32;16.66 62.13 21.21;18.21 58.31 23.49;19.11 59.95 20.94;16.53 67.27 16.19;19.41 63.87 16.72];
Std= [1.2141 3.0580 3.0398]
Mean=[18.2167 62.8050 18.9783]
Bar=bar(Mean,0.4, 'FaceColor', 'flat');
Bar.CData(1,:)=[0.8500 0.3250 0.0980]
Bar.CData(2,:)= [1 0 0]
Bar.CData(3,:)= [0 1 0]
set(gca,'FontSize',12,'XTick',[1 2 3 ],'XTickLabel',{'Empty space','Higher Diameter','Lower Diameter' });
xlabel('Percentage of spcace occupied ')
ylabel('Mean')
errbar=[Std]
yd=[Mean]
hold on
for k1 = 1:3
errorbar([1:3]+.01*(k1-2), yd(k1,:), errbar(k1,:), '.k', 'LineWidth',1)
end
hold off
Looking for some cordial help. Thank you.

Accepted Answer

Star Strider
Star Strider on 6 Jun 2022
Try this —
r1=[19.38 65.3 15.32;16.66 62.13 21.21;18.21 58.31 23.49;19.11 59.95 20.94;16.53 67.27 16.19;19.41 63.87 16.72];
Std= [1.2141 3.0580 3.0398];
Mean=[18.2167 62.8050 18.9783];
Bar=bar(Mean,0.4, 'FaceColor', 'flat');
Bar.CData(1,:)=[0.8500 0.3250 0.0980];
Bar.CData(2,:)= [1 0 0];
Bar.CData(3,:)= [0 1 0];
set(gca,'FontSize',12,'XTick',[1 2 3 ],'XTickLabel',{'Empty space','Higher Diameter','Lower Diameter' });
xlabel('Percentage of spcace occupied ')
ylabel('Mean')
errbar=[Std];
yd=[Mean];
hold on
eb = errorbar(Bar.XEndPoints, Bar.YEndPoints, errbar, '.k', 'LineWidth',1, 'MarkerSize',0.1, 'DisplayName','Error Bars');
hb1 = bar(NaN,NaN, 'FaceColor',[0.8500 0.3250 0.0980], 'DisplayName','One');
hb2 = bar(NaN,NaN,'FaceColor',[1 0 0], 'DisplayName','Two');
hb3 = bar(NaN,NaN,'FaceColor',[0 1 0], 'DisplayName','Three');
hold off
legend([hb1,hb2,hb3,eb], 'Location','best')
.
  2 Comments
Yeasir Mohammad Akib
Yeasir Mohammad Akib on 6 Jun 2022
Thanks a lot Dr. Strider. It helps!
Star Strider
Star Strider on 6 Jun 2022
As always, my pleasure!

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!