一、matlablegend顏色
matlablegend是Matlab中一個繪製圖例的函數,我們可以使用它給我們的繪圖結果添加圖例。其中一個重要的參數就是legend的顏色。在matlablegend中可以使用顏色名稱、縮寫或者RGB、CMYK值對顏色進行設定。下面是一個將圖例的顏色等分成五份並添加到plot圖中的示例:
figure
x = linspace(0,10);
y1 = sin(x);
y2 = cos(x);
y3 = x;
y4 = x.^2;
y5 = x.^3;
plot(x, y1)
hold on
plot(x, y2)
plot(x, y3)
plot(x, y4)
plot(x, y5)
legend('sine', 'cosine', 'linear', 'squared', 'cubed')
cm = lines(5);
for i=1:5
p = plot(nan,nan,'s');
set(p,'markersize',10,'markeredgecolor',cm(i,:),'markerfacecolor',cm(i,:));
end
hold off
在這段代碼中,我們使用了Matlab自帶的colors函數lines()將五種不同的顏色提取出來,並將這些顏色等分分別添加到plot圖中實現顏色的區分。
二、matlab legend大小
設置matlab legend的大小需要使用到函數set(),在set()函數中可以設置legend的fontsize、fontweight、fontname等參數。下面是一個將字體大小設置成14,並且字體類型設置為Times New Roman的代碼示例:
figure
x = linspace(0,10);
y1 = sin(x);
y2 = cos(x);
y3 = x;
y4 = x.^2;
y5 = x.^3;
plot(x, y1)
hold on
plot(x, y2)
plot(x, y3)
plot(x, y4)
plot(x, y5)
legend('sine', 'cosine', 'linear', 'squared', 'cubed')
set(gca,'FontSize',14,'FontName','Times New Roman')
hold off
在這段代碼中,我們使用了set()函數將整個圖的字體大小和字體類型進行了統一設置。
三、matlablegend函數用法
matlablegend函數是Matlab中用來添加圖例的函數。它可以將多個subplots的圖例合併成一個,也可以將單個subplot的圖例添加上。除了提供圖例名稱外,還可以設置它們的坐標位置、符號大小等。下面是一個簡單的示例:
x=linspace(0,pi);
y1=sin(x);
y2=cos(x);
y3=sin(x).*cos(x);
figure;
subplot(2,2,1); plot(x,y1); title('Sine');
subplot(2,2,2); plot(x,y2); title('Cosine');
subplot(2,2,3); plot(x,y3); title('Sine times cosine');
titleHandle = title('Subplot1'); % Capture the handle of the subplot
hLegend = legend('Sine','Cosine','Sine times cosine');
set(hLegend,'FontSize',8,'Location','SouthEast')
set(titleHandle,'FontSize',8);
在這段代碼中,我們將三個subplot的圖例添加到一個legend中,並且將legend放到了subplot的右下角。其中,Location參數用來設定legend的位置,可以選擇為NorthEast、SouthWest、SouthEast等等。
四、matlablegend大小調整
使用matlablegend函數添加圖例後,我們可以使用legend中的子函數set()來設置圖例的各項參數。可以通過set函數來修改legend中的屬性,如font size、position、alignment等等。下面是一個在legend中增加一個標題,並且調整邊框的大小的實例:
set(hlegend,'Fontsize',10,'Box','on','LineWidth',2) % Set legend position, fontsize, and box properties htitle = get(hlegend,'Title'); % Get title's handle set(htitle,'String','My Legend','Fontsize',11); % Set title of legend
在這段代碼中,我們調整了legend的字體大小、位置和邊框大小,並且給legend添加了一個標題。
五、matlab legend函數功能
matlablegend函數的功能非常強大,它可以為每個subplot添加不同的圖例樣式,還可以在legend中增加很多自定義屬性,如圖例形狀、線型、線寬等等。下面是一個將多個subplot的圖例添加到一個legend中,並且設置每個subplot的圖例樣式的示例:
x=linspace(0,pi);
y1=sin(x);
y2=cos(x);
y3=sin(x).*cos(x);
x4=linspace(0,pi*2);
y4=cos(x4);
x5=linspace(-pi/2,pi/2);
y5=tan(x5);
figure; subplot(2,3,1); plot(x,y1); title('Sine');
subplot(2,3,2); plot(x,y2); title('Cosine');
subplot(2,3,3); plot(x,y3); title('Sine times cosine');
subplot(2,3,4); plot(x4,y4); title('Double frequency cosine');
subplot(2,3,5); plot(x5,y5); title('Tangent');
legendHandle = legend({'sine'; 'cosine'; 'sine times cosine'; 'double frequency cosine'; 'tangent'});
set(legendHandle,'Orientation','vertical', 'Location','North','Position',[0.39,0.4,0.2,0.2]);
set(findobj(legendHandle,'type','line','linestyle','-'),'color','green','linewidth',2)
set(findobj(legendHandle,'type','line','color',[0 0 1]),'linestyle','-')
在這段代碼中,我們將多個subplot的圖例添加到了一個legend中,並且為不同的圖例添加了不同的線型、顏色等自定義屬性。
六、matlab的legend用法
matlablegend函數是Matlab中最常用的函數之一。它可以讓我們很簡單地將圖例添加到我們的繪圖結果中。除了函數本身的功能外,我們還可以通過其他設置來對legend進行修改。下面是一個在legend中增加加粗效果以及背景色的代碼示例:
figure;
x=linspace(0,2*pi);
plot(x,sin(x),'-r',x,cos(x),'-g');
lh = legend('sin','cos');
set(lh,'color',[.9 .9 .9],'EdgeColor',[.7 .7 .7],'FontSize',18,'FontWeight','bold')
在這段代碼中,我們為legend增加了加粗的效果以及背景色,使其更加美觀。
七、matlab legend位置
在Matlab中,我們使用legend函數來添加圖例,位置參數用來設置legend的位置。這個位置參數可以更直觀的通過圖形化的交互操作來進行選擇。下面是一種通過交互窗口選擇legend位置的代碼示例:
figure
x = linspace(0,10);
y1 = sin(x);
y2 = cos(x);
y3 = x;
y4 = x.^2;
y5 = x.^3;
plot(x, y1)
hold on
plot(x, y2)
plot(x, y3)
plot(x, y4)
plot(x, y5)
legend('sine', 'cosine', 'linear', 'squared', 'cubed')
set(gca,'FontSize',14,'FontName','Times New Roman')
[hleg, hobj, hout, mout] = ...
legend('show');
hleg.Location = 'southwest';
hold off
在這段代碼中,我們使用legend(‘show’)的方式來喚起圖形化的交互操作,方便選擇legend的位置。
八、matlab繪圖legend用法
Matlab中,legend函數是用來將圖例添加到繪圖結果中的。它可以僅添加到指定的個別對象中,也可以添加到整個圖像中。下面是一個用來將圖例添加到subplot中的代碼示例:
x=linspace(0,pi);
y1=sin(x);
y2=cos(x);
figure;
subplot(2,2,1);
plot(x,y1);
title('Sine');
subplot(2,2,2);
plot(x,y2);
title('Cosine');
subplot(2,2,[3, 4]);
plot(x,y1,'--r',x,y2,'--b');
lh = legend('sin','cos');
set(lh,'Location','NorthWest','FontSize',14);
在這段代碼中,我們使用三個subplot來繪製不同的曲線,並且將兩個曲線的legend添加到第三個subplot中。其中,Location參數用來指定legend的位置,可以選擇為NorthEast、SouthWest等等。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/151628.html
微信掃一掃
支付寶掃一掃