Doesn't Match legend color and plot color

68 次查看(过去 30 天)
legend color doesn't match the color of plot. I have already searched the other questions asked, but none of them seem to work for me.
-----
%% f(x) and its derivatives are declared as anonymous functions
f = @(x) 2.^x .* log(2).^0;
f_prime1 = @(x) 2.^x .* log(2).^1;
f_prime2 = @(x) 2.^x .* log(2).^2;
f_prime3 = @(x) 2.^x .* log(2).^3;
% Taylor series (polynomials) are declared as anonymous functions
% x0 : expansion point
% x : the point at which I would like to evaluate the Taylor series
%% 0th order Taylor series
T0 = @(x0,x) f(x0);
% 1st order Taylor series
T1 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0);
% 2nd order Taylor series
T2 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0) + f_prime2(x0)*(x-x0).^2/2;
% 3rd order Taylor series
T3 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0) + f_prime2(x0)*(x-x0).^2/2 + f_prime3(x0)*(x-x0).^3/6;
%%
x0 = 1;
% Plot the Taylor series T0, T1, T2, T3 between -5 <= x <= +5 using expansion point x0 = 1;
x = linspace(-5,5,60);
y0 = T0(x0,x);
y1 = T1(x0,x);
y2 = T2(x0,x);
y3 = T3(x0,x);
yTrue = f(x);
%%
figure
plot(x,y0,'-ob','LineWidth',2)
hold on
plot(x,y1,'-ok','LineWidth',2)
hold on
plot(x,y2,'-or','LineWidth',2)
hold on
plot(x,y3,'-og','LineWidth',2)
hold on
plot(x,yTrue,'-oc','LineWidth',2)
xlabel('x')
ylabel('y')
legend('T0','T1','T2','T3','f(x) = 2^x')
set(gca, 'FontSize',18)
grid on
-------

采纳的回答

Star Strider
Star Strider 2022-4-3
编辑:Star Strider 2022-4-3
There are several lines created for the first plot call. Return handles to each plot call and then choose the first line of each as the axis argument array to the legend call —
f = @(x) 2.^x .* log(2).^0;
f_prime1 = @(x) 2.^x .* log(2).^1;
f_prime2 = @(x) 2.^x .* log(2).^2;
f_prime3 = @(x) 2.^x .* log(2).^3;
% Taylor series (polynomials) are declared as anonymous functions
% x0 : expansion point
% x : the point at which I would like to evaluate the Taylor series
%% 0th order Taylor series
T0 = @(x0,x) f(x0);
% 1st order Taylor series
T1 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0);
% 2nd order Taylor series
T2 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0) + f_prime2(x0)*(x-x0).^2/2;
% 3rd order Taylor series
T3 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0) + f_prime2(x0)*(x-x0).^2/2 + f_prime3(x0)*(x-x0).^3/6;
%%
x0 = 1;
% Plot the Taylor series T0, T1, T2, T3 between -5 <= x <= +5 using expansion point x0 = 1;
x = linspace(-5,5,60);
y0 = T0(x0,x);
y1 = T1(x0,x);
y2 = T2(x0,x);
y3 = T3(x0,x);
yTrue = f(x);
%%
figure
hp{1} = plot(x,y0,'-ob','LineWidth',2);
hold on
hp{2} = plot(x,y1,'-ok','LineWidth',2);
hold on
hp{3} = plot(x,y2,'-or','LineWidth',2);
hold on
hp{4} = plot(x,y3,'-og','LineWidth',2);
hold on
hp{5} = plot(x,yTrue,'-oc','LineWidth',2);
xlabel('x')
ylabel('y')
legend([hp{1}(1);hp{2}(1);hp{3}(1);hp{4}(1);hp{5}(1)], 'T0','T1','T2','T3','f(x) = 2^x')
set(gca, 'FontSize',18)
grid on
EDIT — Clarified explanation.
.

更多回答(2 个)

