linspace legend
linspace
生成线性间距向量
y = linspace(x1,x2)
y = linspace(x1,x2,n)
y = linspace(x1,x2) 返回包含 x1 和 x2 之间的 100 个等间距点的行向量。
y = linspace(x1,x2,n) 生成 n 个点。这些点的间距为 (x2-x1)/(n-1)。
linspace 类似于冒号运算符“:”,但可以直接控制点数并始终包括端点。“linspace”名称中的“lin”指示生成线性间距值而不是同级函数 logspace,后者会生成对数间距值。
clc,clear;
x = linspace(0,4*pi,10);
y = sin(x);
p = polyfit(x,y,7);
x1 = linspace(0,4*pi);
y1 = polyval(p,x1);
figure
plot(x,y,'go')
hold on
plot(x1,y1,'b')
hold off
clc,clear,close all;
x = linspace(0,1,5);
y = 1./(1+x);
p = polyfit(x,y,4);
x1 = linspace(0,2);
y1 = 1./(1+x1);
f1 = polyval(p,x1);
figure
plot(x,y,'o')
hold on
plot(x1,y1)
plot(x1,f1,'r--')
legend('y','y1','f1')
clc,clear,close all;
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1,'g')
hold on
y2 = cos(2*x);
plot(x,y2,'m')
legend('cos(x)','cos(2x)')
y3 = cos(3*x);
plot(x,y3,'DisplayName','cos(3x)')
hold off
legend('off')
clc,clear,close all;
tiledlayout(2,1)
y1 = rand(3);
ax1 = nexttile;
plot(y1)
y2 = rand(5);
ax2 = nexttile;
plot(y2)
legend(ax1,{'Line 1','Line 2','Line 3'})
clc,clear,close all;
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1,'DisplayName','cos(x)')
hold on
y2 = cos(2*x);
plot(x,y2,'DisplayName','cos(2x)')
hold off
legend
clc,clear,close all;
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
y3 = cos(3*x);
plot(x,y3)
y4 = cos(4*x);
plot(x,y4)
hold off
legend({'cos(x)','cos(2x)','cos(3x)','cos(4x)'},'Location','northwest','NumColumns',2)
clc,clear,close all;
tiledlayout(2,2);
nexttile
plot(rand(5))
nexttile
plot(rand(5))
nexttile
plot(rand(5))
lgd = legend;
lgd.Layout.Tile = 4;%版本问题
clc,clear,close all;
x = linspace(0,pi);
y1 = cos(x);
p1 = plot(x,y1);
hold on
y2 = cos(2*x);
p2 = plot(x,y2);
y3 = cos(3*x);
p3 = plot(x,y3);
hold off
legend([p1 p3],{'First','Third'})
clc,clear,close all;
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
hold off
lgd = legend('cos(x)','cos(2x)');
title(lgd,'Legend Title')
clc,clear,close all;
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
hold off
legend({'cos(x)','cos(2x)'},'Location','southwest')
legend('boxoff')
clc,clear,close all;
rdm = rand(4);
plot(rdm)
lgd = legend({'Line 1','Line 2','Line 3','Line 4'},'FontSize',12,'TextColor','m')
lgd.NumColumns = 2;