MATLAB Answers

Adding legend to reference line in a plot

4 views (last 30 days)
MiauMiau
MiauMiau on 16 Aug 2017
Hi,
As you can see below, I am plotting bar graphs. There is additionally also a red reference line in the plot. Now I want to add a legend, however not as the default for the bar plots, but for the reference line. How can I do that? Additionally, how can I have the reference line going over the entire range of the x axis in this specific case? Many thanks and here the code:
model_series = [65,50,0; 0,0,60];
model_error = [0, 0,0;0,0,0];
h = bar(model_series);
set(h,'BarWidth',1); % The bars will now touch each other
set(gca,'YGrid','on')
set(gca,'GridLineStyle','-')
set(gca,'YLim',[0 100])
title(['SVM Classification'])
hline = refline([0 63]);
hline.Color = 'r';
set(gca,'xtick',[1 2 3 4])
set(gca,'xticklabel',{'Condition 1','Condition 2'})
set(get(gca,'YLabel'),'String','Classification Accuracy')
hold on;
numgroups = size(model_series, 1);
numbars = size(model_series, 2);
groupwidth = min(0.8, numbars/(numbars+1.5));
for i = 1:numbars
% Based on barweb.m by Bolu Ajiboye from MATLAB File Exchange
x = (1:numgroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*numbars); % Aligning error bar with individual bar
errorbar(x, model_series(:,i), model_error(:,i), 'k', 'linestyle', 'none');
end

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!