Voss
Voss 2022-4-3
Your first plot call plots y0 vs x. y0 is a scalar and x is 1-by-60. Plotting these variables creates 60 lines, so when you make your legend, and it has 5 entries, legend uses the first 5 lines in the axes, all of which are from that first plot call.
Instead, plot with two 1-by-60 variables by repeating y0 60 times, for instance using y0*ones(1,60), which will create a single line.
%% f(x) and its derivatives are declared as anonymous functions
f = @(x) 2.^x .* log(2).^0;
f_prime1 = @(x) 2.^x .* log(2).^1;
f_prime2 = @(x) 2.^x .* log(2).^2;
f_prime3 = @(x) 2.^x .* log(2).^3;
% Taylor series (polynomials) are declared as anonymous functions
% x0 : expansion point
% x : the point at which I would like to evaluate the Taylor series
%% 0th order Taylor series
T0 = @(x0,x) f(x0);
% 1st order Taylor series
T1 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0);
% 2nd order Taylor series
T2 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0) + f_prime2(x0)*(x-x0).^2/2;
% 3rd order Taylor series
T3 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0) + f_prime2(x0)*(x-x0).^2/2 + f_prime3(x0)*(x-x0).^3/6;
%%
x0 = 1;
% Plot the Taylor series T0, T1, T2, T3 between -5 <= x <= +5 using expansion point x0 = 1;
x = linspace(-5,5,60);
y0 = T0(x0,x);
y1 = T1(x0,x);
y2 = T2(x0,x);
y3 = T3(x0,x);
yTrue = f(x);
%%
figure
whos x y*
Name Size Bytes Class Attributes x 1x60 480 double y0 1x1 8 double y1 1x60 480 double y2 1x60 480 double y3 1x60 480 double yTrue 1x60 480 double
plot(x,y0*ones(1,60),'-ob','LineWidth',2)
hold on
plot(x,y1,'-ok','LineWidth',2)
hold on
plot(x,y2,'-or','LineWidth',2)
hold on
plot(x,y3,'-og','LineWidth',2)
hold on
plot(x,yTrue,'-oc','LineWidth',2)
xlabel('x')
ylabel('y')
legend('T0','T1','T2','T3','f(x) = 2^x')
set(gca, 'FontSize',18)
grid on

Emrah Can Ucar
Emrah Can Ucar 2022-4-3
clear
clc
%% f(x) and its derivatives are declared as anonymous functions
f = @(x) 2.^x .* log(2).^0;
f_prime1 = @(x) 2.^x .* log(2).^1;
f_prime2 = @(x) 2.^x .* log(2).^2;
f_prime3 = @(x) 2.^x .* log(2).^3;
% Taylor series (polynomials) are declared as anonymous functions
% x0 : expansion point
% x : the point at which I would like to evaluate the Taylor series
%% 0th order Taylor series
T0 = @(x0,x) f(x0);
% 1st order Taylor series
T1 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0);
% 2nd order Taylor series
T2 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0) + f_prime2(x0)*(x-x0).^2/2;
% 3rd order Taylor series
T3 = @(x0,x) f(x0) + f_prime1(x0)*(x-x0) + f_prime2(x0)*(x-x0).^2/2 + f_prime3(x0)*(x-x0).^3/6;
%%
x0 = 1;
% Plot the Taylor series T0, T1, T2, T3 between -5 <= x <= +5 using expansion point x0 = 1;
x = linspace(-5,5,60);
y0 = T0(x0,x);
y1 = T1(x0,x);
y2 = T2(x0,x);
y3 = T3(x0,x);
yTrue = f(x);
%
hold on
grid on
xlabel('x')
ylabel('y')
set(gca, 'FontSize',18)
plot(x,y0,'LineWidth',2)
plot(x,y1,'LineWidth',2)
plot(x,y2,'LineWidth',2)
plot(x,y3,'LineWidth',2)
plot(x,yTrue,'LineWidth',2)
legend('T0','T1','T2','T3','f(x) = 2^x')
As far as I know, both marker and legend are not used in this way. But you can use it this way.

类别

Help CenterFile Exchange 中查找有关 Smoothing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by