change the colors of legent in 2D-plot

1 view (last 30 days)
SIEF ADDEEN
SIEF ADDEEN on 27 Feb 2024
Edited: Voss on 27 Feb 2024
i need to change colors of curves as the same of the vurves up as shown in the attatchment
this is the code that i was used:
hold on
ylim([10 90]);
yticks(10:10:90);
plot (A,Bs1,'LineWidth',1)
plot (A,Bs2,'LineWidth',1)
plot (A,Bs3,'LineWidth',1)
plot (A,Bs4,'LineWidth',1)
plot (A,Bs5,'LineWidth',1)
plot (A,Bs6,'LineWidth',1)
plot (A,Bs7,'LineWidth',1)
xlabel("Crank angle (°)");
ylabel("Pressure (bar)");
xticks(-15:5:35);
yyaxis right
ylim([-20 300]);
yticks(-20:20:300);
plot (A1,Cs1,'K','LineWidth',1)
plot (A1,Cs2,'r','LineWidth',1)
plot (A1,Cs3,'b','LineWidth',1)
plot (A1,Cs4,'.','LineWidth',1)
plot (A1,Cs5,'G','LineWidth',1)
plot (A1,Cs6,'LineWidth',1)
plot (A1,Cs7,'LineWidth',1)
ylabel("Heat release rate (J/°CA)");
legend('40-D','B30','D60B30E10','DBEM10','DBEM15','40-DBME15','D60B30M10');
grid on
title('160 Nm');

Answers (2)

Voss
Voss on 27 Feb 2024
Edited: Voss on 27 Feb 2024
In your plot calls, use the LineSpec argument (see this) or 'Color',c input argument pair (see this).
Examples:
figure
hold on
plot(1:10,'r') % red color, using LineSpec with color short-name
plot(2:11,'Color','k') % black color, using 'Color',c property/value pair with color short-name
plot(3:12,'Color',[0 1 0]) % green color, using 'Color',c property/value pair with RGB notation specifying the color
More examples:
figure
hold on
plot(1:10,'-s','Color',[1 0.5 0]) % solid orange line with square marker
% orange has no short-name, so it can't be included in LineSpec;
% you must use 'Color',c property/value pair for specifying this color
plot(2:11,'--r') % dashed red line
plot(3:12,'-gd') % solid green line with diamond marker

Hassaan
Hassaan on 27 Feb 2024
Edited: Hassaan on 27 Feb 2024
@SIEF ADDEEN To what I understand your question:
% Dummy data
A = linspace(-15, 35, 100); % Common x-axis for Bs series
A1 = linspace(-15, 35, 100); % Common x-axis for Cs series
% Generate linear data for Bs series
Bs1 = linspace(10, 90, 100);
Bs2 = linspace(20, 80, 100);
Bs3 = linspace(30, 70, 100);
Bs4 = linspace(40, 60, 100);
Bs5 = linspace(50, 50, 100);
Bs6 = linspace(60, 40, 100);
Bs7 = linspace(70, 30, 100);
% Generate sine wave data for Cs series
Cs1 = 50 + 20*sin(2*pi*0.05*A1);
Cs2 = 60 + 15*sin(2*pi*0.05*A1 + 1);
Cs3 = 70 + 10*sin(2*pi*0.05*A1 + 2);
Cs4 = 80 + 5*sin(2*pi*0.05*A1 + 3);
Cs5 = 90 + 25*sin(2*pi*0.05*A1 + 4);
Cs6 = 100 + 20*sin(2*pi*0.05*A1 + 5);
Cs7 = 110 + 15*sin(2*pi*0.05*A1 + 6);
% Plotting commands as previously described
hold on
ylim([10 90]);
yticks(10:10:90);
plot(A, Bs1, 'k', 'LineWidth', 1); % Black
plot(A, Bs2, 'r', 'LineWidth', 1); % Red
plot(A, Bs3, 'b', 'LineWidth', 1); % Blue
plot(A, Bs4, 'm', 'LineWidth', 1); % Magenta
plot(A, Bs5, 'g', 'LineWidth', 1); % Green
plot(A, Bs6, 'c', 'LineWidth', 1); % Cyan
plot(A, Bs7, 'y', 'LineWidth', 1); % Yellow
xlabel("Crank angle (°)");
ylabel("Pressure (bar)");
xticks(-15:5:35);
yyaxis right
ylim([-20 300]);
yticks(-20:20:300);
plot(A1, Cs1, 'k', 'LineWidth', 1); % Black
plot(A1, Cs2, 'r', 'LineWidth', 1); % Red
plot(A1, Cs3, 'b', 'LineWidth', 1); % Blue
plot(A1, Cs4, 'm', 'LineWidth', 1); % Magenta
plot(A1, Cs5, 'g', 'LineWidth', 1); % Green
plot(A1, Cs6, 'c', 'LineWidth', 1); % Cyan
plot(A1, Cs7, 'y', 'LineWidth', 1); % Yellow
ylabel("Heat release rate (J/°CA)");
legend('40-D','B30','D60B30E10','DBEM10','DBEM15','40-DBME15','D60B30M10');
grid on
title('160 Nm');
See also
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!