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

22 views (last 30 days)

Show older comments

LUCA D'AMBROSIO on 6 Jul 2024 at 8:53

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable

Commented: Star Strider on 6 Jul 2024 at 14:13

Accepted Answer: Star Strider

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 Comment

Show -1 older commentsHide -1 older comments

LUCA D'AMBROSIO on 6 Jul 2024 at 10:00

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204181

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204181

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

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

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Star Strider on 6 Jul 2024 at 11:00

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#answer_1481966

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#answer_1481966

Open in MATLAB Online

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 Comments

Show 2 older commentsHide 2 older comments

LUCA D'AMBROSIO on 6 Jul 2024 at 11:21

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204211

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204211

i posted the function fn above.

i don't know if probably isnt visualized correctly, here i attach it again:

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

Star Strider on 6 Jul 2024 at 13:05

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204271

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204271

Open in MATLAB Online

I just looked at the code in your Comment.

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});

% hc{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')

if q == a

hl = legend([hc{:}]);

end

end

sppos = [subplot(2,2,2).Position; subplot(2,2,4).Position];

lpos = hl.Position;

hl.Position = [sppos(1,1)+sppos(1,3) mean(sppos(:,2))+sppos(1,4)*0.25 lpos(3)*0.85 lpos(4)];

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

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

This appears to elilminate the extraneous llines. I did my best to get the legend to be narrower so that it fits, however the third position argument to it (‘width’) does not appear to be working correctly here. (It may work correctly in your code.)

.

LUCA D'AMBROSIO on 6 Jul 2024 at 14:12

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204311

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204311

thank you very much, it works very well. thank you

Star Strider on 6 Jul 2024 at 14:13

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204316

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134971-problem-with-legend-colors-don-t-match-value-of-variable#comment_3204316

As always, my pleasure!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend

Find more on Legend in Help Center and File Exchange

Tags

  • legend
  • colorstring
  • indexing
  • plot
  • subplot
  • color

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


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

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Problem with legend: colors don't match value of variable (2024)
Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6640

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.