MATLAB Answers

legend color does match with drawn lines?

1 view (last 30 days)
Hi,
I draw the plot and run the code but the legends color does not match with the line drawn? Does not Why, previously when i run the same code, it's showing perfectly fine but this time it's don't.
Can I changes the color code now or I have to run the codes again as it will take 12 hours to run the same code. The code i written for the plot are as follows:
fig3 = figure(3);
saveas(fig3,'BER.png');
semilogy(t,ber_fixed(1,:),'-ok','linewidth',2); hold on; grid on;
semilogy(t,ber_dynamic(1,:),'--ok','linewidth',2);
semilogy(t,ber_fixed1(2,:),'-o','linewidth',2); hold on;
semilogy(t,ber_dynamic1(2,:),'-+r','linewidth',2);
legend('Fixed user-1','Dynamic user-1','Fixed1 user-2','Dynamic1 user-2');
xlabel('No. of simulated positions(t_m)');
ylabel('Individual bit error rate (BER)');
axis([1 position 0 1]);
Please help me to resolve the issue, because donot know run same code will or will not show same mistake again.
  1 Comment
Wan Ji
Wan Ji on 20 Aug 2021
It's becuase you hold all the lines in the figure at the first run of your code, and later you run the code again, the two different colors may mix together to produce another color. What you need to do is just use clf right after the command figure.
fig3 = figure(3);
clf;

Sign in to comment.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 20 Aug 2021
When you want proper control of what is shown in legends always use the graphics-handles returned by the plotting-functions:
fig3 = figure(3);
saveas(fig3,'BER.png');
ph = semilogy(t,ber_fixed(1,:),'-ok','linewidth',2); hold on; grid on;
ph(2) = semilogy(t,ber_dynamic(1,:),'--ok','linewidth',2);
ph(3) = semilogy(t,ber_fixed1(2,:),'-o','linewidth',2); hold on;
ph(4) = semilogy(t,ber_dynamic1(2,:),'-+r','linewidth',2);
legend(ph,'Fixed user-1','Dynamic user-1','Fixed1 user-2','Dynamic1 user-2');
xlabel('No. of simulated positions(t_m)');
ylabel('Individual bit error rate (BER)');
axis([1 position 0 1]);
Here you can exclude some lines from the legend by simply selecting a subset of ph in the legend-call, and manipulate the line style, color and width etc after the initial plotting to "spice-up" the plot as you see fit.
HTH

More Answers (0)

Community Treasure Hunt

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

Start Hunting!