Legend command does not distinguish the colours specified in the plot command

1 次查看(过去 30 天)
Here is my code. The signals are send from the Simulink to the workspace and the save format is ' Structure with time' . The code does displays the plot but in the legend first three Machines are shown with same red colour and the fourth machine i.e. Machine 19 is depicted by blue colour. Can anyone help me correct this?
figure
plot(Vpu_15.time, Vpu_15.signals.values,'r')
hold on
plot(Vpu_16.time, Vpu_16.signals.values,'b')
hold on
plot(Vpu_17.time, Vpu_17.signals.values,'g')
hold on
plot(Vpu_19.time, Vpu_19.signals.values,'k')
hold off
xlabel('Time (s)')
ylabel('Voltage (pu)')
title('Machine Voltages')
legend('Machine 15', 'Machine 16', 'Machine 17', 'Machine 19')
legend('TextColor', 'black')
  5 个评论
Aakash
Aakash 2023-6-23
Can you share your variables 'Vpu_5, Vpu_16, Vpu_17, Vpu_19' which you told are stored in the MATLAB Workspace.
You can refer to this link on how to save your variables: https://www.mathworks.com/help/matlab/matlab_env/save-load-and-delete-workspace-variables.html. Then share as attachments

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-6-23
The code does displays the plot but in the legend first three Machines are shown with same red colour and the fourth machine i.e. Machine 19 is depicted by blue colour.
We can predict that Vpu_15.signals.values has three columns, so
plot(Vpu_15.time, Vpu_15.signals.values,'r')
is creating three lines, all of them colored red.
legend() matches to graphic objects not to the number of times that graphics functions are called. graphics functions can create multiple graphics objects in one call.
Work-around:
figure
h1 = plot(Vpu_15.time, Vpu_15.signals.values,'r');
hold on
h2 = plot(Vpu_16.time, Vpu_16.signals.values,'b');
hold on
h3 = plot(Vpu_17.time, Vpu_17.signals.values,'g');
hold on
h4 = plot(Vpu_19.time, Vpu_19.signals.values,'k');
hold off
xlabel('Time (s)')
ylabel('Voltage (pu)')
title('Machine Voltages')
legend('Machine 15', 'Machine 16', 'Machine 17', 'Machine 19')
legend([h1(1); h2(1); h3(1); h4(1)], 'TextColor', 'black')
  4 个评论
Walter Roberson
Walter Roberson 2023-6-23
figure
h1 = plot(Vpu_15.time, Vpu_15.signals.values,'r');
hold on
h2 = plot(Vpu_16.time, Vpu_16.signals.values,'b');
hold on
h3 = plot(Vpu_17.time, Vpu_17.signals.values,'g');
hold on
h4 = plot(Vpu_19.time, Vpu_19.signals.values,'k');
hold off
xlabel('Time (s)')
ylabel('Voltage (pu)')
title('Machine Voltages')
legend([h1(1); h2(1); h3(1); h4(1)], {'Machine 15', 'Machine 16', 'Machine 17', 'Machine 19'}, 'TextColor', 'black');

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by