Different Colors in plot and legend
112 views (last 30 days)
Show older comments
Hi,
In an app I am working on, I am plotting different graphs toghether in one plot. The first 6 lines need defined colors, the rest does not. This part is working correctly, but the legend shows the same color for the other graphs as the graphs before and not the actual colors. Does it have something to do with the arrays for the first six lines? I tried initializing them as 1x500 and 500x1 arrays but that didn't make a difference. Unfortunately, I also can't "hardcode" the legend using 'DisplayName' when plotting, as the number of plots can be different. The code I provided is a bit downsized for readability and I only cut out parts that either don't run in this case (ifs and switches) or just change the label or name of the graphic or figure.
Any guesses?

figure()
hold on
app.Data = getAllCycles(app);
x = linspace(0, app.CycleToEvaluateSlider.Limits(2), 500);
dataType = strcat(dataType, "(A)");
% linearly distributed values from 0 to the limit of the slider (number of cycles)
% number of values = rows in target sheet
y = zeros(500,1)+targets.(dataType)(1);
plot(x,y, 'r')
if strcmp(dataType,"temp")
y = zeros(500,1)+targets.(dataType)(2);
plot(x,y, 'Color', [1 0.64 0.57])
y = zeros(500,1)+targets.(dataType)(3);
plot(x,y, 'Color', [1 0.64 0.57])
strLegend = ["Target", "Min", "Max",app.DataToInclude.Items];
else
y = zeros(500,1)+targets.(dataType)(2);
plot(x,y, 'r')
for i=3:1:length(targets.(dataType))
y = zeros(500,1)+targets.(dataType)(i);
plot(x,y, 'Color', [1 0.64 0.57])
end
strLegend = ["Min", "Max", "MinTol-", "MinTol+", "MaxTol-",...
"MaxTol+",app.DataToInclude.Items];
end
for i=2:2:size(app.Data,2)
x = app.Data.(i-1);
y = app.Data.(i);
plot(x,y)
end
xlabel("Period number")
ylabel('Force in N')
set(gcf,'Name', strjoin([app.TesttypeDropDown.Value ", " app.DatatypeDropDown.Value]))
legend(strLegend, 'Interpreter', 'none');
set(gca,"Fontsize", 20)
set(gca,"fontname", "Arial")
set([gca, gcf], 'units', 'centimeters')
set(gca, 'Position', [3 2 26 17]) %position and size of the chart
set(gcf, 'Position', [0 2 30 20]) %position and size of the figure (window)
set(gca,'XMinorTick','on','YMinorTick','on')
set(gca,'YGrid','on')
hold off
2 Comments
Rik
on 24 Apr 2023
I always try to use DisplayName. That way it is immediately obvious when you're creating multiple line objects when you intend to only create one. I suspect that is the case here as well.
You should also consider creating an array of handles to feed into legend. That way you also have more control over what is happening, and it makes it easier to spot when you have more objects than you expected.
Accepted Answer
Cris LaPierre
on 25 Apr 2023
Legends take their linespec from the line object they correspond to. They are assigned in the same order they are plotted unless you specify the subset of objects to add to the legend using the following syntax:
I must admit I didn't put much effort into trying to figure out what is getting plotted from your code, but if you use line objects to add items to your legend, you can be more certain about which lines and therefore which linespecs are appearing.
x = linspace(0,pi);
y1 = cos(x);
p1 = plot(x,y1);
hold on
y2 = cos(2*x);
p2 = plot(x,y2);
y3 = cos(3*x);
p3 = plot(x,y3);
hold off
legend([p1 p3],{'First','Third'})
More Answers (0)
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!