Legend colors change when I have two y axes
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I am having difficulties with making figure. Here is the thing:
I have 10 different data sets. 2nd starts at the timestamp 1st end etc. Each data sets are saved and have similarly named variablses. I want data "density" to be on the left y axis and 7 measurements to be on the right y axis. The problem is with legend. After 1st measurement everything is ok. But when I run code again with another dataset, legend colors and linestyle will change. I'll add both code and pictures of legends in several times.
Legend after 1st measurement. This is how it should look like:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1735043/image.png)
And after 2 runs:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1735048/image.png)
after many runs:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1735053/image.png)
The program is using whole different legends.
So, here is the code. (before I had 8 measurements, but now I only need 7)
colors=["#028A0F","#1D57A9","#3DCBC8","#9C58A1","#F0D04D","#CD0027","#EF9F26","#B2B3B5","#FA9DAC","#98FF98","#67032F","#010101"];
fig1=figure(1);
hold on
yyaxis left
plot(Time_h,density, 'color', '#000000','LineStyle',':');
axLeft=gca;
axLeft.YColor = "#000000";
axLeft.XLim=[0,350];
axLeft.YLim=[0,1.5];
ylabel('density');
yyaxis right
plot(Time_h,Measure_1, 'color', colors(1),'LineStyle','-','Marker','none');
plot(Time_h,Measure_2, 'color', colors(2),'LineStyle','-','Marker','none');
plot(Time_h,Measure_3, 'color', colors(3),'LineStyle','-','Marker','none');
plot(Time_h,Measure_4, 'color', colors(4),'LineStyle','-','Marker','none');
plot(Time_h,Measure_5, 'color', colors(5),'LineStyle','-','Marker','none');
plot(Time_h,Measure_6, 'color', colors(6),'LineStyle','-','Marker','none');
plot(Time_h,Measure_7, 'color', colors(7),'LineStyle','-','Marker','none');
ylabel('')
axRight=gca;
axRight.YLim=[0,1];
axRight.YColor = "#000000";
lgnd=legend('density','Measurement 1','Measurement 2','Measurement 3','Measurement 4','Measurement 5','Measurement 6','Measurement 7');
xlabel('Time_ (h)');
I upload always the data and then run the code and then repeat. I don't close the picture between these. The final picture is then saved.
So thanks in advance and I can describe problem more if nessessary.
0 comentarios
Respuesta aceptada
VBBV
el 16 de Jul. de 2024
colors=["#028A0F","#1D57A9","#3DCBC8","#9C58A1","#F0D04D","#CD0027","#EF9F26","#B2B3B5","#FA9DAC","#98FF98","#67032F","#010101"];
fig1=figure(1);
hold on
yyaxis left
h1 = plot(Time_h,density, 'color', '#000000','LineStyle',':');
axLeft=gca;
axLeft.YColor = "#000000";
axLeft.XLim=[0,350];
axLeft.YLim=[0,1.5];
ylabel('density');
yyaxis right
h2 = plot(Time_h,Measure_1, 'color', colors(1),'LineStyle','-','Marker','none');
h3 = plot(Time_h,Measure_2, 'color', colors(2),'LineStyle','-','Marker','none');
h4 = plot(Time_h,Measure_3, 'color', colors(3),'LineStyle','-','Marker','none');
h5 = plot(Time_h,Measure_4, 'color', colors(4),'LineStyle','-','Marker','none');
h6 = plot(Time_h,Measure_5, 'color', colors(5),'LineStyle','-','Marker','none');
h7 = plot(Time_h,Measure_6, 'color', colors(6),'LineStyle','-','Marker','none');
h8 = plot(Time_h,Measure_7, 'color', colors(7),'LineStyle','-','Marker','none');
ylabel('')
axRight=gca;
axRight.YLim=[0,1];
axRight.YColor = "#000000";
lgnd=legend([h1(1),h2(1),h3(1),h4(1),h5(1),h6(1),h7(1),h8(1)],{'density','Measurement 1','Measurement 2','Measurement 3','Measurement 4','Measurement 5','Measurement 6','Measurement 7'});
xlabel('Time_ (h)');
3 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!