开发者

How do I make matlab legends match the colour of the graphs?

开发者 https://www.devze.com 2022-12-30 00:21 出处:网络
Here is the code I used: 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;

Here is the code I used:

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

This produces a legend matching the right colors to actual and linear, then after that it seems to skip over red开发者_StackOverflow中文版 on the graph, but not on the legend, i.e. the legend says quadratic should be red, but the graph shows it as green, the legend says cubic should be green, but the graph shows it as purple etc.

Any help is appreciated.


The lin curve needs to be fixed --- now you just have a bunch of points instead of a line. quad and cub need to be fixed as well (see below).

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

Are quad and cub meant to be the same? Maybe it should be:

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

精彩评论

暂无评论...
验证码 换一张
取 消