Clear Filters
Clear Filters

Why colororder function does not work with ylim?

10 views (last 30 days)
Hello everybody,
I have a problem with my Matlab code. I would like to make a 2D plot with two y-axis with different axis limits. The created a proper function for the colors and I used them using the colororder function. However, it does not work with I wrote ylim function.
Do you know why?
I hope you can help me :)
Here my code:
figure
hold on
fontsize(12,"points");
x=[0,6,24,48,72];
xlabel('Time [h]');
yyaxis left
ylabel('Sugars [g/L]');
Glu1=[20.68,23.35,5.41,0.54,0];
errGlu1=[0.77,2.07,0.69,0.12,0];
Fru1=[5.19,5.55,2.07,0.93,0.78];
ylim([0 25])
yyaxis right
ylabel('Products [g/L]');
Et1=[0,0.16,7.51,9.37,8.20];
errEt1=[0,0.02,0.34,0.71,0.42];
Gly1=[0,0.2,0.3,0.36,0.37];
errGly1=[0,0.01,0.03,0.03,0.04];
ylim([0 12]);
plot(x,Glu1,'^-','LineWidth',2);
plot(x,Fru1,'^-','LineWidth',2);
plot(x,Et1,'v-', 'LineWidth',2);
plot(x,Gly1,'v-','LineWidth',2);
errorbar(x,Glu1,errGlu1,'LineStyle','none','Color','black');
errorbar(x,Fru1,errFru1,'LineStyle','none','Color','black');
Unrecognized function or variable 'errFru1'.
errorbar(x,Et1,errEt1,'LineStyle','none','Color','black');
errorbar(x,Gly1,errGly1,'LineStyle','none','Color','black');
newcolors = [0.83 0.14 0.14
1.00 0.54 0.00
0.47 0.25 0.80
0.25 0.80 0.54];
colororder(newcolors)
hold off
  4 Comments
Dyuman Joshi
Dyuman Joshi on 9 Oct 2023
Do you want to colors the individual plots according to the newcolors array?
If not, please specify the output that you want to obtain.
Ludovica Varriale
Ludovica Varriale on 9 Oct 2023
I would like to have a plot with 4 lines with the different colors specified in the array "newcolors".
As you can see, the problem is that the beginning of the red line is not visible because of the limits I guess.
es.

Sign in to comment.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 9 Oct 2023
Note that in using ylim([0 25]) for the left yaxis, the upper cap of an errorbar data point is not visible. Adjust according to requirements.
%Data
x=[0,6,24,48,72];
Glu1=[20.68,23.35,5.41,0.54,0];
errGlu1=[0.77,2.07,0.69,0.12,0];
Fru1=[5.19,5.55,2.07,0.93,0.78];
errFru1= [0.13,0.57,0.41,0.14,0.06];
Et1=[0,0.16,7.51,9.37,8.20];
errEt1=[0,0.02,0.34,0.71,0.42];
Gly1=[0,0.2,0.3,0.36,0.37];
errGly1=[0,0.01,0.03,0.03,0.04];
figure
hold on
xlabel('Time [h]');
fontsize(12,"points");
yyaxis left
ylabel('Sugars [g/L]');
plot(x,Glu1,'^-','LineWidth',2);
plot(x,Fru1,'^-','LineWidth',2);
errorbar(x,Glu1,errGlu1,'LineStyle','none','Color','black');
errorbar(x,Fru1,errFru1,'LineStyle','none','Color','black');
ylim([0 25])
%Colorder for the left side
newcolors1 = [0.83 0.14 0.14
1.00 0.54 0.00];
colororder(newcolors1)
yyaxis right
ylabel('Products [g/L]');
plot(x,Et1,'v-', 'LineWidth',2);
plot(x,Gly1,'v-','LineWidth',2);
errorbar(x,Et1,errEt1,'LineStyle','none','Color','black');
errorbar(x,Gly1,errGly1,'LineStyle','none','Color','black');
ylim([0 12]);
%Colorder for the right side
newcolors2 = [0.47 0.25 0.80
0.25 0.80 0.54];
colororder(newcolors2)
hold off
Also, you can also specify the color of a line plot individually without needing to use colororder(), like you have done for the errorbars, as follows -
%Data
x=[0,6,24,48,72];
Glu1=[20.68,23.35,5.41,0.54,0];
errGlu1=[0.77,2.07,0.69,0.12,0];
Fru1=[5.19,5.55,2.07,0.93,0.78];
errFru1= [0.13,0.57,0.41,0.14,0.06];
Et1=[0,0.16,7.51,9.37,8.20];
errEt1=[0,0.02,0.34,0.71,0.42];
Gly1=[0,0.2,0.3,0.36,0.37];
errGly1=[0,0.01,0.03,0.03,0.04];
%Colors to be used for line plots
c = [0.83 0.14 0.14
1.00 0.54 0.00
0.47 0.25 0.80
0.25 0.80 0.54];
figure
hold on
xlabel('Time [h]');
fontsize(12,"points");
yyaxis left
ylabel('Sugars [g/L]');
plot(x,Glu1,'^-','LineWidth',2,'Color',c(1,:));
plot(x,Fru1,'^-','LineWidth',2,'Color',c(2,:));
errorbar(x,Glu1,errGlu1,'LineStyle','none','Color','black');
errorbar(x,Fru1,errFru1,'LineStyle','none','Color','black');
ylim([0 25])
yyaxis right
ylabel('Products [g/L]');
plot(x,Et1,'v-', 'LineWidth',2,'Color',c(3,:));
plot(x,Gly1,'v-','LineWidth',2,'Color',c(4,:));
errorbar(x,Et1,errEt1,'LineStyle','none','Color','black');
errorbar(x,Gly1,errGly1,'LineStyle','none','Color','black');
ylim([0 12]);
hold off
  2 Comments
Ludovica Varriale
Ludovica Varriale on 10 Oct 2023
Moved: Dyuman Joshi on 10 Oct 2023
Dear Dyuman Joshi,
thank you a lot for your help! Now the script works as I want!!!
Best regards,
Ludovica Varriale
Dyuman Joshi
Dyuman Joshi on 10 Oct 2023
You are welcome!
If my answer solved your problem, please consider accepting the answer.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!