Changing title, line color, weight etc in errorbar figures

35 vues (au cours des 30 derniers jours)
Andrew
Andrew le 25 Juil 2014
Modifié(e) : dpb le 28 Juil 2014
Good day to all,
I have a 152x5 matrix called Seg (Figure 1).
My goal was to graph each row separately which I was able to achieve (Figure 2) with some success [I would have like for each row to have been a different color] using the following code
% code
plot(Seg(1:3,:).','--or','LineWidth',3,'MarkerEdgeColor','k','MarkerFaceColor','r','MarkerSize',10);
title('Frontal (Fp1,Fp2,F3) Activity over Segments')
xlabel('Segments')
ylabel('Microvolts')
legend('Fp1','Fp2','F3')
legend('Location','Northeast') %move legend to the upper left quadrant
end
I wanted Figure 2 but with error bars to all of these series in such a way that Row 1,2 and 3 of Seg [as seen in Figure 1] each had their respective Standard Error of the Mean (SEM). To the best of my knowledge the only way to achieve this is to use errorbar [please correct me if I'm wrong]. In order to accomplish this I computed the SEM using the following code
% code
%Calculate the standard error of the mean for every row in Seg
SEM_Seg=std(Seg,[],2)/sqrt(length(Seg));
end
Which created a 152x1 matrix. However in order to get errorbar [If not you get the error "Error using errorbar (line 77). X, Y and error bars must all be the same length to work"] I had to copy/paste the 1st column 5 times, the total number of columns in Seg. (Figure 3)
If I use the following code I get the graph seen in Figure 4
% code
errorbar(Seg(1:3,:),SEM_Seg(1:3,:));
end
The problem is that I want to be able to add axis labels, Chart Title, Change line color etc. How is it possible to change these Plot properties? I am able to do so when I use the plot(x) function but this same syntax does not function using errorbar. Perhaps there is an alternative.
I appreciate any criticism, opinions and/or help.
Thanks!

Réponses (2)

Robert Cumming
Robert Cumming le 25 Juil 2014
Modifié(e) : Robert Cumming le 25 Juil 2014
is the xlabel etc going on the wrong plot?
if so pass in the axes handle:
xlabel ( axH, 'x label' )
ylabel ( axH, 'y label' )
legend ( axH, 'my legend' )
edit
where axH is the handle to the axes you want to label.
e.g. create 4 figures
f1 = figure;
ax1 = axes ( 'parent', f1 );
plot ( ax1, rand(10), rand(10) );
f2 = figure;
ax2 = axes ( 'parent', f2 );
plot ( ax2, rand(10), rand(10) );
f3 = figure;
ax3 = axes ( 'parent', f3 );
plot ( ax3, rand(10), rand(10) );
f4 = figure;
ax4 = axes ( 'parent', f4 );
plot ( ax4, rand(10), rand(10) );
Now label axes 3:
xlabel ( ax3, 'label on plot 3' )
  2 commentaires
Andrew
Andrew le 25 Juil 2014
Good day Richard,
Thank you for your response. I'm trying to add axis handles onto Figure 4(see above). From my understanding this is what you are telling me to attempt
% code
errorbar(Seg(1:3,:),SEM_Seg2(1:3,:));
xlabel ( axH, 'x label' )
ylabel ( axH, 'y label' )
legend ( axH, 'my legend' )
end
However this code results in the following error " Undefined function or variable 'axH'. " Is there a script required for axH or is it defined somewhere?
Sincerely, Andrew
dpb
dpb le 28 Juil 2014
You've got to save (or retrieve) any handles you need/refer to...Matlab doesn't know who you mean by magic. At the time you've just executed errorbar, the axes handle you need can be obtained by
hAx=gca; % retrieve, save current axes handle
errorbar returns handles to the error series objects, not the axes, similarly as plot returns line handles.

Connectez-vous pour commenter.


dpb
dpb le 25 Juil 2014
My goal was to graph each row separately...
Given the default orientation of Matlab and plot and errorbar to plot data by column, if you wish to use the five values as the independent variable and the others as observation, it would be better to orient the array as 5 by nColumns instead.
Then
SEM=std(Seg)/sqrt(size(Seg,1));
and you'll have the right number to use. Also it appears that you're using the wrong length in the computation as above as length()==max(size()).
Also, if use this way the line colors then should cycle automagically just as the do for plot with an array as input. If you want additional properties, use the 'LineSpec' parameter that has the same options as those for plot
Save the handle from the call to have more direct access to the properties.
Labels and titles are independent of whether use plot or any of the other graphing routines--just call title, [x|y]label, text and friends as for any other.
  2 commentaires
Andrew
Andrew le 28 Juil 2014
dpb thank you very much for your helpful answer. You are correct my calculation for the SEM was incorrect. I have since changed it. For this I simply want to compare those 5 values between themselves. At the current time I do not have an interest in comparing the rows, simply the 5 columns.
I have tried the code you graciously provided me for the SEM which is the following
% code
SEM=std(Seg)/sqrt(size(Seg,1));
end
Although this does give me the proper SEM it does have one major flaw : It only reports for 1 column and not the entire matrix (See Figure 1)
This would mean that there would be 152 different SEM variables that have to be created (not very compact code). I have re-iterated what I want in greater detail [Figure 2]. Furthermore I am uncertain how I would refer to this in errorbar since you need to specify one variable/matrix per unit of data. For example
The end result should give me a matrix that is 152x5 (Same size as Seg). I tried doing this but it doesn't work
if true
% code
SEM=std(Seg)/sqrt(size(Seg,1:number_of_segments)); %number_of_segments is = 5 in this example
end
Since I do not want 152 different graphs, I attempted to graph more than one per figure using this code.
I have transposed Seg (152x5) and called it Seg2 (5x152). The only thing that changes in errorbar code will be a slight reference difference to this code
if true
% code
errorbar(Seg2(:,1:3), *SEM33(:,1:3));*
title('Frontal (Fp1,Fp2,F3) Activity over Segments')
xlabel('Segments')
ylabel('Microvolts')
legend('Fp1','Fp2','F3') %Fp1 is row1, Fp2 is row2, F3 is row 3
legend('Location','Northeast') %move legend to the upper left quadrant
end
Notice that using your code the SEM needed for the code above would need to be
if true
% code
errorbar(Seg2(:,1:3), *SEM1,SEM2,SEM3(:,1:3));*
end
You're solution for the graphs worked perfect (as can be seen in the code above. Thank you very much, if I have no expressed myself with sufficient clarity please let me know I can post more pictures etc. I'm a fairly motivated beginner.
Sincerely, Andrew
dpb
dpb le 28 Juil 2014
Modifié(e) : dpb le 28 Juil 2014
SEM=std(Seg)/sqrt(size(Seg,1));
...Although this does give me the proper SEM it ... only reports for 1 column and not the entire matrix...
Which is the primary reason I suggested you should reorient the data as 5xN rather than Nx5 since you're working by row and Matlab by default is column-major. Plot functions assume arrays by column and the default for functions like std() is by column, also. If you retain the other order, then you must tell std() over which dimension to operate--
SEM=std(Seg,2)/sqrt(size(Seg,2));

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by