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!

  • Diallo Fily

    So happy to find your blog!! I'm student in west africa, and i prapare my M.sc in subject ocean colour!
    your blog gives me a lot aid for my images processing! thank's a lot!!
    Ps: I haven't a good level in english!!

    • mrsoltys

      Great! I'm glad you liked it and found it helpful! Please feel free to ask new questions!

  • Dave Cox

    Sir,
    You are a life saver. Being only an occasional matlab user I always have to keep returning to help files and the web to refresh my memory of how to get things done, and I couldn't work out how to do this, and the other 'help' I'd found weren't very helpful at all. With your help I have just created a surface plot of data covering seven orders of magnitude in z but less than one in x and y and it looks great. Just bit of tidying and it will be finished.

    Many thanks

    Dave

  • Pingback: Tool of the Week: Pretty(er) Matlab Plots | Mike Soltys()

  • bob

    Does this work for surf(X,Y,Z) plotting as well?

    Thx

    • mrsoltys

      I believe it should, but give it a try and let me know!

  • PRS

    Dear Sir,

    This is PRS from India. I have data in excel sheet where the first column in date and remaining columnar are height, u and v wind components. I have plotted the u & v component with height using code and I need to display the date of measurement of these parameters in the plot (let us say 04-19-2013). The format of the date mm/dd/yyyy. Can you please tell me how to read the date from excel sheet and display in plot?

    Thanking you in anticipation.

    Kind Regards,

    PRS

  • PRS, I recently wrote a post about how to import from CSVs, thanks for the suggestion! http://www.mikesoltys.com/2014/04/06/readingwriting-to-csv-in-matlab/

  • Beatriz Recinos

    Hello, your function is great.. but I was just wondering if there is a possibility of doing a color scale like that but with negative values ... my data has negative and positive data therefore I cant use the log function.
    Do you have any idea?

  • Mark Harrison

    Question: How do I now label the contour levels with the original "Contours" values and not the log(Contours) values?