Legend colors change when I have two y axes

11 visualizaciones (últimos 30 días)
Eetu
Eetu el 16 de Jul. de 2024
Comentada: Eetu el 17 de Jul. de 2024
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:
And after 2 runs:
after many runs:
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.

Respuesta aceptada

VBBV
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
Eetu
Eetu el 17 de Jul. de 2024
Editada: Eetu el 17 de Jul. de 2024
Thanks @VBBV for your response. Your solution seemed good and worked for first 4 data set runs. After that colors remained as they should but the first dashed line began to have different patterns. I'll add figures:
This is legend after 6 runs. Colors are right but first one has those markers. And the plot looks like:
First markers were circles (red) and after next run they were triangles (blue). Could you help with this also? Thanks!
Eetu
Eetu el 17 de Jul. de 2024
Oh, I fixed it. Just had to type
h1 = plot(Time_h,density, 'color', '#000000','LineStyle',':','Marker','none');
Anyway, Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by