10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ludovica Varriale am 9 Okt. 2023
Kommentiert: Dyuman Joshi am 10 Okt. 2023
Akzeptierte Antwort: Dyuman Joshi
In MATLAB Online öffnen
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 Kommentare 2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Dyuman Joshi am 9 Okt. 2023
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2030959-why-colororder-function-does-not-work-with-ylim#comment_2915704
⋮
There is an undefined variable in your code, see above.
Also, specify which plots you want to draw with the left y-axis and which ones with the right y-axis.
Ludovica Varriale am 9 Okt. 2023
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2030959-why-colororder-function-does-not-work-with-ylim#comment_2915729
Hi,
thank you very much for you reply. This should be the correct code (sorry for the mistake).
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];
errFru1= [0.13,0.57,0.41,0.14,0.06];
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');
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
I would need on the y axis left: Glu1 and Fru1 with the respective errorbars, on the right: Et1 and Gly1 with the respective errorbars.
Thank you !
Dyuman Joshi am 9 Okt. 2023
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2030959-why-colororder-function-does-not-work-with-ylim#comment_2915799
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 am 9 Okt. 2023
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2030959-why-colororder-function-does-not-work-with-ylim#comment_2915814
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.
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Akzeptierte Antwort
Dyuman Joshi am 9 Okt. 2023
In MATLAB Online öffnen
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 Kommentare Keine anzeigenKeine ausblenden
Keine anzeigenKeine ausblenden
Ludovica Varriale am 10 Okt. 2023
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2030959-why-colororder-function-does-not-work-with-ylim#comment_2916654
Verschoben: Dyuman Joshi am 10 Okt. 2023
Dear Dyuman Joshi,
thank you a lot for your help! Now the script works as I want!!!
Best regards,
Ludovica Varriale
Dyuman Joshi am 10 Okt. 2023
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2030959-why-colororder-function-does-not-work-with-ylim#comment_2916659
You are welcome!
If my answer solved your problem, please consider accepting the answer.
Melden Sie sich an, um zu kommentieren.
Weitere Antworten (0)
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
MATLABLanguage FundamentalsData TypesDates and TimeCalendar
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung