Plot line colors and legend colors don't match, & error bars don't show the points in the plot

3 views (last 30 days)
Please see attached the files. When I run my code with shaded error bars, I got the output where the legend color does not match plot line colors. On the other hand, if I want to plot the traditional error bars, I see that one plot does not show the points. The respective output figures are attached for your reference. I am not sure where I made a mistake. Any help will be very much appreciated!

Accepted Answer

Mathieu NOE
Mathieu NOE on 6 Dec 2021
hello again my friend !
this is your code a bit tweaked
h1 = plot(x*100, avey1, '-ok',... : forcing the line color to black
h3 = plot(x*100, avey3, '-dr', ... : forcing the line color to red
e1 = errorbar(x*100,avey1,sey1, 'HandleVisibility','off') : set the “errorbar” handle to be invisible. This will prevent the “legend” from updating with the new “errorbar” handle:
this is now the result :
code
%This code finds the excel file, calculates the average, standard
%deviation and standard error of y values, and plots the graph by usingg
%subplot function and shaded errorbars. The shaded error bars are different
%representation of the traditional error bars
clc
clear vars
clear all
%% define path and variables
addpath(genpath('C:\Users\anusu\Documents\MATLAB\github_repo')) %for geeting shaded errorbar (what matlab does not), see FEX:
% %https://uk.mathworks.com/matlabcentral/fileexchange/26311-raacampbell-shadederrorbar
fileDir = 'C:\Users\anusu\Downloads\Tibetan _syllabic files tone\Tibetan _syllabic files tone\bi-syllabic_tibetan';
% fileDir = pwd;
S = dir(fullfile(fileDir,'output3.xlsx')); % get data file in directory
sheet = 'final graph'; % data must be retrieved from sheet named final graph
%% reading excel sheet and finding index and blocks
data = xlsread(fullfile(fileDir, S.name),sheet);
[m,n] = size(data);
ind_nan = find(isnan(data(1,:)));
nb_blocks = numel(ind_nan)+1;
ind_start = [1 ind_nan+1];
ind_stop = [ind_nan-1 n];
%% finding data for each block
for ci = 1:nb_blocks
x = data(1,ind_start(ci):ind_stop(ci));
y(:,:,ci) = data(2:end,ind_start(ci):ind_stop(ci));
end
%% finding y for each block
newy1 = y(:,:,1); %y for first word_CV1
newy2 = y(:,:,2); %y for first word_CV2
newy3 = y(:,:,3); %y for second word_CV1
newy4 = y(:,:,4); %y for second word_CV2
%% finding average, standard deviation, and standard error...
%% of y for each block
avey1= mean(newy1); % finds average of y1
sdy1= std(newy1); % finds standard deviation of y1
sey1= sdy1/9; % In the whole data, there are 9 speakers, but will change accordingly
avey2= mean(newy2);
sdy2= std(newy2);
sey2= sdy2/9;
avey3= mean(newy3);
sdy3= std(newy3);
sey3= sdy3/9;
avey4= mean(newy4);
sdy4= std(newy4);
sey4= sdy4/9;
%% plot
filename_fig = S.name;
ind = strfind(filename_fig,'.');
filename_fig = filename_fig(1:ind-1);
f= figure();
t = sgtitle('thangpo'); % the name of the title needs to change accordinly
t.FontSize = 14;
t.FontWeight = 'bold';
ax(1)=subplot(1,2,1);
h1 = plot(x*100, avey1, '-ok', 'MarkerSize',6,'MarkerEdgeColor','k',...
'MarkerFaceColor','k');
e1 = errorbar(x*100,avey1,sey1, 'HandleVisibility','off'); %if want the tranditional error bars,
%uncomment these three lines (e1, e1.Color, e1.Capsize) for all the below
%four cases
e1.Color = 'black';
e1.CapSize = 10;
shadedErrorBar(x*100, avey1, sey1, 'lineprops', '-k');
hold on
h3 = plot(x*100, avey3, '-dr', 'MarkerSize',6,'MarkerEdgeColor','r',...
'MarkerFaceColor','r');
e3 = errorbar(x*100,avey3,sey3, 'HandleVisibility','off');
e3.Color = 'red';
e3.CapSize = 10;
shadedErrorBar(x*100, avey3, sey3, 'lineprops', '-r');
axis padded
grid;
xlabel('Time points (%)');
ylabel('f0 (Hz)');
linkaxes(ax,'y')
hold off
title('cv1')
ax(2) =subplot(1,2,2);
h2 = plot(x*100, avey2, '-ok', 'MarkerSize',6,'MarkerEdgeColor','k',...
'MarkerFaceColor','k');
e2 = errorbar(x*100,avey2,sey2, 'HandleVisibility','off');
e2.Color = 'black';
e2.CapSize = 10;
shadedErrorBar(x*100, avey2, sey2, 'lineprops', '-k');
hold on
h4 = plot(x*100, avey4, '-dr', 'MarkerSize',6,'MarkerEdgeColor','r',...
'MarkerFaceColor','r');
e4 = errorbar(x*100,avey4,sey4, 'HandleVisibility','off');
e4.Color = 'red';
e4.CapSize = 10;
shadedErrorBar(x*100, avey4, sey4, 'lineprops', '-r');
axis padded
grid;
xlabel('Time points (%)');
ylabel('f0 (Hz)');
legstr = {'health','one'};
hold off
title('cv2')
set(gcf,'Position',[288 342 860 420]); % for 2 panels
%ll= legend([h2, h4], legstr,'Location','south','Orientation','horizontal', 'NumColumns',1);
%set(ll,'Position',[0.932 0.79 0.02691 0.0323]); %this is for shaded error bars
ll= legend(legstr,'Location','south','Orientation','horizontal', 'NumColumns',1);
set(ll,'Position',[0.932 0.79 0.02691 0.0323]); % this is for traditional error bars
linkaxes(ax,'y')
print(filename_fig, '-dpng')
  4 Comments
Anu
Anu on 13 Dec 2021
Edited: Anu on 13 Dec 2021
Thanks, so much, @Mathieu NOE! It worked perfectly the way I wanted. I am learning a lot from you. Really appreciate it.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!