如何使matlab图例与图形的颜色相匹配?

时间:2010-05-10 18:29:10

标签: matlab graph

以下是我使用的代码:

x = linspace(0,2);
e = exp(1);
lin = e;
quad = e-e.*x.*x/2;
cub  = e-e.*x.*x/2;
quart = e-e.*x.*x/2+e.*x.*x.*x.*x/24;
act = e.^cos(x);
mplot = plot(x,act,x,lin,x,quad,x,cub,x,quart);
legend('actual','linear','quadratic','cubic','quartic')

这会产生一个匹配正确颜色与实际和线性相关的图例,然后它会在图表上跳过红色,但不会跳过图例,即图例中的二次方应为红色,但图表显示为绿色,传说立方体应该是绿色,但图表显示为紫色等。

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

需要修复lin曲线 - 现在你只有一堆点而不是一条线。 quadcub也需要修复(见下文)。

x = linspace(0,2);
e = exp(1);
lin = ones(size(x))*e; %#Now it's a vector with the same size as x
quad = e-e.*x.*x/2;
cub  = e-e.*x.*x/2;
quart = e-e.*x.*x/2+e.*x.*x.*x.*x/24;
act = e.^cos(x);
mplot = plot(x,act,x,lin,x,quad,x,cub,x,quart);
legend('actual','linear','quadratic','cubic','quartic')

quadcub是否相同?也许它应该是:

quad = e-e.*x.*x/2;
cub  = e-e.*x.*x.*x/2;