![eb1a2984b48de2ef2eafeb21b07293f3.png](https://i-blog.csdnimg.cn/blog_migrate/15b000ecb96b65476e4e11b20386f9b8.jpeg)
本示例说明如何通过面积图来比较两个数据集。
两个面积图
创建2004年至2008年的销售和费用数据。
years = 2004:2008;
sales = [51.6 82.4 90.8 59.1 47.0];
expenses = [19.3 34.2 61.4 50.5 29.4];
在同一轴上将销售和费用显示为两个单独的面积图。首先,绘制的面积图sales
。通过使用RGB三元组颜色值设置FaceColor
和EdgeColor
属性来更改区域图的颜色。
area(years,sales,'FaceColor',[0.5 0.9 0.6],'EdgeColor',[0 0.5 0.1])
![7aeb9f7a00a451850cee1e1d62a0af3d.png](https://i-blog.csdnimg.cn/blog_migrate/dd290bc2394dc01a5eb651aa32677203.png)
使用hold
命令可防止新图替换现有图。绘制的第二个面积图expenses
。然后,将hold
状态设置回off
。
hold on
area(years,expenses,'FaceColor',[0.7 0.7 0.7],'EdgeColor','k')
hold off
![5d26901688886de088006ad20284e2ef.png](https://i-blog.csdnimg.cn/blog_migrate/fd0004cb1ab66c6dea6aa78555ceac08.png)
添加网格线
沿x轴设置刻度线以对应于整个年份。为每个刻度线绘制一条网格线。通过设置Layer
属性,在面积图的顶部显示网格线。使用点表示法设置属性。
ax = gca; % current axes
ax.XTick = years;
ax.XGrid = 'on';
ax.Layer = 'top';
![eb0dc2ff049d78917073c47bb1f888af.png](https://i-blog.csdnimg.cn/blog_migrate/d25b1c83961d87d980aced65fd2bad36.png)
添加标题,轴标签和图例
添加标题和轴标签。在图上添加图例以指示利润和费用的区域。
title('Profit Margin for 2004 to 2008')
xlabel('Years')
ylabel('Expenses + Profits = Sales in 1000s')
legend('Profits','Expenses')
![eb735f0ed2c8063c0b2921b85511a565.png](https://i-blog.csdnimg.cn/blog_migrate/981c53ddb431c5fd50dc98cdc4d4d0b4.png)
注:本文根据MATLAB官网内容修改而成。
我推荐给你以下的视频教程,特点是没有PPT,不掺水,直接编程环境下的实操课程: 用100分钟了解MATLAB编程
知乎 - 安全中心www.1data.pro《MATLAB编程360》视频课程:
MATLAB编程360 - 网易云课堂study.163.comstudy.163.com