How to combine 2 graphs in one legend entry?

35 visualizaciones (últimos 30 días)
EmirBeg
EmirBeg el 18 de Jul. de 2021
Editada: Image Analyst el 18 de Jul. de 2021
Hello, i have 6 arrays of data that i want to plot but each 2 are combined. I already changed the colour of each plots so they fit together in my figure but now i have the problem that the legend doesn't works how i want to. The colours don't match.
Here's my code.
I want
plot(t_1,CH0_1,'r');
title('Measurement');
hold on;
plot(t_2,CH0_2,'r');
hold on;
% T1 and T2 combined together
plot(t_8,CH0_8,'m');
hold on;
plot(t_7,CH0_7,'m');
hold on;
% T7 and T8 combined together
plot(t_10,CH0_10,'b');
hold on;
plot(t_11,CH0_11,'b');
% T11 and T12 combined together
legend('Data1','Data2','Data3'); % Legend for all plots
Currently it looks like this. I want a legend where Data1 is red, Data2 magenta and Data 3 blue.
Can anyone help me here?

Respuesta aceptada

Image Analyst
Image Analyst el 18 de Jul. de 2021
Editada: Image Analyst el 18 de Jul. de 2021
Try it this way, where you pass only the handles of some of the plots into legend(). Plus you don't need to call hold on over and over again. Once it's called, it's set until you call hold off or clear the axes.
%=======================================================
% Create sample data
numPoints = 10;
t_1 = 1:numPoints;
CH0_1 = rand(1, numPoints);
t_2 = 1:numPoints;
CH0_2 = rand(1, numPoints);
t_7 = 1:numPoints;
CH0_7 = rand(1, numPoints);
t_8 = 1:numPoints;
CH0_8 = rand(1, numPoints);
t_10 = 1:numPoints;
CH0_10 = rand(1, numPoints);
t_11 = 1:numPoints;
CH0_11 = rand(1, numPoints);
%=======================================================
% Now plot the data.
% Plot T1 and T2 combined together
h1 = plot(t_1,CH0_1,'r', 'LineWidth', 3);
fontSize = 16;
title('Measurement of CHO vs. t', 'FontSize', fontSize);
xlabel('t', 'FontSize', fontSize);
ylabel('CHO', 'FontSize', fontSize);
hold on;
plot(t_2,CH0_2,'r', 'LineWidth', 3);
% Plot T7 and T8 combined together
h2 = plot(t_7,CH0_7,'m', 'LineWidth', 3);
plot(t_8,CH0_8,'m', 'LineWidth', 3);
% Plot T10 and T11 combined together
h3 = plot(t_10,CH0_10,'b', 'LineWidth', 3);
plot(t_11,CH0_11,'b', 'LineWidth', 3);
% Show legend
legend([h1,h2,h3], 'Data1','Data2','Data3'); % Legend for all plots
grid on;

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by