MATLAB Tip: Logarithmic color-scales for contour and image plots

Logarithmic Color-bar for Contour Plot

A lot of my data ranges orders of magnitude, and can be very hard to depict using standard MATLAB functions like imagesc, contourf, contour, etc.  A quick google search on how to make logarithmic contour plots and logarithmic color-bars yielded some unhelpful results, so I thought I'd give a quick post here.

1) Define Your Contours Define where you'd like contours.  For the plot here, I used:

Contours=[1 3 10 30 100 300 1000];

2) Plot your Data Using imagesc, contourf, or some other function, plot the log of your data. Also be sure to take the log of your defined contours so they show up in the right spot.

contourf(log(Data(:,:)),log(Contours));

3) Define the tick marks on your colorbar The last step is to make the colorbar show the correct data. Use:

colorbar('YTick',log(Contours),'YTickLabel',Contours);
    colormap(jet);
    caxis(log([Contours(1) Contours(length(Contours))]));

4) Make sure the last thing you do is set the Ticks! I've noticed if you have any other commands after this in the colorbar command, it reverts back to something funky.

Example: Correct:

colorbar('FontSize',12,'YTick',log(Contours),'YTickLabel',Contours);

Incorrect:

colorbar('YTick',log(Contours),'YTickLabel',Contours,'FontSize',12);

In a different post, I explain how to freeze colors for different colormaps on the same figure. You might want to check that one out! I hope this is helpful!