Legend Color and plot color mismatch
43 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have 8 sets of data I would like to plot and want every 2 to have the same color in the plot and in the legend. I would also like every 2 plots to have the same color for example I am trying to have plots 1 & 2 to have the same color plots 3 & 4 to have another color etc. For some reason when I plot the data the color on the plots themselves is fine but the legend color is wrong. The legend for plot A1 and A2 have default colors that I never set and the rest of legend colors are skewed by 1. For example I get B1 & B2 to be green instrad of yellow, I get C1 % C2 to be yellow instead of magenta etc. I have tried 2 methods for this one using the 'legend_bonus' and setting the legend that way and another method that is shown but commented out is when using the set for the plot make the displayname as shown either wayt i get the same result. Why is this happening and how to I fix this.
Thank you
legend_bonus = {'A1 ', 'A2 ','B1 ', 'B2 ','C1 ', 'C2 ','D1 ', 'D2 '}
%
h1 = plot(zplot_Ax1, zplot_Ay1,'o-','Color','green');
set( h1, 'LineWidth', 4)%, 'DisplayName', 'A1 ');
hold on
h1 = plot(zplot_Ax2, zplot_Ay2,'o-','Color','green');
set( h1, 'LineWidth', 4)%, 'DisplayName', 'A2 ');
hold on
%
h1 = plot(zplot_Bx1, zplot_By1,'o-','Color','yellow');
set( h1, 'LineWidth', 4)%, 'DisplayName', 'B1 ');
hold on
h1 = plot(zplot_Bx2, zplot_By2,'o-','Color','yellow');
set( h1, 'LineWidth', 4)%, 'DisplayName', 'B2 ');
hold on
%
h1 = plot(zplot_Cx1, zplot_Cy1,'o-','Color','magenta');
set( h1, 'LineWidth', 4)%, 'DisplayName', 'C1 ');
hold on
h1 = plot(zplot_Cx2, zplot_Cy2,'o-','Color','magenta');
set( h1, 'LineWidth', 4)%, 'DisplayName', 'C2 ');
hold on
%
h1 = plot(zplot_Dx1, zplot_Dy1,'o-','Color','red');
set( h1, 'LineWidth', 4)%, 'DisplayName', 'D1 ');
hold on
h1 = plot(zplot_Dx2, zplot_Dy2,'o-','Color','red');
set( h1, 'LineWidth', 4)%, 'DisplayName', 'D2 ');
hold on
title('title');
xlabel('X axis');
ylabel('Y axis');
ax = gca;
ax.FontSize = 20;
legend(legend_bonus);
legend('Location', 'best');
2 Kommentare
Cris LaPierre
am 9 Mai 2024
Here's an attempt to create a runnable example. The issue is not reproducible here.
zplot_Ax1 = rand(1,10);
zplot_Ax2 = rand(1,10);
zplot_Bx1 = rand(1,10);
zplot_Bx2 = rand(1,10);
zplot_Cx1 = rand(1,10);
zplot_Cx2 = rand(1,10);
zplot_Dx1 = rand(1,10);
zplot_Dx2 = rand(1,10);
zplot_Ay1 = rand(1,10);
zplot_Ay2 = rand(1,10);
zplot_By1 = rand(1,10);
zplot_By2 = rand(1,10);
zplot_Cy1 = rand(1,10);
zplot_Cy2 = rand(1,10);
zplot_Dy1 = rand(1,10);
zplot_Dy2 = rand(1,10);
legend_bonus = {'A1 ', 'A2 ','B1 ', 'B2 ','C1 ', 'C2 ','D1 ', 'D2 '};
%
h1 = plot(zplot_Ax1, zplot_Ay1,'o-','Color','green','LineWidth', 4);
hold on
h1 = plot(zplot_Ax2, zplot_Ay2,'o-','Color','green','LineWidth', 4);
%
h1 = plot(zplot_Bx1, zplot_By1,'o-','Color','yellow','LineWidth', 4);
h1 = plot(zplot_Bx2, zplot_By2,'o-','Color','yellow','LineWidth', 4);
%
h1 = plot(zplot_Cx1, zplot_Cy1,'o-','Color','magenta','LineWidth', 4);
h1 = plot(zplot_Cx2, zplot_Cy2,'o-','Color','magenta','LineWidth', 4);
%
h1 = plot(zplot_Dx1, zplot_Dy1,'o-','Color','red','LineWidth', 4);
h1 = plot(zplot_Dx2, zplot_Dy2,'o-','Color','red','LineWidth', 4);
hold off
title('title');
xlabel('X axis');
ylabel('Y axis');
legend(legend_bonus);
Antworten (1)
Josh
am 9 Mai 2024
code seems to work fine for me so might be something more local to your machine or version of MATLAB? sorry i'm not able to provide a clearer answer to your issue
...copy & pasted, trimmed out redundant lines (no need for multiple 'hold on') and added in some random data to plot. figure attached below was produced from running the code:
legend_bonus = {'A1 ', 'A2 ','B1 ', 'B2 ','C1 ', 'C2 ','D1 ', 'D2 '}
%
zplot_Ax1=randi(10,1,10);
zplot_Ay1=randi(10,1,10);
zplot_Ax2=randi(10,1,10);
zplot_Ay2=randi(10,1,10);
plot(zplot_Ax1, zplot_Ay1,'o-','Color','green','LineWidth',4);
hold on
plot(zplot_Ax2, zplot_Ay2,'o-','Color','green','LineWidth',4);
%
zplot_Bx1=randi(10,1,10);
zplot_By1=randi(10,1,10);
zplot_Bx2=randi(10,1,10);
zplot_By2=randi(10,1,10);
plot(zplot_Bx1, zplot_By1,'o-','Color','yellow','LineWidth',4);
plot(zplot_Bx2, zplot_By2,'o-','Color','yellow','LineWidth',4);
%
zplot_Cx1=randi(10,1,10);
zplot_Cy1=randi(10,1,10);
zplot_Cx2=randi(10,1,10);
zplot_Cy2=randi(10,1,10);
plot(zplot_Cx1, zplot_Cy1,'o-','Color','magenta','LineWidth',4);
plot(zplot_Cx2, zplot_Cy2,'o-','Color','magenta','LineWidth',4);
%
zplot_Dx1=randi(10,1,10);
zplot_Dy1=randi(10,1,10);
zplot_Dx2=randi(10,1,10);
zplot_Dy2=randi(10,1,10);
plot(zplot_Dx1, zplot_Dy1,'o-','Color','red','LineWidth',4);
plot(zplot_Dx2, zplot_Dy2,'o-','Color','red','LineWidth',4);
title('title');
xlabel('X axis');
ylabel('Y axis');
ax = gca;
ax.FontSize = 20;
legend(legend_bonus);
legend('Location', 'best');
0 Kommentare
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!