Wednesday, April 25, 2012

MATLAB tricks

The figure club met today to discuss tricks we use in MATLAB. Here's what we talked about.

(I hope that this discussion can remain ongoing into the future, so if you have MATLAB frustrations, ask your question as a comment here and hopefully someone will have a solution.)

Saving with export_fig
export_fig is a great way to save plots. Way better than "save" or "print" because it is a lot smarter. Personally, I like that it crops my figure. More info on the matlab central file exchange website.

Using dropbox to make matlab code the same on both a home and work computer. (Dan can you elaborate more on this?)

Edit Plot tip
Don't use Edit Plot tool in matlab. It does some funky things so you're way better off to write the code that will plot the figure in its final version.

File types
For vector graphics, save your figures as pdf or eps. For raster graphics, use png or tiff. Do not use jpeg because it compresses the file in a way that's only appropriate for photos and that will make the text in your figure very pixelated. I personally use pdf whenever possible because it preserves the text as text so it can be changed in a program like Illustrator. pdfs are also very robust, in other words, they will appear the same on any computer.

Labeling a colorbar:
ylabel(h,'waterdepth, m')

Multiple colormaps:
freezeColors and cbfreeze are very useful tools from the file exchange.  freezeColors will allow you to have multiple colormaps on one plot and cbfreeze allows multiple colorbars. Use the following syntax:
axis xy
set(gca,'dataaspectratio',[1 cos(48*pi/180) 1]) 
axis xy
set(gca,'dataaspectratio',[1 cos(48*pi/180) 1])
export_fig test.pdf

The end result is a figure that has two different colormaps!

Instead of pcolor: imsc
Dan really likes imsc to plot rather than pcolor or imagesc. Dan can you give more feedback as to why this function is great? Pcolor has the disadvantage that when you try to save it as a vector graphic it comes back with really weird white lines. Not only that, it cuts off a row and column of data. As of now, if you want to use pcolor, you have to save it as a tiff or png to make it appear without the bad rendering.

Default figure settings
You can change the default figure settings by changing the startup file. If you haven't created one already, make a file called startup.m and put it in your matlab folder. This will be executed every time matlab opens. I have defined three things in my startup file: (1) I want the figure command to put a figure window in the upper right hand corner of my screen rather than right in the center, (2) I want the default figure background color to be white, and (3) I want the default font size to be 14 pts.

Here's my startup.m file:
% set the default figure position to be in the upper right
set(0,'defaultFigurePosition',[1353 661 560 420])
% set the default figure color to be white
set(0,'defaultFigureColor',[1 1 1])
% set the default font size to be 14

Other things you may want to set here are the figure orientation (set(0,'orient','landscape')), or the default line or text color.

You can see what your default settings are:

If you don't already use them, structures can be a great way to organize your data. Instead of different variable names for everything, save things as structures. For instance, if I always have structures called ctd and adcp and then within those structures I'll save the variables such as ctd.z, ctd.temp, ctd.salt, adcp.u, adcp.v, adcp.w, etc.

I always like to include a readme file in each structure (if it is data from an instrument). For instance, ctd.readme would tell me what all of the variables are and what their units are. I like to create readme files using the strvcat command:
ctd.readme = strvcat(...
    'CTD data from October 2010 cruise at TTP',...
    'salt = salinity [psu]',...
    'temp = temperature [deg C]');

One of the biggest advantages to a structure is that you can easily save all of the data by just saying:
 save data.mat ctd

However, if you don't want to save the variables that are in a structure as a structure, you can append the command:
save data.mat -struct ctd z temp salt

Sometimes you may need to run through a loop that will plot a lot of data that is all saved in a structure. Let's say you have data from many different moorings called moor1, moor2, moor3, etc. and within each structure there are variables (moor1.temp, moor1.salt, moor1.time, etc). You can plot these using the eval function within a loop:

for i = 1:10
    eval(['plot(moor' num2str(i) '.time,moor' num2str(i) '.salt)'])

Dan pointed out that you could also have a giant structure, where each mooring is an element within the structure moor. Then you could plot just by:

for i = 1:10

Neil Banas gave me nicecolor.m ages ago and I use it all the time. Nicecolor allows you to specify colors by using the matlab abbreviations for colors (r=red, b=blue, etc) instead of specify the rgb vector. It takes an average of all of the colors you specify. So for instance, dark yellow could be 'yk' and orange could be 'ryy'. Use as many letters as you want until you get the color that you want. syntax:

This is a built-in matlab command that is very useful for grabbing x and y data from a plot by eye. It can be used as simply as:
or you can be more specific to get multiple variables:
[x,y] = ginput

