Main Content

合并线图和针状图

此示例演示如何合并一个线图和两个针状图。然后,显示如何添加标题、坐标轴标签和图例。

创建数据并绘制一条线条。

x = linspace(0,2*pi,60);
a = sin(x);
b = cos(x);
plot(x,a+b)

Figure contains an axes object. The axes object contains an object of type line.

在坐标区中添加两个针状图。使用 hold on 防止新绘图替换现有的绘图。

hold on
stem(x,a)
stem(x,b)
hold off

Figure contains an axes object. The axes object contains 3 objects of type line, stem.

添加标题、坐标轴标签和图例。按照创建绘图的顺序指定图例说明。

title('Linear Combination of Two Functions')
xlabel('Time in \musecs')
ylabel('Magnitude')
legend('a+b','a = sin(x)','b = cos(x)')

Figure contains an axes object. The axes object with title Linear Combination of Two Functions, xlabel Time in mu secs, ylabel Magnitude contains 3 objects of type line, stem. These objects represent a+b, a = sin(x), b = cos(x).

另请参阅

| |