matlab更改legend中marker的大小

Ps:更简单的方式

可以手动添加一组自定义的数据(X, Y),把这组数据的X或Y坐标与自己的数据错开。通过句柄对随便添加的自定义的数据设置markersize。然后通过xlim或ylim把手动添加的点遮掉即可。

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

添加legend:

h = legend('C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'Orientation', 'horizontal');
legendmarkeradjust(12, 'horizontal')

 调用legendmarkeradjust(varargin)函数,第一个参数为Markersize尺寸,第二个参数为legend的方向

legendmarkeradjust函数定义如下:

function legendmarkeradjust(varargin)

a1=ver('matlab');


rls=a1.Release;

rlsnumber=str2num(rls(5:6));

if rlsnumber>=14 && ~strcmp(a1.Release,'(R2014a')
    
leg=get(legend);

legfontsize=leg.FontSize;
legstrings=leg.String;
legloc=leg.Location;

delete(legend)

[l1,l2,l3,l4]=legend(legstrings, 'Orientation', varargin{2});


for n=1:length(l2)
    if sum(strcmp(properties(l2(n)),'MarkerSize'))
        l2(n).MarkerSize=varargin{1};
    elseif sum(strcmp(properties(l2(n).Children),'MarkerSize'))
        l2(n).Children.MarkerSize=varargin{1};
    end
end

for n=1:length(l2)
        if sum(strcmp(properties(l2(n)),'FontSize'))
            l2(n).FontSize=legfontsize;
        elseif sum(strcmp(properties(l2(n).Children),'FontSize'))
            l2(n).Children.FontSize=varargin{1};
    end
end

set(l1,'location',legloc)


else
    
s=get(legend);
s1=s.Children;
s2=[];

s2=findobj(s1,{'type','patch','-or','type','line'});

switch length(varargin)
    case 1
        marksize=varargin{1};
        for m=1:length(s2)
            set(s2(m),'markersize',marksize);
        end

    case 2
        marksize=varargin{1};
        lwidth=varargin{2};

        for m=1:length(s2)
            set(s2(m),'markersize',marksize,'linewidth',lwidth);
        end
end

end

  

参考:

https://www.ilovematlab.cn/thread-583495-1-1.html

 

posted on 2021-04-26 15:40  那抹阳光1994  阅读(2815)  评论(0编辑  收藏  举报

导航