Matlab figure: The legend replaces the graph completely and there is no graph
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2024-2-24
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!