Thursday, May 17, 2012

multiple colormaps in matlab

How do I use multiple colormaps in a single matlab figure? This is probably one of the biggest frustrations that almost all matlab users have to deal with. I would argue that this shortcoming of matlab --- combined with the default jet colormap --- have led to an entire generation of ugly scientific figures.

I used to use freezeColors and cbfreeze to make plots and colorbars with different colormaps. However, now that I use export_fig regularly, these do not work. (The incompatibility has to do with different renderers. Export_fig uses the painters renderer (aka. "the good renderer") but freezeColors does not allow the painters renderer to be used.)

My (super awesome) officemates, Nick and Noel, have come up with a solution for this problem. They use the fact that contourf essentially plots separate patch objects for each level of the colormap. They extract the coordinates from contourf and then manually plot each layer as a patch object with a specified color. This way, they avoid using a colormap entirely when they plot the figure. Sounds more complicated than it is. Here's an example of some code that Nick wrote. (Hopefully, this will be turned into a function soon, but for now, you can use this as inspiration.)


cvec=-.5:.5:10.5;
%make a "colormap"
tcmap=flipud(cbrewer('div', 'RdYlBu',length(cvec),'linear'));
%contourf it
[con conhand]=contourf(rgrid/1000,grdDepths(stind:end),TEMP_W_MASK,cvec,'linecolor','none');hold on

%get the patch object handles:
p=get(conhand,'children');
thechild=get(p,'CData');   
cdat=cell2mat(thechild);
%loop through and manually set facecolor of each patch to the colormap you  made:
for i=1:length(cvec)
   set(p(cdat==cvec(i)),'
Facecolor',tcmap(i,:))  
end
%%%%%%%%%%
%now you need to make a fake colorbar using the same trick: 

%define the colorbar axes location just above the subplot you are working with:
lbwh=get(gca,'Position'); 

Tcb=axes('Position',[lbwh(1) (lbwh(2)+lbwh(4)+.005) lbwh(3) .04]);

%contourf your fake colorbar: 

[con conhand]=contourf(cvec',[0 1],[cvec; cvec],cvec,'linecolor','none');
p=get(conhand,'children');
thechild=get(p,'CData');   
cdat=cell2mat(thechild);
% AGAIN, loop through and manually set facecolor of each patch to the colormap you  made (this makes a horizontal colorbar):
for i=1:length(cvec)
   set(p(cdat==cvec(i)),'
Facecolor',tcmap(i,:))  
end
%set the colorbar axes to whatever you want
set(gca,'yticklabel',[],'xaxislocation','top','xtick',[0:3:9])
 
 
I used a variation of this code to make some figures and it worked great. Here's an example:
 

Monday, May 14, 2012

Vaughn's presentation on Dynamic Figures

Thanks so much to Vaughn who gave a super informative presentation about dynamic figures! I'm really looking forward to learning some of the tools that he told us about. Click here to see his presentation, which was made using the same tools one would use to make a dynamic figure. It should just display in your browser and use arrow keys to go from one "slide" to the next.

His main message was to use the following tools:
- JSON for data management
- svg
- coffeescript
- Lots of frameworks such as d3.js

Wednesday, May 9, 2012

Figure club meeting on 10 May: Interactive Figures

Join us on Thursday, 10 May 2012 for our next figure club meeting.  Vaughn Iverson will be talking about making interactive figures on the web and elsewhere.  This should be a great session, so don't miss it. 

4:30 PM, OSB 410 (note different room).

There will be a faculty candidate seminar immediately prior to our meeting (in a different room) and we'll wait to start until after that seminar is over.  Some come to both events!


Saturday, May 5, 2012

When you have a few minutes, be sure to check out chartsnthings, a fascinating blog that documents the creative process of producing New York Times charts and infographics.  A typical post starts with a hand-drawn sketch of the initial idea and goes through the iterations leading to the final product that makes it into the print edition or online.

Wednesday, May 2, 2012

Meeting on Thursday, May 3

Hi everyone,

The figure club will meet on Thursday, May 3 at 4:30pm (immediately following the faculty candidate talk) in OSB 425.

We'll be doing figure critiques. Bring figures that you want to improve. For instance, if you can't decide what colormap to use, print out the figure with multiple colormaps and we can help you pick the best one. If you don't have figures that need critiquing, feel free to come anyway and to give others advice. Don't be intimidated --- the critiques are friendly!

Also, if anyone has "before" and "after" versions of figures they have made, I'd love to post them on this blog! Email them to me.

The following meeting will be on Thursday, May 10 at 4:30. The topic will be poster design.

See you tomorrow!