If you don't know where you will want to place text in a figure, you can use gtext

If you want your code to ask you before doing something, the input command can be very useful. For instance you can say:
year = input('What year would you like to plot? ')

sometimes I use it so that I don't mistakenly overwrite data:
disp('Did you really want to overwrite that .mat file?')
yn = input('Say ''yes'' or ''no'' ');
if strcmp(yn,'yes') == 1
    save data.mat

better subplots
Matlab often leaves excessive space between subplots. There are two ways to deal with this. The first is easier and the second gives you more control.

packrows, packcols, and packboth allow you to tighten up your subplots after you have already plotted them. I use this when I'm just quickly plotting data. This comes from Jonathan Lilly's plotting toolbox (JLAB), which contains many other useful commands besides this one. syntax:

tight_subplot comes from the matlab file exchange and allows you to specify exactly what you want.
ax = tight_subplot(3,2,0.01)

Good books
The book Modeling Methods for Marine Science by David M. Glover, William J. Jenkins and Scott C. Doney details many statistical methods. The added benefit of this book is that they tie these methods directly to the matlab code that one would use to implement the statistical analysis. This book takes a simpler approach than Emery and Thomson's Data Analysis Methods in Physical Oceanography (which I also recommend.)

Questions that still remain:
  1. Better alternative to plotyy and/or easier way to deal with datetick and plotyy. (Is there an easier way to do this than to specify two axes handles and using the datetick command twice?)
  2. Getting a time axes to align when some subplots need a colorbar and others don't. (I always just plot colorbars that I don't need and delete them or I specify the axes limits manually.)
  3. Saving a discrete colorbar (one with only a few color levels.) It's easy to plot this in matlab, but the save always screws up the colorbar and makes it continuous, even with export_fig.

Monday, April 23, 2012

next meeting: MATLAB TRICKS

The next meeting of the figure design club will be on Wednesday, April 25 from 4-5pm. Sorry for the change of time! It's due to the visiting faculty candidates and the oceanography honors convocation that both take place this Thursday.

In preparation for that meeting, we want to know if you have any MATLAB frustrations. Are you stumped at figuring out how to make your plots look the way you want them to look? Post your questions here as a comment to this post and hopefully someone will have a cool trick to solve your problem when we meet on Wednesday.

Thursday, April 19, 2012

Some useful Matlab colormaps

We talked about several alternative colormaps at figure club last week. Here are links to a few of them on the Matlab File Exchange.

lbmap.m -- four Matlab colormaps from the Light & Bartlein Eos article that Sally wrote about. These colormaps are useful for communicating color to people with colorblindess (and those without, too!).

haxby.m -- a Matlab colormap to implement the Haxby scheme, often used on bathymetric charts.

cbrewer -- tools to implement colormaps from (more info in Sally's post).

cpt-city -- Nick sent us this awesome site with tons of ideas for alternative colormaps and color schemes.

Do you have any great color resources? Leave a comment!

Wednesday, April 18, 2012

Meeting recap: colors in figures

On Thursday, April 12, the figure club met to talk about colors in figures. The topics we covered were: (1) when to use color, or the importance of gray, (2) colors for line plots, (3) colormaps for contour/pcolor plots, and (4) colorblindness and photocopying.

(1) When to use color, or the importance of gray

Here are two examples of figures I made that were originally in color, but I converted them to black and white because my advisor wouldn't let me have any color figures in the paper. (I'm using my own figures because I don't have before and after versions of ones made by other people. If you have an example, I'd love to post it too!)

Obviously, in this case, more than just the colors were changed. I think it's actually easier to see the three different line in black, gray and black dashed than in blue, green and red.

In this case, the black and white vorticity contours are still visible on top of the gray sea surface height contours because the grays are chosen in the middle of the spectrum.

Often it's nice to use color in order to add more excitement to a figure for a presentation or poster, but when you're limited with color for print material, there are often options in black, white and gray. You may be surprised, sometimes the black and white figures look better!

(2) Colors for line plots

How many times have you been listening to a talk and the speaker has apologized that you can't see the lines in their plot? Using cyan, light green and yellow is often disastrous. Similarly, using red and magenta or dark blue and black means that the audience can't see the difference between lines.

The default colors in MATLAB are often not good choices in a presentation because it is too hard to see some of the colors and some of them are too similar as seen below:
This really only leaves blue and red or red and black as options for colors that will show up against each other.

Here an example where there are just too many colors to be able to see the difference between the lines:

 One of the things that can help in a line plot to make the colors more visible is simply to makes the lines thicker. Also, it helps to change the MATLAB default colors so they are darker and more distinguished from each other. 

