空传奇Matlab

时间:2015-09-23 12:36:48

标签: matlab legend

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

            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);    

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

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

            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);