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

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

At the time you posted that reminder, the only areas of the world where it was usual "working hours" was Chatham Islands (population about 600), and Line Islands (including Christmas Island) (total population about 9000). I don't know if there are any MATLAB users in that section of the world.
@Walter thanks for your contributions.I got what you want to say :) Please guide me how to set back ground in the color map. Thanks in advance. :)
I do not have the mapping toolbox. It will be another couple of hours before the volunteers in Western Europe start checking postings; perhaps one of them has the mapping toolbox.
@Walter then request from me to one of them to assist me to resolve my problem. Thanks if in one of them is you.
@Muhammad: I do not understand your question. Which background do you want to set?
Please be kind to Walter. Perhaps he is drinking a cup of tea. He helps voluntarily.
Thanks to reply @Jan . The above shows color map has been created with the 1-1000.text file which i have uploaded with question. I want to show this variations over the country Pakistan which this data represented. Please see my sample_map.png file also . I want to make such kind of map.? shape file of the Pakistan also attached with the question. and i can also share image of pakistan in the case if shape file not sit best for color map. Thanks for understanding my question .
When I say that I do not have the mapping toolbox, I mean that I have never had the mapping toolbox. Anything I might be able to say about it would be from reading the documentation and reading Questions and Answers about it. Learning a new toolbox merely by reading about it at 01:43 in the morning is not something that I have the resources for.
As for me asking other volunteers to assist on this question: there are over 15000 volunteers who have answered at least one question here. I have the email addresses of only about 8 volunteers, none of whom happen to work with the Mapping Toolbox.
Mathworks has approximately 73 toolboxes. I doubt there is anyone on the planet who knows the details of all of them.
it will be me who becomes 1st to know all about 73 toolboxes of matlab. I am future of matlab @Walter :)
There are 20 released versions of MATLAB that still get questions (R14SP1 still gets questions distinct from R14SP3, but not many for either of those; people seem to have stopped asking about R13); to really understand the toolboxes you have to know all the different versions. That multiplier is the killer; it takes the number of distinct products to understand to up over 1000. There will be a test on it; over 200 tests a day on this. At least 8 distinct versions of C and 4 distinct versions of C++ need to be understood as well. Along with Perl; and now 3 distinct versions of Python. Oh and Java, and Javascript, and JPEG and JPEG2000 and TIFF, and several different cryptography algorithms.
@i know the guy who are working from last 5 years on this mathwork site to answer the questions. This guy claims i have not mapping tool box or want to learn. What i say in reply. I do not know. You do not want to help actually.
i am ready to learn all of these complex material and becomes the 1st on the earth to have such capability. People do not want to learn instead try to disappoint others to not chose the path. It will be very difficult. Difficulty is the part of life and i want to bear and gain my goal to become the first.
I do admire the fact that you want to excel in MATLAB. However, I disagree with your comment. People on this community have their own lives and their own preferences. They may learn some toolboxes and not learn the others. Your questions are not their priority and most people will answer questions that interest them. They are simply here to help others as volunteers . The person you mentioned, he probably had no interest in the mapping toolbox or he had no use in learning the toolbox. If you want to make your questions priority, then there are websites available that you can pay and get solutions.
I refer you to http://www.mathworks.com/matlabcentral/answers/1427#comment_295860 except that you are expecting one person to know all of that.
I did not say I am not interested in learning new things: I said that I do not have the resources to learn complete toolboxes from documentation alone at 01:43 in the morning.
I was in at the doctor again yesterday. I was trying to get an assessment on how quickly I am going blind. The doctor didn't have any idea and told me to wait and see for my 6 month post-operation appointment with the specialist. The doctor didn't even listen to my questions about how quickly I am going lame. My physiotherapist ascribes the ruin of my hips to my sitting in front of the computer for long long hours (that is, doing this volunteer technical support.) Which I am doing with only one useful eye.

@Walter Roberson i am sorry. i am a young scientist and can not understand your views. Now i got all. I apologize you , if any of my word was out of manner. Walter , @Image ,@dpi are my ideal scientists and i would like to become one of them in future.

Hat off from this little Newton for you guys. May Almighty Allah(God) gives you health and more success.

Why are you doing such hard working for mankind. People are selfish in nature, you do not know? @Walter

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!)

@Walter thanks for your contributions. Please vote up if my question was interesting :)

Sign in to comment.

 Accepted Answer

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

