legend color does match with drawn lines?

1 次查看(过去 30 天)
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 个评论
Wan Ji
Wan Ji 2021-8-20
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;

请先登录,再进行评论。

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2021-8-20
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

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by