Doesn't Match legend color and plot color
68 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
kerem yaman
el 3 de Abr. de 2022
Comentada: Voss
el 3 de Abr. de 2022
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
-------
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/951409/image.png)
0 comentarios
Respuesta aceptada
Star Strider
el 3 de Abr. de 2022
Editada: Star Strider
el 3 de Abr. de 2022
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 comentarios
Más respuestas (2)
Voss
el 3 de Abr. de 2022
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*
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
2 comentarios
Emrah Can Ucar
el 3 de Abr. de 2022
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.
Ver también
Categorías
Más información sobre Smoothing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!