Clear Filters
Clear Filters

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

2 views (last 30 days)
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 Comments
Aakash
Aakash on 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

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 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 Comments
Walter Roberson
Walter Roberson on 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');

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!