# Problem with legend: colors don't match value of variable

28 views (last 30 days)
LUCA D'AMBROSIO on 6 Jul 2024 at 8:53
Commented: Star Strider on 6 Jul 2024 at 14:13
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
LUCA D'AMBROSIO on 6 Jul 2024 at 10:00
update: i tried the following, now the colors match but the legend adds some unused lines
n = 101;
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(numel(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
LegendsStrings{r} = ['t = ', num2str(t(r))];
v = [0, 0];
contour(X, Y, Z, v, 'LineWidth', 1.5, 'LineColor', clr(r), 'DisplayName', LegendsStrings{r})
xlabel('x')
ylabel('y')
title("e = " + e(q))
grid on
axis equal
hold off
% xp = [-95, -80];
% yp = (-30-12*r)*ones(1, 2);
% plot(xp, yp, ['-', clr(r)])
% text(xp(2)+5, yp(2),['t = ', num2str(t(r))],'FontSize',8)
end
legend()
end

Star Strider on 6 Jul 2024 at 11:00
I can’t run this since ‘fn’ is missing.
n = 101;
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(numel(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
LegendsStrings{r} = ['t = ', num2str(t(r))];
v = [0, 0];
hc(r) = contour(X, Y, Z, v, 'LineWidth', 1.5, 'LineColor', clr(r), 'DisplayName', LegendsStrings{r});
xlabel('x')
ylabel('y')
title("e = " + e(q))
grid on
axis equal
hold off
% xp = [-95, -80];
% yp = (-30-12*r)*ones(1, 2);
% plot(xp, yp, ['-', clr(r)])
% text(xp(2)+5, yp(2),['t = ', num2str(t(r))],'FontSize',8)
end
legend(hc, 'Location','best')
end
Unrecognized function or variable 'fn'.
That aside, I propose a solution, that being to create a handle to the contours you want, and then pass them to your legend call. (I will test this when ‘fn’ appears. In the interim, I leave that to you)
.
LUCA D'AMBROSIO on 6 Jul 2024 at 14:12
thank you very much, it works very well. thank you
Star Strider on 6 Jul 2024 at 14:13
As always, my pleasure!