Here, the colors have the following RGB values:

1. darkred = [127.5 0 0];
2. red = [237 28 36];
3. orange = [241 140 34];
4. yellow = [255 222 23]; 
5. lightgreen = [173 209 54];
6. darkgreen = [8 135 67];
7. lightblue = [71 195 211];
8. darkblue = [33 64 154];
9. purple = [150 100 155];
10. pink = [238 132 181];

(Note that these need to be divided by 255 before using them in MATLAB.) The yellow is probably still not good to use because it's too light and a few people in the meeting complained that they couldn't see the difference between red and dark red. Experiment when you're making your own plots. Often good combinations can be found.

One really cool idea suggests picking colors that vary both the hue and the saturation across the color wheel. (Unfortunately, I lost this link.)

Furthermore, if you're a real color nerd, there are numerous sites where people share their color schemes that they think go well together. These can be an interesting places to find color combos:

(3) Colormaps

A great source for colormaps: cpt-city. Thanks Nick for showing me this! I'll definitely use it in the future when I want colormaps for... well, just about anything. 

Also, is really cool. There they have both divergent and sequential colormaps. There is an accompanying matlab file from the central file exchange.

Here are some examples of sequential, divergent and categorical colormaps:

Usually, anomalies are plotted with divergent colormaps and other variables plotted with sequential ones. However, sometimes it makes most sense to plot temperature with a red/blue differential colormap. Just make sure that the red color corresponds to the warmer colors! Choose a colormap wisely so that it highlights the important parts of the data.

Eleanor Frajka-Williams (one of my figure design gurus) has chosen colormaps that she always uses for the same variables: blue to yellow for salinity, red to blue for temperature, brown to green for oxygen, and light green to dark green for chlorophyll. Here's an example of one of her figures. All of the colormaps are from colorbrewer:

Here's an other example that shows why the jet colormap is so bad. Look at how different it is from the gray colormap. 

Here, we see that in the panel on the left where there is a perfectly linear gradient, the jet colormap makes it look like there are many different "levels." In the second panels, even though the gradient has a jump in the middle, it can't be seen in the jet colormap. The right two panes show other differences in the appearances of the two colormaps. This example comes from a great article by David Borland and Russell M. Taylor II called Rainbow Color Map (Still) Considered Harmful.

Another thing to consider is whether to use a discrete or continuous colormap. See how different the following two examples are even though they have the same color limits.

Finally, Eleanor gave me an example of a very different colormap that is useful for highlighting eddies and small scale variability. 

(4) Colorblindness

When using colormaps like jet, many people who are colorblind have trouble seeing the colors. The following figure shows two colormaps and how they are seen to people with the most common type of colorblindness.

Just another example of why it's good to use other colormaps besides jet. Good colorblind information can be found on the web. There are even sites where you can load in your figures and it will display what the figure will look like to colorblind people. 

Add a label to a colorbar in Matlab

I find myself Googling for this one every time I need to label a colorbar in a Matlab plot.

To add a label to your colorbar:

h = colorbar;
ylabel(h, 'Water depth, m');

We'll cover stuff like this in our next Figure Club meeting on Matlab tips and tricks.

Thursday, April 12, 2012

Meeting recap: The Elements of Graphing Data

On 29 March, we discussed the first and second chapters of Cleveland's The Elements of Graphing Data, which gives much advice on graphing techniques and best practices. Some of the topics we discussed were:
  • Visual clarity, which is the idea that when making a figure, one should strive to make the data stand out and avoid superfluity in your graph. This means things like ensuring data points aren't buried by trend lines, axes, labels, or other forms of clutter.
  • A clear understanding of a graph's purpose, by way of putting the conclusions from which you want the reader to draw in graphical form and making captions comprehensive and informative. A good caption should describe everything that is graphed in the figure, draw attention to the important points, and describe the conclusions to be drawn from the graph.
  • Aspect ratios and the idea of banking to 45 degrees. This is the idea that humans are best able to estimate rates of change in graphical data when the average slope from point to point is 45 degrees. Making your figure dimensions to bring the plotted data close to this is probably a good idea.
  • Scales, and the appropriate choices of tick marks and scales to compare data between graphs.
Some good resources for general scientific figure creation and examples, both good and bad, include:

along with classic books like Tufte's The Visual Display of Quantitative Information and Cleveland's The Elements of Graphing Data.
Creating good scientific figures is difficult even for the best scientists. In Spring 2012, a few oceanographers from the University of Washington decided to start talking about figure design every week with the goal of improving our scientific figures. This blog is a place for design ideas, links to helpful websites, good and bad figure examples, MATLAB tricks, and anything else that might help us design better figures.