0

我应该在同一张图中显示数据图(曲线)和数据平均值图(平水平线)。我的代码计算一切正常并在图表中显示曲线,但我无法显示水平线。

plot(1:24,hours3,'b-.',1:24, avg3,'r--');

所以“hours3”列的图显示得很好,但“avg3”的平均图根本没有出现。它应该是一条从 x 轴上的点 1 延伸到 24 的平线。有人知道为什么吗?

4

3 回答 3

2

这是因为plot(1:24,avg3,rx--)被解释为 24 个单点。用作线型rx--向您显示点绘制正确但未连接。

plot(1:24,hours3,'b-.',1:24, repmat(avg3,1,24),'r--');

您还hold on可以继续在现有图形中绘图。

于 2015-11-01T00:14:40.517 回答
0

试试下面的代码:

StartPoint=1;
EndPoint=24;
Resolution=1; % you can set 0.1 if you want more precision
Ave=Ave3;% set a value for Ave3

x=[StartPoint:Resolution:EndPoint]; 
NumberofPoints=((EndPoint-StartPoint)/Resolution)+1;
HrzLine=Ave*ones(1,NumberofPoints);

Curve=0.2*x.^3-4.*x.^2+8;

plot(x,HrzLine);
hold on;
plot(x,Curve);
于 2015-11-01T00:38:25.597 回答
0

2018b 版本使添加水平线(和垂直线)变得更加容易yline()xline()对于垂直线)。

Xrng = 1:.01:24;
fh =@(x) 0.2*x.^3 - 4.*x.^2 + 50*sin(x)+ 8;     % curve

plot(Xrng,fh(Xrng))
yline(0)                                        % horizontal line at y = 0

可以很容易地添加后续行。

yline(50)                                       % horizontal line at y = 0
xline(12,'b--')                                 % vertical line at x = 12  (blue, dashed)

需要 MATLAB R2018b 或更高版本。

于 2019-12-17T17:57:04.853 回答