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

1 Ansicht (letzte 30 Tage)
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 Kommentare
Aakash
Aakash am 23 Jun. 2023
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Jun. 2023
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 Kommentare
Walter Roberson
Walter Roberson am 23 Jun. 2023
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');

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by