首页 > 解决方案 > 绘图功能线规范

问题描述

我正在尝试在 MATLAB 中使用直线绘制图形;但是,我只能使用圆点打印它。

我尝试用“r-”和其他不同的解决方案更改“ro-”,但没有任何效果。使用“r-”时,它不会打印任何内容。

这是我的代码:

for T = temp
  figure(i)
  for xb = linspace (0,1,10)
    xt = 1-xb;
    Pb = 10^(6.89272 - (1203.531/(T+219.888)));
    Pt = 10^(6.95805 - (1346.773/(T+219.693)));
    Ptot = Pb*xb + Pt*xt;
    yb = (Pb*xb)/Ptot;
    plot(xb, Ptot, 'bo-.')
    hold on
    plot(yb, Ptot, 'ro-')
  end
  i = i + 1;
  saveas(gcf, filename, 'png')
end

这就是我得到的:

在此处输入图像描述

这就是我要的:

在此处输入图像描述

我怎样才能用线条制作这个情节?

标签: matlabplot

解决方案


要绘制一条线,该plot命令必须在一次函数调用中获取沿该线的所有点。重复调用plot将向图形添加新行,在这种情况下,每行都由一个点组成。简而言之,MATLAB 不知道您想连接这些点。

那么如何在一个数组中获取所有数据点呢?只需将它们存储在您的循环中:

T = 70;
xb = linspace(0,1,10);
Plot = zeros(size(xb)); % preallocate output array
yb = zeros(size(xb));   % preallocate output array
for ii=1:numel(xb)
  xt = 1-xb(ii);
  Pb = 10^(6.89272 - (1203.531/(T+219.888)));
  Pt = 10^(6.95805 - (1346.773/(T+219.693)));
  Ptot(ii) = Pb*xb(ii) + Pt*xt;
  yb(ii) = (Pb*xb(ii))/Ptot(ii);
end
figure
plot(xb, Ptot, 'b-')
hold on
plot(yb, Ptot, 'r--')

但实际上完全没有循环更容易做到这一点:

T = 70;
xb = linspace(0,1,10);
xt = 1-xb;
Pb = 10^(6.89272 - (1203.531/(T+219.888)));
Pt = 10^(6.95805 - (1346.773/(T+219.693)));
Ptot = Pb*xb + Pt*xt;
yb = (Pb*xb)./Ptot;    % note ./ is the element-wise division
figure
plot(xb, Ptot, 'b-')
hold on
plot(yb, Ptot, 'r--')

推荐阅读