Add world map background to contour plot
32 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jonathan Epperl
2013-2-7
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.
采纳的回答
Jonathan Epperl
2013-2-10
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
- 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.
- 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
- 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)
- 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 个评论
Jonathan Epperl
2013-2-11
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.
更多回答(3 个)
Kostas
2013-2-8
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
t_123
2013-6-4
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 个评论
t_123
2013-6-4
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!