How to color a specific value ?
4 次查看(过去 30 天)
显示 更早的评论
%Sine and cos sequence in MATLAB
close all;
clc;
n=-20:20;
x_n=sin(pi/2-2*pi*0.1*n);
plot(n,x_n);
xlabel('Time sample');
ylabel('Amplitude');
hold on;
x2_n=cos(2*pi*0.1*n);
plot(n,x_n);
xlabel('Time sample');
ylabel('Amplitude');
very good moring to all of you.
can anyone help me I want to color positive value in a red and negative value in blue??
2 个评论
Jiri Hajek
2022-12-13
Hi, see this discussion with examples: https://stackoverflow.com/questions/31685078/change-color-of-2d-plot-line-depending-on-3rd-value
回答(1 个)
daniel mitchell
2022-12-13
%Sine and cos sequence in MATLAB
close all;
clc;
n=-20:20;
f = 0.1;
n0 = 1/(4*f)-4/(2*f):1/(2*f):1/(4*f)+3/(2*f);
ns = sort([n n0]);
x_n=sin(pi/2-2*pi*0.1*ns);
x_n = round(x_n,3);
x_n_neg = nan(size(x_n)); x_n_neg(x_n<=0) = x_n(x_n<=0);
x_n_pos = nan(size(x_n)); x_n_pos(x_n>=0) = x_n(x_n>=0);
figure; hold on;
plot(ns,x_n_neg,'r');
plot(ns,x_n_pos,'b');
xlabel('Time sample');
ylabel('Amplitude');
3 个评论
daniel mitchell
2022-12-20
just noticced it now well simply replace plot with area..
%Sine and cos sequence in MATLAB
close all;
clc;
n=-20:20;
f = 0.1;
n0 = 1/(4*f)-4/(2*f):1/(2*f):1/(4*f)+3/(2*f);
ns = sort([n n0]);
x_n=sin(pi/2-2*pi*0.1*ns);
x_n = round(x_n,3);
x_n_neg = nan(size(x_n)); x_n_neg(x_n<=0) = x_n(x_n<=0);
x_n_pos = nan(size(x_n)); x_n_pos(x_n>=0) = x_n(x_n>=0);
figure; hold on;
area(ns,x_n_neg);
area(ns,x_n_pos);
xlabel('Time sample');
ylabel('Amplitude');
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!