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

조회 수: 4 (최근 30일)
LUCA D'AMBROSIO
LUCA D'AMBROSIO 2024년 7월 6일
댓글: Star Strider 2024년 7월 6일
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
  댓글 수: 1
LUCA D'AMBROSIO
LUCA D'AMBROSIO 2024년 7월 6일
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
Star Strider 2024년 7월 6일
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)
.
  댓글 수: 4
LUCA D'AMBROSIO
LUCA D'AMBROSIO 2024년 7월 6일
thank you very much, it works very well. thank you
Star Strider
Star Strider 2024년 7월 6일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by