MATLAB: Add world map background to contour plot

figure scalingmap

Hello,
I'm pretty new to MATLAB, & have the basic 2012b version with no toolboxes.
I have an outline map of the USA in .gif format (759×381 pixels), which I can display. I also have a contourf plot of temperature for the same latitudes/longitudes as the USA map, which I can also display.
I'd like to plot the contourf on top of the USA map, and have the colors of the contourf be semi-transparent so the map below can be seen.
The problem I'm running into is that I can't get them the same size. If I do the map first, the contourf just fills a tiny portion in the upper left of the figure. If I do the contourf first, only the very upper left portion of the map is plotted.
I found a function that makes the USA map gif smaller by scaling, however this causes an unacceptable loss of resolution. So, I'd like to know how to make the contourf plot bigger so it's the same dimensions as the USA map (759*381 pixels).
Thanks you for any help you can give!!

Best Answer

I hope you're still interested in an answer to this question, here's what I can offer:
lon = 30:2.5:150;
lat = 55:-2.5:-5; lat = lat';
%%%Example data
[LO LA] = meshgrid(lon, lat);
da_grid = LO.^2.*LA;
cfig = figure('Position', [100, 100, 1400, 800]);
da_map = './new-york-county-map.gif';
[I,Imap] = imread(da_map);
% This converts your image with indexed colors to a true color image
Itc = ind2rgb(I,Imap);
% if you invoke image() like that it won't reverse axis direction etc.
ih = image('XData',lon([1 end]),'YData',lat([1 end]),'CData',Itc);
hold on;
% Now the contours, save the handles the function supplies
[c,ch] = contourf(lon, lat, da_grid,10);
set(gca, 'Ytick', -5:5:55);
set(gca, 'xtick', 30:5:150);
colorbar; title('Wind Speed (m/s)');
chf = findobj(ch,'-property','FaceAlpha');
set(chf,'FaceAlpha',.3)
yl = ylabel('Latitude (degrees)');
xl = xlabel('Longitude (degrees)');
hold off;
The crucial steps are really
  1. Convert your gif image to true color, otherwise it will have to share the color map with the contours -- there is only one colormap per figure.
  2. By invoking image the way I did it'll place the pixels so that the wanted region is filled with the image. doc image if you want to know more
  3. chf = findobj(ch,'-property','FaceAlpha'); finds all the Children of your contour that have a 'FaceAlpha' property, which you then set to a value less than 1 to make them transparent: set(chf,'FaceAlpha',.3)
  4. Don't forget to hold on
If you have too many contours, then you will notice that the ones corresponding to the higher levels will seem more opaque. That is because unfortunately, contourf draws patches that overlap, the one for the highest level is just on top of everything else. If that's a problem we can think of solutions.
Lastly, I have the bug where all of a sudden, all labels are inverted, if you have that problem, see http://www.mathworks.com/matlabcentral/answers/30925
Related Question