Setting line colors in the MATLAB story legend?

I use the plotgauss2d BNT function to visualize how the response of a 2D Gaussian node changes when evidence is observed elsewhere on the network.

eng = jtree_inf_engine(bnet); evidence = cell(1, 2) eng = enter_evidence(eng, evidence); marginals = marginal_nodes(eng, 1); p_1 = marginals.T marginals = marginal_nodes(eng, 2); p_2 = marginals.T marginals clf; plotgauss2d(marginals.mu, marginals.Sigma); hold all; evidence{1} = 1; marginals = marginal_nodes(enter_evidence(eng, evidence), 2); p = plotgauss2d(marginals.mu, marginals.Sigma); set(p, 'Color', 'green'); evidence{1} = 2; marginals = marginal_nodes(enter_evidence(eng, evidence), 2); p = plotgauss2d(marginals.mu, marginals.Sigma); set(p, 'Color', 'red'); legend({'Unknown', 'Class 1', 'Class 2'}); hold off; 

enter image description here

As you can see, the legend does not collect the changed colors that I had to set manually. (Unfortunately, plotgauss2d does not cycle colors automatically as you might wish.)

Is there a way to set the line colors used in the legend?

+4
source share
2 answers

The PLOTGAUSS2D function returns a vector of three descriptors corresponding to the minor axis, main axis, and ellipse, respectively. So, here is an example of how to store descriptors 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'}) 

screenshot

+7
source

The legend does display line colors, styles, markers, etc.

Each plotgauss2d plot must have at least three plot . So your legend command applies legend to the first three plot s, from the first call to plotgauss2d .

By skipping part of your code, you can make your legend by doing the following:

 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 call legend to apply legend to one plot from each of your calls to plotgauss2d .

In addition, I would suggest adding a line at the end:

 axis equal; 

I think you will like what he does.

+2
source

All Articles