@Amy Haskins thank you very much for contributions. I make what i understand from you answer
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
h = geoshow(Yi,Xi,Ci,'DisplayType','surface');
h.ZData = zeros(size(h.XData));
p = geoshow('pakistanshapefile\PAK_adm1.shp', ...
'DisplayType','polygon','FaceColor','none','EdgeColor','w')
uistack(p,'bottom')
It has output, lines on image is actually matlab to png conversion
i am getting this error also
C =
[323x1 double] [323x1 double] [323x1 double]
f =
'lat' 'long' 'temp'
??? Attempt to reference field of non-structure array.
Please see that in image x,y are wrong in number and also no background polygon is set? please correct these codes . I shall be very thankful to you on this favor.
Try this instead. You don't need the call to imagesc. If you have a version of MATLAB older than R2014b, then you have to use set and get for graphics properties.
fid = fopen('1-1000.txt');
C = textscan(fid, '%f %f %f')
fclose(fid);
f = {'lat', 'long', 'temp'}
S = cell2struct(C,f,2);
N = 100;
[Xi, Yi] = meshgrid(linspace(60,80,N),linspace(20,40,N));
Ci = griddata(S.long, S.lat, S.temp, Xi, Yi);
f = figure; set(f, 'Renderer', 'painters')
colormap ('hot')
colorbar
h = geoshow(Yi,Xi,Ci,'DisplayType','surface');
set(h,'ZData', zeros(size(get(h,'XData'))));
p = geoshow('pakistanshapefile\PAK_adm1.shp', 'DisplayType','polygon','FaceColor','none','EdgeColor','w');
@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.
@Amy Haskins thank you very much for resolving this problem :) Please vote up my question

Sign in to comment.

More Answers (1)

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

Again thanks @Nitin Khola for your kind contributions. I want to adjust some little more
  1. There is white region which is free from data frame. I want to show only data frame not white space?
  2. Please see the boundary line its format has distortions. I want your suggestions if we use jpg/ tiff image of PAK_admin instead of shapefile , will the boundary edges improve in format? Please test this i have attached jpg image of PAK_admin
3. I want to makes these maps as shown in the sample_map.jpeg (i have attached this) , this consist on 36 maps , three represent for each months
4. I want your suggestion , i have missing values of temperature in text file.I have put them as 0 values. What about the idea if i replace 0 values with NaN(Not A Number) values. In making map we adjust our code such that in map where NaN values occur there no raster surface created( In this way i can tell the user of my map white color show missing data for that geographic region). Please see my sample_map(white color on it shows missing data)?
1. You can take out the white space by changing the axes limits so that the axes limits match those of the colormap. e.g. using "xlim" and "ylim" commands:
xlim([60 80]);
ylim([20 40]);
You can also do it using the Property Editor.
2. I am not sure what is meant by "distortions" here?
3. You can export MATLAB figures to other formats and use an external application to create a a grid of 36 figures.
4. As per the colorbar shown, currently the value zero has black color corresponding to it. You can change this color to white. Follow the steps suggested in this MATLAB Answers post. Note that in your case, you want the color set to white instead.
Hope this helps.
Single irregularity in your data is causing other data to be near 0. As I mentioned in your previous question, if you remove the irregularity, you can get much more evenly distributed heat map. The bright spot is 34.5 67.5 3386.3 in your text file. There is no need to change it to NaN.
@Nitin Khola Thanks for your contributions.
  1. Please see in the image below , this is distortion in the line boundary of the Pakistan i am talking about
2.I want to remove the impact of 0 values from our map, that why i want to replace them with NaN (Not A Number). I want to produce a color map with sample data values not include NaN values. See below the image , I have marked in the image below , Where no color map has created for NaN values.( Color map is created with know values)
3. Is this possible we set the range of color in color bar. My data set has maximum 10000 value. I want to make color bar of interval 500 with color variation light yellow to red for 0 to above 10000 temp? Thanks for understanding my probelms
@Coffee thanks for your contributions. I know about your correction regarding to irregularity. i can not remove that. As i have to make 36 such maps (3 for each months). Please tell me is this possible to make 6 such maps on 1 A4 paper size? also guide me how can i makes my desire color classes and range of class in color bar??
Instead of changing the 0s to NaNs, just change the color limits of the axes.
set(gca,'CLim',[1000,3000])
You can adjust the numbers for find the range that works for your data.
@Nitin Khola , If i want to label features( actually want to label districts in PAK.shp). Kindly guide me how i correct this in 1st line of your code? Thanks in advance for this kind assistance

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!