How to color a specific value ?

4 次查看(过去 30 天)
Abubakr Mursi
Abubakr Mursi 2022-12-13
%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
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
Abubakr Mursi
Abubakr Mursi 2022-12-13
Hi,
all i need to color peak and trough values with a color by utilizing (area)

请先登录,再进行评论。

回答(1 个)

daniel mitchell
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
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');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by