空传说Matlab

empty legend Matlab

我正在尝试在条形图上叠加标记以突出显着差异... 使用以下代码一切都很好

            figure
            y = [1 2 3 4];
            bar(y);
            ylim([0 6])
            p05=1;
            p01=2;
            p001=3;

            l1 = line(p05, 5, ...
                    'linestyle', 'none', ...
                    'marker', '*', ...
                    'markersize', 5 ...
                    );     


           l2 = line(p01, 5, ...
                    'linestyle', 'none', ...
                    'marker', 'x', ...
                    'markersize', 5 ...
                    );


           l3 =    line(p001, 5, ...
                    'linestyle', 'none', ...
                    'marker', 'o', ...
                    'markersize', 5 ...
                    );                

            leg = legend([l1,l2,l3],{'p<0.05','p<0.01','p<0.001'});
            legend('boxoff')
            set(leg,'FontSize',8);   

有时我有(例如)p05 = [] 并且图例变得错误...我的意思是第二个标记关联到 p<0.05...

            figure
            y = [1 2 3 4];
            bar(y);
            ylim([0 6])
            p05=[];
            p01=2;
            p001=3;

            l1 = line(p05, [], ...
                    'linestyle', 'none', ...
                    'marker', '*', ...
                    'markersize', 5 ...
                    );     


           l2 = line(p01, 5, ...
                    'linestyle', 'none', ...
                    'marker', 'x', ...
                    'markersize', 5 ...
                    );


           l3 =    line(p001, 5, ...
                    'linestyle', 'none', ...
                    'marker', 'o', ...
                    'markersize', 5 ...
                    );                

            leg = legend([l1,l2,l3],{'p<0.05','p<0.01','p<0.001'});
            legend('boxoff')
            set(leg,'FontSize',8);    

我该如何解决这个问题?

您好,找到了解决方案...

            lines_n = [l1,l2,l3];
            cc = [isempty(p05) isempty(p01) isempty(p001)];
            legend_names = {'p<0.05','p<0.01','p<0.001'};
            legend_names(cc)=[];


            leg = legend(lines_n,legend_names);
            legend('boxoff')
            set(leg,'FontSize',8);