Add world map background to contour plot

29 views (last 30 days)
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 (759x381 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!!
  1 Comment
Jonathan Epperl
Jonathan Epperl on 7 Feb 2013
Please show us the code you have so far, it's a lot harder to help if as a first step we have to figure out what you have done.

Sign in to comment.

Accepted Answer

Jonathan Epperl
Jonathan Epperl on 10 Feb 2013
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
  3 Comments
Jonathan Epperl
Jonathan Epperl on 11 Feb 2013
As a first remedy, you should do, what you were doing, and use the same Alpha value as you have used for the contours. This way it is easier than using findobj:
cb = colorbar;
cb1 = get(cb,'Children')
set(cb1,'AlphaData',.3)
For me, that looks pretty good for the lower contour levels, but not so much for the higher one. I think the effect we are seeing is what I hinted at in my answer: The contours are layered on top of each other, and so if you make them transparent, the ones that are below a given contour will shine through and the colors will mix. The only remedies I can think of for that are going to be a lot of work, so maybe here's a thought:
Instead of a colorbar, label the contours using clabel. Insert this code between the call to contourf and the alpha-business (otherwise, for whatever reason, the alphas will be reset to 1)
cl = clabel(c,ch,'FontSize',18,'BackgroundColor','w');
for i=cl'
istring = get(i,'String');
set(i,'String',[istring(:)', 'K']);
end
You should tweak the appearance to your liking, and the for-loop will allow you to add the unit Kelvins, or whatever you'd like, to the labels.
John
John on 13 Feb 2013
I think I'll run the charts with colorbar and again with clabel and see which one our customer likes best. I think with what you've given me I can take it from here. Thanks again for your help!!

Sign in to comment.

More Answers (3)

John
John on 7 Feb 2013
My map/data are actually for a portion of the mid-east..
Here's what I have for plotting the data. The matrix 'da_grid' is just temperature data in columns and rows:
lon = 30:2.5:150; lat = 55:-2.5:-5; lat = lat'; FigHandle = figure('Position', [100, 100, 1400, 800]); contourf(lon, lat, da_grid); set(gca, 'Ytick', -5:5:55); set(gca, 'xtick', 30:5:150); colorbar; title('Wind Speed (m/s)'); ylabel('Latitude (degrees)'); xlabel('Longitude (degrees)');
Here's what I do to show the map (the_map.gif). I had to do the colormap because it wanted to make the gif shades of red, so this makes it black/white:
da_map = 'c:\data2\the_map.gif' I=imread(da_map); imagesc(I); map=[0 0 0; 1 1 1]; colormap(map); set(gca, 'xtick', []); set(gca, 'ytick', []);
So the data plotting gives me a good figure of the temperature contour, and the map portion gives me a good figure of the map, so now I want to overlay the contour on the map in one figure as in a standard weather chart.
Thanks again!

Kostas
Kostas on 8 Feb 2013
If you had the Map toolbox, that would be quite easy to make it. Now i would suggest you to google for the M_Map: A mapping package for Matlab and see the examples there
  1 Comment
John
John on 8 Feb 2013
Thank you, but unfortunately we are not allowed to download programs from the internet..

Sign in to comment.


t_123
t_123 on 4 Jun 2013
hey i have the same doubt ..i have to plot temp climatological data and to plot it in world map..i have converted both the images on jpg format..but dnt knw how to plot it together.pls help
  1 Comment
t_123
t_123 on 4 Jun 2013
bcoz when m plotting its showing the error.. ??? Index exceeds matrix dimensions.
Error in ==> ind2rgb at 27 r = zeros(size(a)); r(:) = cm(a,1);
Error in ==> worldmap at 15 RGB = ind2rgb(I,Imap) and m using the same code u given above

Sign in to comment.

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!