Generating .gifs from Matlab Figures

If your results have some form of time dependence, you would probably like to see them in motion. Movie file formats such as .avi, .mpg, etc. can be bulky, low-quality, and unreliable for presentations. Graphics Interchange Files (.gif) are a comparatively lightweight, cross-platform alternative. A .gif file is basically just a sequence of images stitched together, and Matlab can perform this stitching for you.

(Disclaimer: Matlab can also generate movies in .avi/.mpg format, but I tend to avoid that for the reasons mentioned above. Perhaps someone can comment on the “dynamic” capabilities of Matplotlib and R — these might be nice alternatives, but I’ve never used them).

For a simple example, consider a wave function sin(wt + kx). You can make a time-varying .gif of this function using the following code:

x = 0:0.01:2*pi;
omega = 0.5;
k = 1;

for t=1:50

    plot(x, sin(omega*t + k*x), 'linewidth', 2, 'color', 'red');
    ylim([-1 1]);
    xlim([0 2*pi]);
    grid on;

    % gif utilities
    set(gcf,'color','w'); % set figure background to white
    drawnow;
    frame = getframe(1);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);
    outfile = 'sinewave.gif';

    % On the first loop, create the file. In subsequent loops, append.
    if t==1
        imwrite(imind,cm,outfile,'gif','DelayTime',0,'loopcount',inf);
    else
        imwrite(imind,cm,outfile,'gif','DelayTime',0,'writemode','append');
    end

end

This will produce the following (if the image appears static, you may need to click it to open in a new tab):

sinewave

From this example, you can see how a movie of any figure can be created fairly easily. Just stick the plotting commands inside a loop, and at the end of each iteration, append to the .gif file.

If you’d like to see a more compelling example, I’ve been using this to generate .gifs of time-varying sensitivity analysis for a watershed. In the figure below, you can see how the spatially distributed cell sensitivity (top-left) changes with time. The top-right panel shows precipitation, and the bottom panel shows the overall time period with a red line to denote the current time.

movie-event-1

(Again, you may need to click the image to see the animation). After large rainfall events, sensitivity is concentrated at the watershed outlet (bottom). Over time, it propagates up toward the headwaters. This type of thing is very difficult to convey with static figures, but it’s much more intuitive with a .gif. I hope you find this useful, and thanks for reading.

Advertisements

Colorbrewer: Color palettes for your figures

If you’re ever trying to choose a nice-looking color palette for figures, you may want to try ColorBrewer, a tool developed by Penn State geography professor Cynthia Brewer. (Several of you are likely already using this). The color schemes are designed for maps, but will look equally good on other types of plots too. You can choose between sequential, diverging, or quantitative colors depending on your application. When you have your colors chosen, you can click the little tab that says “Export your Colors” near the bottom of the map, then copy and paste the RGB values into whatever application you’re using.

But wait, you say, I would rather have an automatic way to import these color schemes without going to a website every time. Fortunately there is a package on the Matlab file exchange that can do exactly that. Just include the “cbrewer” directory on your path, and grab a colormap of your choice by calling the function “cbrewer”. The nice thing about this is that you’re not limited to 8 colors like the web interface–in theory, you can make a nearly continuous color scale by requesting, say, 256 colors. Happy designing!