You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to plot pixels on the figure when I have coordinates of the center of each pixel
15 views (last 30 days)
Show older comments
Hello,
I have the coordinates of the center of 93 0.5x0.5 pixels.
I want to plot them like this picture:
Here is my data which contains latitude, longitude, and also value.
Any suggestion or advice is highly appreciated.
Accepted Answer
Ameer Hamza
on 5 May 2020
Edited: Ameer Hamza
on 5 May 2020
Try scatteredInterpolant()
x = points{:,1};
y = points{:,2};
z = points{:,3};
interp_model = scatteredInterpolant(x, y, z);
xg = linspace(min(x), max(x), 100);
yg = linspace(min(y), max(y), 100);
[Xg, Yg] = meshgrid(xg, yg);
Zg = interp_model(Xg, Yg);
pcolor(Xg, Yg, Zg)
or you can add
shading interp
after pcolor() to get a smooth surface
19 Comments
BN
on 5 May 2020
Dear Ameer Hamza,
Thank you so much, but I want to plot only the pixels that I have value for them (I don't want to perform interpolation). Is it possible to plot just latitude and longitude and value that are in the data table as pixels? Sorry, I think the picture I attached caused this misunderstood.
Thanks again
Ameer Hamza
on 5 May 2020
You can try griddata()
x = points{:,1};
y = points{:,2};
z = points{:,3};
xg = linspace(min(x), max(x), 100);
yg = linspace(min(y), max(y), 100);
[Xg, Yg] = meshgrid(xg, yg);
Zg = griddata(x, y, z, Xg, Yg);
pcolor(Xg, Yg, Zg)
BN
on 5 May 2020
Thank you, I'm sorry but the griddata resulted me:
I want to just have pixels where I had coordinates of centers of them. I don't need to have pixels where I hadn't any center coordinates.
I plot my points here(without coressponding value):
I drew a 0.25 x 0.25 box manually on some of them (because each center represents 0.5x0.5 pixel), I want something like this. The other places that I don't have centroid of them good to stay white.
Ameer Hamza
on 5 May 2020
Do you just want to plot as points or rectangles? Do you want the output as an image? What are the width and height of the image. 0.5x0.5 pixel will not be clearly visible?
BN
on 5 May 2020
Edited: BN
on 5 May 2020
Ok, I describe the details:
these points are centroids of the grid. Each grid has 0.25-degree space in top, bottom, right and left (Square).
They present the locations of climate stations and value is mean precipitation.
I will add these pixels to the shapefile of the country after plot them here.
I want plot square 0.25x0.25x0.25x0.25 around each point. Yes, I want the output as an image, I add this figure on the shape of the country afterward.
look like this picture below:
In above picture, all pixels that have corresponding coordinates values are plotted and color represents amount of precipitation in them. And where there is no coordinates for pixels its remains white.
The output is a picture (with regular size like when plotting something) and these pixels (grids) are inside it.
The output that I need is something like the green picture above that I found it on internet.
Thank you very much
Ameer Hamza
on 5 May 2020
Something like this?
x = round(points{:,1});
y = round(points{:,2});
z = points{:,3};
x_range = max(x)-min(x)+1;
y_range = max(y)-min(y)+1;
im = nan(y_range, x_range);
idx = sub2ind(size(im), y-min(y)+1, x-min(x)+1);
im(idx) = z/255;
p = pcolor(im);
p.EdgeColor = 'none';
colormap(summer)
BN
on 5 May 2020
Thank you so much yes that is what I want. Just one little thing is I wanted to put this on my shape file:
But it ploted far away from shape file. Here is my code:
s = shaperead('country_Boundary.shp');
mapshow(s)
hold on
axis equal
x = round(points{:,1});
y = round(points{:,2});
z = points{:,3};
x_range = max(x)-min(x)+1;
y_range = max(y)-min(y)+1;
im = nan(y_range, x_range);
idx = sub2ind(size(im), y-min(y)+1, x-min(x)+1);
im(idx) = z/255;
p = pcolor(im);
p.EdgeColor = 'none';
colormap(summer)
Really thank you again
Ameer Hamza
on 5 May 2020
Try this. Is this correct?
s = shaperead('country_Boundary.shp');
%%
mapshow(s)
hold on
axis equal
ax = gca;
xL = ax.XLim;
yL = ax.YLim;
%%
load data
x = round(points{:,1});
y = round(points{:,2});
z = round(points{:,3});
x_range = ceil(max(x));
y_range = ceil(max(y));
im = nan(y_range, x_range);
idx = sub2ind(size(im), y, x);
im(idx) = z/255;
p = pcolor(im);
p.EdgeColor = 'none';
colormap(summer)
ax.XLim = xL;
ax.YLim = yL;
BN
on 5 May 2020
I'm sorry but this is not correct. The problem is the location of pixels is wrong. Here is my point (center of each pixel) that I plot them on the map (left figure). (right figure is the generated plot using the code)
As you can see there is a difference between locations if I plot a box around each point.
I don't know why this problem arises.Thank you
Ameer Hamza
on 5 May 2020
Yes, the problem is caused by rounding. I have now changed the method to draw these using patch(). Try the following code
s = shaperead('country_Boundary.shp');
%%
mapshow(s)
hold on
axis equal
ax = gca;
xL = ax.XLim;
yL = ax.YLim;
%%
load data
rect_x = [-0.25 -0.25 0.25 0.25];
rect_y = [0.25 -0.25 -0.25 0.25];
x = points{:,1};
y = points{:,2};
z = points{:,3};
num_colors = 100;
clrs = summer(num_colors);
zlim = [min(z) max(z)]+[-0.1 +0.1];
clr_val = @(z) clrs(ceil(interp1(zlim, [0 1], z)*num_colors), :);
for i=1:numel(x)
p(i) = patch(rect_x + x(i), rect_y + y(i), ...
clr_val(z(i)), ...
'EdgeColor', 'none');
end
ax.XLim = xL;
ax.YLim = yL;
BN
on 5 May 2020
Dear Ameer Hamza everything is awesome just I want to have actual z values. In the previous code, you divide it by 255 and I simply deleted that line. But in this code, I don't know how I can use actual z values. So when I add color bar it show real value for each pixel.
Thank you so much.
Ameer Hamza
on 5 May 2020
Can you show what does the colorbar looks like and how do you want to change it?
BN
on 5 May 2020
Edited: BN
on 5 May 2020
Here is color bar. it is between 0 to 1
I want it shows actual values of z like when I read:
z = points{:,3};
in prevous code after I delete /255 line the color bar is:
Which was true. But here it shows wrong values. Since they are precipitation data I don't want to change them and I want to use points{:,3} exact values.
Thanks
Ameer Hamza
on 5 May 2020
Create colorbar using these lines
colorbar
colormap(summer)
caxis([min(z) max(z)])
BN
on 7 May 2020
Dear Ameer Hamza,
I'm sorry but I have another problem here.
When I start comparing the figure from the first model and second model's pixels I found that the values of the color bar are different while the colors of pixels are similar. For example please look at these two pictures, the first one generated for the first model and the second one generated for the next model:
I have a yellow color in both but in the first one, it indicates values near 350 while in second figure yellow pixels show values about 140.
In order to compare, do you think there is any way to make color stable in both?
For example, yellow color in all figures of models shows near 300 values.
Here is an example and I have to compare more than 20 models.
Thank you again for your help.
Ameer Hamza
on 7 May 2020
Yes, It is possible, but that will make the points in some figures to be very similar. For example, If you set the limits of the color axis from 0 to 400 for all figures, then if the points lie between [127 130], they will have a very similar color. Try following code. I set the limits of color axis as [0 400]
s = shaperead('country_Boundary.shp');
%%
mapshow(s)
hold on
axis equal
ax = gca;
xL = ax.XLim;
yL = ax.YLim;
%%
load data
rect_x = [-0.25 -0.25 0.25 0.25];
rect_y = [0.25 -0.25 -0.25 0.25];
x = points{:,1};
y = points{:,2};
z = points{:,3};
num_colors = 200;
clrs = summer(num_colors);
zlim = [0 400];
clr_val = @(z) clrs(ceil(interp1(zlim, [0 1], z)*num_colors), :);
for i=1:numel(x)
p(i) = patch(rect_x + x(i), rect_y + y(i), ...
clr_val(z(i)), ...
'EdgeColor', 'none');
end
ax.XLim = xL;
ax.YLim = yL;
colorbar
colormap(summer)
caxis(zlim)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)