Matlab figure: The legend replaces the graph completely and there is no graph

4 views (last 30 days)
I am trying to plot four matrices as a line graph with two y axes, when the legend is added it completely replaces the graph and covers the whole figure. I believe the graph is not even behind the legend. There is just one empty figure with a full-screen legend! not graphs! Same thing happens if I just plot the graph and add the legend manually in the figure wondow. Here is my code and I would be grateful if you could help me fix it.
load('PS1.mat')
load("PS2.mat")
load("PS21.mat")
load("PS22.mat")
figure;
yyaxis left; % Use the left y-axis
plot(PS1, 'DisplayName', 'Scenario 1', 'Color', rand(1,3));
hold on;
plot(PS2(:, 1), 'DisplayName', 'Scenario 2', 'Color', rand(1,3));
plot(PS21, 'DisplayName', 'Scenario 3', 'Color', rand(1,3));
plot(PS22, 'DisplayName', 'Scenario 4', 'Color', rand(1,3));
ylabel('Optimal (h)');
% Adding the second column of D with a second y-axis
yyaxis right; % Use the right y-axis
plot(PS2(:, 2), 'DisplayName', 'Scenario 2', 'Color', rand(1,3));
ylabel('Modules');
% Adding legend
lgd.Position = [0.85, 0.65, 0.1, 0.1];
% Adding x-axis label
xlabel('Demand');
% Display the grid
grid on;
  9 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2024
C:\Users\Elino\OneDrive\Documents\MATLAB\ENCI711\Copy_of_Publish Version\set.m
You need to rename that file, or move it so that it is not on your MATLAB path.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!