how to set background of the color map with shape file/image in hand

22 views (last 30 days)
Hi every one; I want to make color map from text file which contains three columns( 1st is latitude, 2nd is longitude, 3rd is temperature). I have accessed to this code which reading and creating color map from text file(1-1000.text)
fid = fopen('1-1000.txt');
C = textscan(fid, '%f %f %f')
fclose(fid);
f = {'lat', 'long', 'temp'}
S = cell2struct(C,f,2);
N = 1000;
[Xi, Yi] = meshgrid(linspace(60,80,N),linspace(20,40,N));
Ci = griddata(S.long, S.lat, S.temp, Xi, Yi);
colormap ('hot')
imagesc(linspace(60,80,N),linspace(20,40,N),Ci);
xlabel('Longitude');
ylabel('Latitude');
colorbar
it has output
The data in text file represent region , i have shape file of that region i want to set this region as the background of this color map. I have attached with this post text data, my requried output map and shape file of the region. Please set my code so that background of the map will become region of my shapefile. Thanks in advance for assistance. Regards
  15 Comments
Walter Roberson
Walter Roberson on 24 Jul 2015

Why do this work? Different people have expressed it different ways. "All things whatsoever ye would that men should do to you, do ye even so to them." "From each according to their abilities; to each according to their needs". "Be the change that you wish to see in the world." "We then that are strong ought to bear the infirmities of the weak, and not to please ourselves."

Decades ago, a successful consulting company that I was working for as a student offered to hire me upon graduation, to work on their business consulting side, suggesting that I would likely rise rapidly in the organization. They were pleasant people to work with, but I turned them down without hesitation, and switched to working for a technology company instead. I could see that there were a lot of people around who could do a perfectly good job of working on business contracts; the business field didn't need me. On the other hand, at the time there were few people who could handle the combination of mathematics and science and computing: scientific computing did need me, and I could see that my best contribution to the world would be on the side of science and technology rather than business. It is a decision that I have not had reason to regret (though parts of it could have been more pleasant!)

Muhammad Usman Saleem
Muhammad Usman Saleem on 24 Jul 2015
@Walter thanks for your contributions. Please vote up if my question was interesting :)

Sign in to comment.

Accepted Answer

Amy Haskins
Amy Haskins on 21 Jul 2015
Once you've gotten your data into a regular grid of latitudes and longitudes as you've done above, the geoshow function in the Mapping Toolbox can help with the rest.
First use geoshow to get your surface plot into a map axes. If you don't already have a map open, geoshow will create a default one. If you would perfer a different projection, take a look at the doc for axesm.
h = geoshow(Yi,Xi,Ci,'DisplayType','surface');
This creates an actual surface plot, but you're after a 2D map, so let's flatten out the ZData.
h.ZData = zeros(size(h.XData));
Your shape file contains polygons. You have a couple of options here. I think it looks nice to overlay just the edge lines, but you could play with the transparency and fill in the polygons.
p = geoshow('pakistanshapefile\PAK_adm1.shp', ...
'DisplayType','polygon','FaceColor','none','EdgeColor','w')
Or
p = geoshow('pakistanshapefile\PAK_adm1.shp',...
'DisplayType','polygon','FaceAlpha',0.25)
Alternately, you could move the polygons to the bottom using uistack(p,'bottom') and change the FaceAlpha property of the surface if you really want the shape file in the background.
  4 Comments
Muhammad Usman Saleem
Muhammad Usman Saleem on 21 Jul 2015
@Amy Haskins thanks for your kind contributions.Please see my reply to @Nitin Khola in the above answer(last two reply)? How can i resolve that problem. Thanks in advance for your kind contributions.
Muhammad Usman Saleem
Muhammad Usman Saleem on 24 Jul 2015
Edited: Muhammad Usman Saleem on 24 Jul 2015
@Amy Haskins thank you very much for resolving this problem :) Please vote up my question

Sign in to comment.

More Answers (1)

Nitin Khola
Nitin Khola on 21 Jul 2015
As per my understanding, you wish to set the background of the given colormap to a map outline imported from a shapefile. I am assuming you have a license for the MAPPING TOOLBOX and are using MATLAB R2015a. To achieve it, as per this assumption, you can use the function "shaperead":
to import the shapefile and then overlay the given colormap on a plot of data from the shapefile. The transparency of the colormap can be changed using "alpha" command so that the outline is visible. I was able to do it using the following code:
im = shaperead('PAK_adm1.shp'); % import the shapefile
plot([im.X],[im.Y],'k','LineWidth',2.0); % plot X,Y data
hold on;
fid = fopen('1-1000.txt');
C = textscan(fid, '%f %f %f');
fclose(fid);
f = {'lat', 'long', 'temp'};
S = cell2struct(C,f,2);
N = 1000;
[Xi, Yi] = meshgrid(linspace(60,80,N),linspace(20,40,N));
Ci = griddata(S.long, S.lat, S.temp, Xi, Yi);
colormap ('hot')
imagesc(linspace(60,80,N),linspace(20,40,N),Ci);
xlabel('Longitude');
ylabel('Latitude');
colorbar
alpha(0.85) % Change transparency of the colormap so that map is visible
You might want to change some of the values in the above code as per your needs.
  9 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!