# How can I subplot 3 graphs with errors barr and different characteristics?

12 views (last 30 days)
Jonathan Bijman on 8 Jan 2020
Commented: Allen on 8 Jan 2020
Dear community, I hope you can help me with this issue:
I need to subplot 3 graphs. Each graph has 2 variables and I need that appears with the same x variable (in this case is "time"). T_A8_C is mean of measurements and T_A8_D is mean of other measurements, and so on. I need 3 subplots: first: T_A8_C with T_A8_D, second: T_C5_C with T-C5_D and third: T_E1_C with T_EA_D
I have this commands:
subplot(2,1,1)
x = linspace(0, 130, numel(T_A8_C));
X = [x;x].';
YA8 = [T_A8_C(:) T_A8_D(:)];
plot(X,YA8)
subplot(2,1,2)
YC5 = [T_C5_C(:) T_C5_D(:)];
plot(X,YC5)
subplot(2,1,3)
YE1 = [T_E1_C(:) T_E1_D(:)];
plot(X,YE1)
err=[T_desvA8_C(:) T_desvC5_C(:) T_desvE1_C(:) T_desvA8_D(:) T_desvC5_D(:) T_desvE1_D(:)];
errorbar(X,Y,err)
hEB=errorbar(X,Y,err);
hEB(1).Color=('red');
hEB(2).Color=('blue');
hEB(3).Color=('black');
hEB(4).Color=('red');
hEB(5).Color=('blue');
hEB(6).Color=('black');
hEB(1).LineStyle=('-');
hEB(2).LineStyle=('-');
hEB(3).LineStyle=('-');
hEB(4).LineStyle=('-.');
hEB(5).LineStyle=('-.');
hEB(6).LineStyle=('-.');
hEB(1).LineWidth=(2);
hEB(2).LineWidth=(2);
hEB(3).LineWidth=(2);
xticks(0:5:130);
title('Cell Temperature Plot')
xlabel('Time (Days)')
ylabel('Cell Temperature (°C)')
legend('A8 Clean','C5 Clean','E1 Clean','A8 Dirty','C5 Dirty','E1 Dirty');
And this message error appears:
Error using subplot (line 332)
Index exceeds number of subplots.
Error in Prueba_TCell (line 79)
subplot(2,1,3)
And doesn´t show the title, labels of each axis, the characteristic width and linestyle and legend.
Thank you so much!

Allen on 8 Jan 2020
Replace the lines containing the subplot functions with the following:
...
subplot(3,1,1)
...
subplot(3,1,2)
...
subplot(3,1,3)
...
Essentially the subplot functions works by specifying the number of rows (m) and columns (n) of plots to generate in the target figure, followed by the instance (i) or location that you currently want to use for plotting.
e.g. - subplot(3,1,1) states that the arrangement of plots is to be 3 rows by 1 column and you are currently targeting the 1st instance (i.e. the top-left subplot location).

Jonathan Bijman on 8 Jan 2020
Thanks a lot @Allen! Now I get the subplots. But I can´t get that appears the error bar and the other characteristics like width, linestyle,etc. I don´t know how can I fix that.
Do you know something about that?
Thank u again =)

#### 1 Comment

Allen on 8 Jan 2020
You need to use the title, label, and legend functions after each use of the plot function. Additionally, you need to set the line characteristics after each plot() as well.
...
subplot(3,1,1)
plot(...)
% Enter errorbar and line charateristics for plot #1
title('...')
xlabel('...')
xlabel('...')
lengend('...')
...
subplot(3,1,2)
plot(...)
% Enter errorbar and line charateristics for plot #2
title('...')
xlabel('...')
xlabel('...')
lengend('...')
...
subplot(3,1,3)
plot(...)
% Enter errorbar and line charateristics for plot #3
title('...')
xlabel('...')
xlabel('...')
lengend('...')
...
You can also set most of the line charateristics directly when using plot(). See the help page for this function. https://www.mathworks.com/help/matlab/ref/plot.html

R2019b

### Community Treasure Hunt

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

Start Hunting!