This example shows how to compare two data sets by overlaying their area graphs.
|On this page…|
Define the sales and expenses data from the years 2004 to 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];
Use the area function to display sales and expenses as two separate area graphs in the same axes. First, create an area graph of sales. Change the color of the area graph by setting the FaceColor and EdgeColor properties using RGB color values.
figure area(years,sales,'FaceColor',[0.5,0.9,0.6],... 'EdgeColor',[0,0.5,0.1])
Use the hold command to prevent a new graph from replacing the area graph of sales. Create a second area graph of expenses and change the color of this graph by specifying the FaceColor and EdgeColor properties. Set the hold state to off.
hold on area(years,expenses,'FaceColor',[0.7,0.7,0.7],... 'EdgeColor','k') hold off
Set the tick marks along the x-axis to correspond to whole years. Draw a grid line for each tick mark by setting the XGrid property to on. Display the grid lines on top of the area graphs by setting the Layer property to top.
set(gca,'XTick',years,... 'XGrid','on',... 'Layer','top')
Give the figure the title, "Profit Margin for 2004 to 2008". Add axis labels for sales and years. Add a legend to the graph to indicate the areas of profits and expenses.
title('Profit Margin for 2004 to 2008') xlabel('Years') ylabel('Expenses + Profits = Sales in 1000s') legend('Profits','Expenses')