Bar and Area Graphs - MATLAB & Simulink
Bar and Area Graphs - MATLAB & Simulink
12/3/12 6:03 PM
Description Displays columns of m-by-n matrix as m groups of n vertical bars. Displays columns of m-by-n matrix as m groups of n horizontal bars. Displays columns of m-by-n matrix as m groups of n vertical 3-D bars. Displays columns of m-by-n matrix as m groups of n horizontal 3-D bars. Displays vector data as stacked area plots.
Four of these five functions display bar graphs (there is only one type of area graph; see Area Graphs). Bar graphs differ according to whether they plot in 2-D or 3-D and create vertical or horizontal bars, as this table describes.
Grouped Bar Graph By default, a bar graph represents each element in a matrix as one bar. Bars in a 2-D bar graph, created by the
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b Page 1 of 19
12/3/12 6:03 PM
bar function, are distributed along the x-axis, with each element in a column drawn at a different location. All elements in a row are clustered around the same location on the x-axis. For example, define Y as a simple matrix and issue the bar function in its simplest form: Y = [5 2 1 8 7 3 9 8 6 5 5 5 4 3 2]; bar(Y) colormap summer grid on The bars are clustered together by rows and evenly distributed along the x-axis.
Detached 3-D Bars The bar3 function, in its simplest form, draws each element as a separate 3-D block, with the elements of each column distributed along the y-axis. Bars that represent elements in the first column of the matrix are centered at 1 along the x-axis. Bars that represent elements in the last column of the matrix are centered at size(Y,2) along the x-axis. For example, bar3(Y) displays five groups of three bars along the y-axis. Notice that larger bars obscure Y(1,2) and Y(1,3).
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 2 of 19
12/3/12 6:03 PM
By default, bar3 draws detached bars. The statement bar3(Y,'detach') has the same effect. Labeling the Graph. To add axes labels and x tick marks to this bar graph, use these statements: xlabel('X Axis') ylabel('Y Axis') zlabel('Z Axis') set(gca,'XTick',[1 2 3]) Grouped 3-D Bars Cluster the bars from each row beside each other by specifying the argument 'group'. For example: bar3(Y,'group') groups the bars according to row and distributes the clusters evenly along the y-axis.
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 3 of 19
12/3/12 6:03 PM
a desired color to each bar. The typical approach is to associate bar colors with bar heights (y values). The following steps describe one way to do this, first using faceted shading and then using smooth (interpolated) shading: 1. Make up some numbers, plot a default bar plot, and assign a bichromatic colormap: n = 13; Z = rand(n,1); h = bar(Z); colormap(summer(n));
Only the first color is used to color the faces. 2. Assign a new color to each bar. bar (and barh) creates a barseries object, which encapsulates a set of patch objects for the bars. The patches have face-vertex syntax. First get a handle for the children, and then obtain the vertices for the bars and the vertex color data: ch = get(h,'Children'); fvd = get(ch,'Faces'); fvcd = get(ch,'FaceVertexCData'); 3. Sort the data to obtain an index for traversing the Faces array from the lowest to highest bar: [zs, izs] = sortrows(Z,1); 4. Traverse the Faces array and assign colors to the face-vertex color data as you go: for i = 1:n row = izs(i); fvcd(fvd(row,:)) = i; end set(ch,'FaceVertexCData',fvcd)
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 4 of 19
12/3/12 6:03 PM
The code assigns colors to bars based on their YData ranks, rather than on their YData values. This helps to distinguish bars by color, but also the code can assign to bars that are nearly the same height a wider range of colors than if the colors were directly mapped to YData values. 5. To make the graph more readable, you can set different colors for vertices on the baseline and on the top, and then apply interpolated shading to change hue going up the bars. The following code colors the two vertices at the base of each bar using the first color in the colormap, and assigns a color to the two vertices at the top proportionally to bar height. A longer color ramp than was used previously is needed to obtain smooth gradations of shading: k = 128; colormap(summer(k)); shading interp for i = 1:n color = floor(k*i/n); row = izs(i); fvcd(fvd(row,1)) = 1; fvcd(fvd(row,4)) = 1; fvcd(fvd(row,2)) = color; fvcd(fvd(row,3)) = color; end set(ch,'FaceVertexCData', fvcd); set(ch,'EdgeColor','k') % Apply the vertex coloring % Give bars black borders % Assign top vertices color % Interpolate a color index % Look up actual row # in data % Color base vertices 1st index % Number of colors in color table % Expand the previous colormap % Needed to graduate colors
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 5 of 19
12/3/12 6:03 PM
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 6 of 19
12/3/12 6:03 PM
You can then make the plot even more readable by interpolating colors along the bars and giving their EdgeColor a contrasting color. The following code accomplishes this: % Tell handle graphics to use interpolated rather than flat shading shading interp % For each barseries, map its CData to its ZData for i = 1:length(h) zdata = get(h(i),'ZData'); set(h(i),'CData',zdata) % Add back edge color removed by interpolating shading set(h,'EdgeColor','k') end
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 7 of 19
12/3/12 6:03 PM
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 8 of 19
12/3/12 6:03 PM
Horizontal Bar Graphs For horizontal bar graphs, the length of each bar equals the sum of the elements in the row. The length of each segment is equal to the value of its respective element. barh(Y,'stack') grid on set(gca,'Layer','top') % Display gridlines on top of graph
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 9 of 19
12/3/12 6:03 PM
Setting Y-Axis Limits By default, the y-axis range is from 0 to 30. To focus on the temperature range from 15 to 30, change the y-axis limits. set(gca,'YLim',[15 30],'Layer','top')
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 10 of 19
12/3/12 6:03 PM
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 11 of 19
12/3/12 6:03 PM
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 12 of 19
12/3/12 6:03 PM
Overlaying a Line Plot on the Bar Graph 1. To overlay the concentration data on the bar graph, position a second axes at the same location as the first axes, but first save the handle of the first axes: h1 = gca; 2. Create the second axes at the same location before plotting the second data set: h2 = axes('Position',get(h1,'Position')); plot(days,TCE,'LineWidth',3)
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 13 of 19
12/3/12 6:03 PM
3. To ensure that the second axes does not interfere with the first, locate the y-axis on the right side of the axes, make the background transparent, and set the second axes' x tick marks to the empty matrix: set(h2,'YAxisLocation','right','Color','none','XTickLabel',[]) 4. Align the x-axis of both axes and display the grid lines on top of the bars: set(h2,'XLim',get(h1,'XLim'),'Layer','top')
Annotating the Graph. These statements annotate the graph: text(11,380,'Concentration','Rotation',-55,'FontSize',16,... 'Color','Red') ylabel('TCE Concentration (PPM)') title('Bioremediation','FontSize',16)
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 14 of 19
12/3/12 6:03 PM
To print the graph, set the current figure's PaperPositionMode to auto, which ensures the printed output matches the display: set(gcf,'PaperPositionMode','auto')
Area Graphs
The area function displays curves generated from a vector or from separate columns in a matrix. area plots the values in each column of a matrix as a separate curve and fills the area between the curve and the x-axis. Area Graphs Showing Contributing Amounts Area graphs are useful for showing how elements in a vector or matrix contribute to the sum of all elements at a particular x location. By default, area accumulates all values from each row in a matrix and creates a curve from those values. The height of the area graph is the sum of the elements in each row. Each successive curve uses the preceding curve as its base. Using the matrix Y and the area function, display a graph containing three graph areas, one per column: Y = [5 1 2 8 3 7 9 6 8 5 5 5 4 2 3]; harea = area(Y) % This returns handles to three hggroups (areaseries objects) grid on
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 15 of 19
12/3/12 6:03 PM
Change the face color of each layer to make the plot more readable: set(harea(1),'FaceColor',[.5 .8 .9]) set(harea(2),'FaceColor',[.7 .9 .1]) set(harea(3),'FaceColor',[.9 1 1])
Displaying the Grid on Top. To display the grid lines in the foreground of the area graph and display only five grid lines along the x-axis, use the statements
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 16 of 19
12/3/12 6:03 PM
set(gca,'Layer','top') set(gca,'XTick',1:5)
12/3/12 6:03 PM
You need to issue the command hold on to prevent the second graph from erasing the first one, and to turn hold off afterward because no more graphs will be plotted in the axes. The graph looks like this.
5. Make the x-ticks correspond to whole years and draw grid lines on top of the area graphs: set(gca,'XTick',x) set(gca,'XGrid','on') set(gca,'Layer','top')
6. Annotate the graph interactively, using the gtext function. It accepts a string to be placed as text annotation, and enters graphic input mode. Position the cross-hair cursor where you want the lower-left corner of the text
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 18 of 19
12/3/12 6:03 PM
to be, and click the mouse button to complete the command. Execute the following statements to add three interactive labels and two axis labels: disp('Click blue line to label Sales') gtext('\leftarrow Sales') disp('Click yellow line to label Expenses') gtext('\leftarrow Expenses') disp('Click green area to label Profits') gtext('Profits') xlabel('Years','FontSize',14) ylabel('Expenses + Profits = Sales in 1,000''s','FontSize',14)
Yes
No
http://www.mathworks.com/help/matlab/creating_plots/bar-and-area-graphs.html?s_tid=doc_12b
Page 19 of 19