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

2 ビュー (過去 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');
Taimoor Khan Mehmand
Taimoor Khan Mehmand 2023 年 6 月 23 日
Thanks a lot. It worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by