Problem with legend: colors don’t match value of variable
hello, i have a problem with the legend in this script: i am doing subplots for the two parameters e and t, in every subplot e is fixed while t can vary between 5 different values. the problem is the legend doesn’t match the colors. for instance, t= -50 should be red, -25 green, 0 blue, 25 light blue and t = 50 black, but the legend doesnt say this. Could anybody give me any suggestions on how to index the color and the value of the parameter so that they match? thank you
here is the code:
clear; clc; clf;
n = 1001;
x = linspace(-100, 100, n);
y = linspace(-100, 100, n);
[X, Y] = meshgrid(x, y);
clr = [‘r’,’g’,’b’,’c’,’k’];
a = 4;
e = [-50 -10 0 50];
t = linspace(-50, 50, 5);
LegendsStrings = cell(length(t),1);
for q = 1:a
for r = 1:5
Z = fn(X, Y, e(q), t(r), n);
subplot(2, 2, q)
xline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
yline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
hold on
v = [0, 0];
contour(X, Y, Z, v, ‘LineWidth’, 1.5, ‘LineColor’, clr(r))
xlabel(‘x’)
ylabel(‘y’)
title("e = " + e(q))
grid on
axis equal
hold off
LegendsStrings{r} = [‘t = ‘, num2str(t(r))];
end
legend(LegendsStrings, ‘Interpreter’, ‘none’)
end
function Z = fn(X,Y,e,t,n)
Z = zeros(n, n);
B = X + Y + e + t;
D = X.*Y – e.*t;
for i= 1:n
for j= 1:n
if B(i,j) >= 0
Z(i,j) = D(i,j);
else
Z(i,j) = -1;
end
end
end
endhello, i have a problem with the legend in this script: i am doing subplots for the two parameters e and t, in every subplot e is fixed while t can vary between 5 different values. the problem is the legend doesn’t match the colors. for instance, t= -50 should be red, -25 green, 0 blue, 25 light blue and t = 50 black, but the legend doesnt say this. Could anybody give me any suggestions on how to index the color and the value of the parameter so that they match? thank you
here is the code:
clear; clc; clf;
n = 1001;
x = linspace(-100, 100, n);
y = linspace(-100, 100, n);
[X, Y] = meshgrid(x, y);
clr = [‘r’,’g’,’b’,’c’,’k’];
a = 4;
e = [-50 -10 0 50];
t = linspace(-50, 50, 5);
LegendsStrings = cell(length(t),1);
for q = 1:a
for r = 1:5
Z = fn(X, Y, e(q), t(r), n);
subplot(2, 2, q)
xline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
yline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
hold on
v = [0, 0];
contour(X, Y, Z, v, ‘LineWidth’, 1.5, ‘LineColor’, clr(r))
xlabel(‘x’)
ylabel(‘y’)
title("e = " + e(q))
grid on
axis equal
hold off
LegendsStrings{r} = [‘t = ‘, num2str(t(r))];
end
legend(LegendsStrings, ‘Interpreter’, ‘none’)
end
function Z = fn(X,Y,e,t,n)
Z = zeros(n, n);
B = X + Y + e + t;
D = X.*Y – e.*t;
for i= 1:n
for j= 1:n
if B(i,j) >= 0
Z(i,j) = D(i,j);
else
Z(i,j) = -1;
end
end
end
end hello, i have a problem with the legend in this script: i am doing subplots for the two parameters e and t, in every subplot e is fixed while t can vary between 5 different values. the problem is the legend doesn’t match the colors. for instance, t= -50 should be red, -25 green, 0 blue, 25 light blue and t = 50 black, but the legend doesnt say this. Could anybody give me any suggestions on how to index the color and the value of the parameter so that they match? thank you
here is the code:
clear; clc; clf;
n = 1001;
x = linspace(-100, 100, n);
y = linspace(-100, 100, n);
[X, Y] = meshgrid(x, y);
clr = [‘r’,’g’,’b’,’c’,’k’];
a = 4;
e = [-50 -10 0 50];
t = linspace(-50, 50, 5);
LegendsStrings = cell(length(t),1);
for q = 1:a
for r = 1:5
Z = fn(X, Y, e(q), t(r), n);
subplot(2, 2, q)
xline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
yline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
hold on
v = [0, 0];
contour(X, Y, Z, v, ‘LineWidth’, 1.5, ‘LineColor’, clr(r))
xlabel(‘x’)
ylabel(‘y’)
title("e = " + e(q))
grid on
axis equal
hold off
LegendsStrings{r} = [‘t = ‘, num2str(t(r))];
end
legend(LegendsStrings, ‘Interpreter’, ‘none’)
end
function Z = fn(X,Y,e,t,n)
Z = zeros(n, n);
B = X + Y + e + t;
D = X.*Y – e.*t;
for i= 1:n
for j= 1:n
if B(i,j) >= 0
Z(i,j) = D(i,j);
else
Z(i,j) = -1;
end
end
end
end legend, colorstring, indexing, plot, subplot, color MATLAB Answers — New Questions