Legend not showing in MATLAB 2020b but works in 2023b
43 views (last 30 days)
Show older comments
The following MATLAB script plots multiple scatter points and a filled polygon, adding them dynamically in a loop. I use the DisplayName property for legend entries and then create a legend using an array of handles.
The legend displays correctly in MATLAB 2023b, but in MATLAB 2020b, the legend does not appear.
% Test data
x=rand(1,10);
y=rand(1,10);
iterationNumber = 2;
A=rand(iterationNumber,10);
B=rand(iterationNumber,10);
C=rand(iterationNumber,10);
D=rand(iterationNumber,10);
% Proper polygon for fill
z = [0 4 5 2 1];
w = [0 0 2 4 3];
fig = figure;
hold on;
grid on;
h1 = scatter(x, y, 'DisplayName', 'XY Points');
h2 = fill(z, w, 'r', 'DisplayName', 'Polygon');
handles = [h1 h2];
for idx = 1:iterationNumber
h3 = scatter(A(idx,:), B(idx,:), 'DisplayName', sprintf('Mode %d', idx));
h4 = scatter(C(idx,:), D(idx,:), 'DisplayName', 'Potential');
handles = [handles h3 h4];
end
xlabel('xLabel');
ylabel('yLabel');
legend(handles);
hold off;
Is there a difference in how DisplayName and legend(handles) are handled between MATLAB 2020b and newer versions? What is the recommended way to ensure that legends show correctly in older versions?
Answers (0)
See Also
Categories
Find more on Legend 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!