Code Sample: Stacked Bars and Lines in Matlab

I needed to make a plot that superimposes a “stacked” bar graph with a line in Matlab.  I got started by referencing this post on Matlab central, which used “function handles” to pass plotting commands into plotyy.  This got me thinking about some other Matlab features that are explained in the following code sample.  Hope this helps in your work too!

The data is from the USGS’s estimate of U.S. Water Use for 2005.


%How to plot data from USGS, Estimated Water Use in the U.S. in 2005
%as a stacked bar/line graph in Matlab.

%Specifically, we want the US population as a line, and the different
%water use types as stacked bars.

%The data, where each data value is for a different 5 year period
population = [151, 164, 180, 194, 206, 216, 230, 242, 252, 267, 285, 301];
totalwithdrawal = [180, 240, 270, 310, 370, 420, 430, 397, 404, 399, 413, 410];
public = [14, 17, 21, 24, 27, 29, 33, 36.4, 38.8, 40.2, 43.2, 44.2];
irrigation = [89, 110, 110, 120, 130, 140, 150, 135, 134, 130, 139, 128];
thermo = [40, 72, 100, 130, 170, 200, 210, 187, 194, 190, 195, 201];

%Place the data in a matrix, where each row is a
%different data type (i.e., population)
data = [thermo; irrigation; public;
 totalwithdrawal-(public+irrigation+thermo)];

%Now, flip the data so each data type has its own column
data = data';

%To use different data types in the plotyy environment, you
%can use Matlab's 'anonymous function' feature. Stackedbar
%and prettyline below are temporary functions you can only
%use inside of this script.
stackedbar = @(x, A) bar(x, A, 'stack');
prettyline = @(x, y) plot(x, y, 'k', 'LineWidth', 5);

%There's a version of plotyy that accepts function handles as an
%argument. We'll pass stackedbar and prettyline in there as the
%last arguments to this function.
[ax, h1, h2] = plotyy(1950:5:2005, data, 1950:5:2005, population, stackedbar, prettyline);

%You'll notice the line and bar axes are not agreeing with each other.
%We can fix this using the ax variable created above.
set(ax(1), 'XLim', [1945 2010]);
set(ax(2), 'XLim', [1945 2010]);

%Get rid of one of the sets of ticks, to make sure
%the figure looks nice.
set(ax(2), 'XTick', []);

%Changing the colormap will change the colors of the
%stacked bars. For example:
colormap summer;

%Finally write the legend. Note that, for whatever reason,
%the line is first in the list of legend entries.
legend('Population', 'Thermoelectric Power', 'Irrigation', 'Public Supply', 'Other', 'Location', 'NorthWest');

Here’s the result!

 

Advertisements

AeroVis: Reproducing the DTLZ1 Animation

As I’m working to reproduce Josh’s DTLZ1 Animation that he demoed in the group meeting last week, I thought I’d write a list of my workflow here on the blog.

  1. Load the DTLZ1.par file from the examples folder on ANGEL
  2. In Edit… Plotting Preferences, go to the Glyph tab, and change the size of the glyphs to something like 0.02.  You just want to change the size so you can see the points better.
  3. To get the axes to scale the correct way…first fast-forward the animation to the end using the animation controls.
  4. Then, open “Central Variable Controls”, and reset each axis to its natural scale.  Lock the scaling of the axes so they don’t change by clicking “lock” on each axis.
  5. “Rewind” the animation to the beginning.
  6. In animation tools, find the pulldown menu for “Sync By”.  Change the sync by option to NFE.  Then, type 25000 in the “skip to” box.  Any time you change something in a menu box, be sure to hit Enter on your keyboard to accept the command.  This will fast-forward the animation to a point where you see some solutions appearing in the glyph cube.
  7. The next step will rotate the cube to show a little bit of depth for 3d.  You’ll want to think about a starting and an ending point that you desire.  Then, move the cube to the starting point.
  8. Take a snapshot using the “take snapshot” tool.
  9. Without moving the mouse, set an interpolation camera in “gif controls.”
  10. Now move carefully to the second view that you want.  This will be the one you keep for the rest of the animation.  Set a second interpolation camera.
  11. We’re ready to start the first animation!  Click the red “record” button in GIF controls.  Then, click the “interpolate spline” button in GIF controls.  Finally, click “create GIF”
  12. Now the window should be at its final view for the animation of the actual MOEA search.  Click “clear keyframes” in the GIF controls.  Also make sure the red record button is still depressed.  Finally, set the “frame skip” in GIF controls to 8, and hit enter.
  13. Now hit play in “Animation Controls”.  You should see the Image Stack amount increasing in GIF controls.
  14. You can “pause” to stop the animation at any time.  When complete, click “Create GIF” in the GIF controls to complete.

The animation files were created successfully!  Here’s a short list of tasks in PowerPoint then to complete the exercise:

  1. If you wish, make your first slide with introductory points using the static version of the figure, in your Snapshots folder.  The subsequent animations can be placed on different slides.
  2. In Image Tools in PowerPoint, make sure that each figure (the static one and the two animations) are exactly the same size.  For example, round off the width of the figure to a round number, and the height should follow.
  3. Then, click in the bottom right corner of Image Tools to get to the size and position dialog box.  In position, make each figure have the exact same horizontal and vertical position.  This will ensure that the figures don’t “jump around” on screen between slides.

And that’s it!