如何使用循环'for',matlab在同一图形中绘制多个向量

时间:2021-03-23 16:11:39

How I can show in only one figure, several pairs of points x, y ? I've tried everything that I found on google, but in all cases only a single ordered pair of points x,y is shown.

我怎么能只在一个图中显示几个点x,y?我已经尝试了在谷歌上找到的所有内容,但在所有情况下,只显示了一对有序的点x,y。

Thank you.

pws = 2000;
q0EF = 500;
EF_vec = [0.2, 0.5, 0.8, 0.9];
hold all;
for k=1:length(EF_vec)
    if(EF_vec(k) <= 1)
        i = 1;
        clearvars y x;
        for pwfi=0:100:pws
            pwfp = pws - (pws - pwfi )*EF_vec(k);
            y(i) = pwfp;
            x(i) = q0EF * (1 - 0.2*(pwfp/pws) - 0.8*(pwfp/pws)^2 );
            i = i + 1;
        end
        plot(x, y); % this doesnt work. This only show only the lastest x,y values
    end
end

3 个解决方案

#1


Use hold on to append more values onto your plot with multiple plot calls. I see you're using hold all, but that only works when there is a figure open. You have no figure open initially, so you need to do that. Therefore, spawn a new figure, use hold on, then use your code. It's also a good idea to plot all of your points as singular points. plot defaults to joining all of the points together by a straight line, so append a '.' as the third parameter to it:

使用hold on可以在多个绘图调用上将更多值附加到绘图上。我看到你正在使用hold all,但只有在打开数字时才有效。你最初没有打开数字,所以你需要这样做。因此,产生一个新的数字,使用hold on,然后使用你的代码。将所有点作为奇点绘制也是一个好主意。 plot默认以直线连接所有点,因此附加一个'。'作为它的第三个参数:

pws = 2000;
q0EF = 500;
EF_vec = [0.2, 0.5, 0.8, 0.9];
figure; %// Change
hold on;
for k=1:length(EF_vec)
    if(EF_vec(k) <= 1)
        i = 1;
        clearvars y x;
        for pwfi=0:100:pws
            pwfp = pws - (pws - pwfi )*EF_vec(k);
            y(i) = pwfp;
            x(i) = q0EF * (1 - 0.2*(pwfp/pws) - 0.8*(pwfp/pws)^2 );
            i = i + 1;
        end
        plot(x, y, '.'); %// Change
    end
end

#2


You can use plot(x, y, 'x'), which generates a scatter plot and use hold on to plot into the figure you should open before the for loops.

您可以使用plot(x,y,'x')生成散点图,并使用hold on绘制到应在for循环之前打开的图形。

figure;
% looops
plot(x, y, 'x');
hold on

Your code works when I use it but you can't really see all curves as they are too close together. If you zoom in you can see them.

你的代码在我使用时可以工作,但你不能真正看到所有的曲线,因为它们太靠近了。如果你放大你可以看到它们。

#3


Try using a cell array

尝试使用单元格数组

pws = 2000;
q0EF = 500;
EF_vec = [0.2, 0.5, 0.8, 0.9];

X = cell(size(EF_vec));
Y = cell(size(EF_vec));

for k=1:length(EF_vec)
    if(EF_vec(k) <= 1)
        i = 1;
        clearvars y x;
        for pwfi=0:100:pws
            pwfp = pws - (pws - pwfi )*EF_vec(k);
            y(i) = pwfp;
            x(i) = q0EF * (1 - 0.2*(pwfp/pws) - 0.8*(pwfp/pws)^2 );
            i = i + 1;
        end
        X{k} = x;
        Y{k} = y;
    end
end

X(cellfun(@isempty, X)) = [];
Y(cellfun(@isempty, Y)) = [];

plot(X{:}, Y{:});

#1


Use hold on to append more values onto your plot with multiple plot calls. I see you're using hold all, but that only works when there is a figure open. You have no figure open initially, so you need to do that. Therefore, spawn a new figure, use hold on, then use your code. It's also a good idea to plot all of your points as singular points. plot defaults to joining all of the points together by a straight line, so append a '.' as the third parameter to it:

使用hold on可以在多个绘图调用上将更多值附加到绘图上。我看到你正在使用hold all,但只有在打开数字时才有效。你最初没有打开数字,所以你需要这样做。因此,产生一个新的数字,使用hold on,然后使用你的代码。将所有点作为奇点绘制也是一个好主意。 plot默认以直线连接所有点,因此附加一个'。'作为它的第三个参数:

pws = 2000;
q0EF = 500;
EF_vec = [0.2, 0.5, 0.8, 0.9];
figure; %// Change
hold on;
for k=1:length(EF_vec)
    if(EF_vec(k) <= 1)
        i = 1;
        clearvars y x;
        for pwfi=0:100:pws
            pwfp = pws - (pws - pwfi )*EF_vec(k);
            y(i) = pwfp;
            x(i) = q0EF * (1 - 0.2*(pwfp/pws) - 0.8*(pwfp/pws)^2 );
            i = i + 1;
        end
        plot(x, y, '.'); %// Change
    end
end

#2


You can use plot(x, y, 'x'), which generates a scatter plot and use hold on to plot into the figure you should open before the for loops.

您可以使用plot(x,y,'x')生成散点图,并使用hold on绘制到应在for循环之前打开的图形。

figure;
% looops
plot(x, y, 'x');
hold on

Your code works when I use it but you can't really see all curves as they are too close together. If you zoom in you can see them.

你的代码在我使用时可以工作,但你不能真正看到所有的曲线,因为它们太靠近了。如果你放大你可以看到它们。

#3


Try using a cell array

尝试使用单元格数组

pws = 2000;
q0EF = 500;
EF_vec = [0.2, 0.5, 0.8, 0.9];

X = cell(size(EF_vec));
Y = cell(size(EF_vec));

for k=1:length(EF_vec)
    if(EF_vec(k) <= 1)
        i = 1;
        clearvars y x;
        for pwfi=0:100:pws
            pwfp = pws - (pws - pwfi )*EF_vec(k);
            y(i) = pwfp;
            x(i) = q0EF * (1 - 0.2*(pwfp/pws) - 0.8*(pwfp/pws)^2 );
            i = i + 1;
        end
        X{k} = x;
        Y{k} = y;
    end
end

X(cellfun(@isempty, X)) = [];
Y(cellfun(@isempty, Y)) = [];

plot(X{:}, Y{:});