Setting line colors in legend of MATLAB plot?

血红的双手。 提交于 2019-12-03 16:35:32

The PLOTGAUSS2D function returns a vector of three handles corresponding to minor axis, major axis, and ellipse respectively. So here is an example how to store the handles and call LEGEND at the end:

figure, hold on
h = zeros(3,3);
h(:,1) = plotgauss2d(rand(2,1), [1 0.5; 0.5 2]);
h(:,2) = plotgauss2d(rand(2,1), [2 -0.5; -0.5 1]);
h(:,3) = plotgauss2d(rand(2,1), [1 0; 0 2]);
hold off
set(h(:,1), 'Color','r')
set(h(:,2), 'Color','g')
set(h(:,3), 'Color','c')
legend(h(1,:), {'1','2','3'})

Legend does pick up line colors, styles, markers, and so on.

You must have at least three plots in each plotgauss2d plot. As such, your legend command is applying the legend to the first three plots, all from the first call to plotgauss2d.

Skipping some of your code, you can make your legend right by doing this:

p = plotgauss2d(marginals.mu, marginals.Sigma);
h = p(1);
hold all;
evidence{1} = 1;
marginals = marginal_nodes(enter_evidence(eng, evidence), 2);
p = plotgauss2d(marginals.mu, marginals.Sigma);
h(end+1) = p(1);
set(p, 'Color', 'green');
evidence{1} = 2;
marginals = marginal_nodes(enter_evidence(eng, evidence), 2);
p = plotgauss2d(marginals.mu, marginals.Sigma);
h(end+1) = p(1);
set(p, 'Color', 'red');
legend(h,{'Unknown', 'Class 1', 'Class 2'});

Now you are causing legend to apply the legend to one plot from each of your plotgauss2d calls.

Also, I would suggest adding a line at the end:

axis equal;

I think you will like what it does.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!