在x轴上的y值以不同的颜色绘制

发布于 2025-01-24 11:36:06 字数 1424 浏览 0 评论 0原文

在这里,我绘制了((2*x。^2+3*exp(-x) - (100*a))。 9*x-42)-10带有输入a = 3dx = 0.1

我想绘制蓝色的所有正y值和红色的所有负y值:

“在此处输入图像说明”

a = input('Please input a: ');
dx = input('Input step size dx: ');
if dx<0
   fprintf('dx must be a non-negative number.');
   while(dx<0)
       dx = input('Input step size dx: ');
   end
end
clf;
x = -10:dx:10;
y = ((2*x.^2+3*exp(-x) -(100*a)).*sin(5*x))./(6*x.^2-9*x-42)-10

plot(x,y)
ylim([-100,100])

绘制红色的蓝色和负面Y-VALUE的正面Y-值,我尝试初始化正Y值及其域的向量,以及负Y值及其域的相同矢量。

s = size(y);
x_1 = [];
x_2 = []; %negative
y_1 = [];
y_2 = []; %negative
for i = 1:s(2)
    if(y(i) >0)
        x_1 = [x_1,x(i)];
        y_1 = [y_1,y(i)];
    elseif (y(i) <0)
        x_2 = [x_2,x(i)];
        y_2 = [y_2,y(i)];
    end
end

s_x1 = size(x_1);
s_x1_lim = s_x1(2);
s_x2 = size(x_2);
s_x2_lim = s_x2(2);

plot(x_1,y_1,'b');
xlim([x_1(1), s_x1_lim]);
hold on
plot(x_2,y_2,'r');
xlim([x_2(1), s_x2_lim]);
hold on;
xlim([-10,10])
ylim([-100,100]);

问题是,这种方法留下了一些重叠,我宁愿不拥有。我该如何更改?

Here I've plotted ((2*x.^2+3*exp(-x) -(100*a)).*sin(5*x))./(6*x.^2-9*x-42)-10 with input a=3, dx=0.1.

I would like to plot all positive y-values in blue and all negative y-values in red:

enter image description here

a = input('Please input a: ');
dx = input('Input step size dx: ');
if dx<0
   fprintf('dx must be a non-negative number.');
   while(dx<0)
       dx = input('Input step size dx: ');
   end
end
clf;
x = -10:dx:10;
y = ((2*x.^2+3*exp(-x) -(100*a)).*sin(5*x))./(6*x.^2-9*x-42)-10

plot(x,y)
ylim([-100,100])

To plot positive y-values in blue and negative y-values in red, I've tried initializing vectors for positive y-values and their domain, and the same vectors for negative y-values and their domain.

s = size(y);
x_1 = [];
x_2 = []; %negative
y_1 = [];
y_2 = []; %negative
for i = 1:s(2)
    if(y(i) >0)
        x_1 = [x_1,x(i)];
        y_1 = [y_1,y(i)];
    elseif (y(i) <0)
        x_2 = [x_2,x(i)];
        y_2 = [y_2,y(i)];
    end
end

s_x1 = size(x_1);
s_x1_lim = s_x1(2);
s_x2 = size(x_2);
s_x2_lim = s_x2(2);

plot(x_1,y_1,'b');
xlim([x_1(1), s_x1_lim]);
hold on
plot(x_2,y_2,'r');
xlim([x_2(1), s_x2_lim]);
hold on;
xlim([-10,10])
ylim([-100,100]);

Problem is, this approach leaves some overlap which I would prefer not to have. How can I change this?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2025-01-31 11:36:06

您可以制作两个数组:neg_x = x; neg_x(neg_x&gt; 0)= nan使用'r'绘图,然后是正值的相反。但是,这将使您有两个阵列之间的零件,即您的差距。您可以通过找到它们并将索引扩展到一个:

x = 0:0.1:6*pi;
y = sin(x);
neg_y = y; neg_y (neg_y>0) = nan;
tmp_y = isnan(neg_y);
idx = find(diff(tmp_y)==1);  % find gaps
neg_y(idx+1) = y(idx+1);  % correct gaps
idx = find(diff(tmp_y)==-1);
neg_y(idx) = y(idx);

pos_y = y; pos_y (pos_y<0) = nan;
plot(x, neg_y,'r');
hold on;
plot(x,pos_y,'b')

使用nan在这里有所帮助,因为MATLAB会在绘图时自动忽略这些条目,即留出一个良好的差距,而不是直线。

结果:

“在此处输入图像描述”

You can make two arrays: neg_x = x; neg_x (neg_x >0) = nan plot using 'r' and then the opposite for the positive values. This will, however, leave you with the pieces between the two arrays, i.e. your gaps. You can correct them by finding them and extending the index by one:

x = 0:0.1:6*pi;
y = sin(x);
neg_y = y; neg_y (neg_y>0) = nan;
tmp_y = isnan(neg_y);
idx = find(diff(tmp_y)==1);  % find gaps
neg_y(idx+1) = y(idx+1);  % correct gaps
idx = find(diff(tmp_y)==-1);
neg_y(idx) = y(idx);

pos_y = y; pos_y (pos_y<0) = nan;
plot(x, neg_y,'r');
hold on;
plot(x,pos_y,'b')

Using nan is a little help here, since MATLAB will automatically ignore those entries when plotting, i.e. leaving a nice gap, rather than a straight line.

Results in:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文