이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to plot pixels on the figure when I have coordinates of the center of each pixel
조회 수: 15 (최근 30일)
이전 댓글 표시
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.
채택된 답변
Ameer Hamza
2020년 5월 5일
편집: Ameer Hamza
2020년 5월 5일
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
BN
2020년 5월 5일
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
2020년 5월 5일
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
2020년 5월 5일
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
2020년 5월 5일
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
2020년 5월 5일
편집: BN
2020년 5월 5일
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
2020년 5월 5일
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
2020년 5월 5일
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
2020년 5월 5일
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
2020년 5월 5일
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
2020년 5월 5일
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
2020년 5월 5일
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
2020년 5월 5일
Can you show what does the colorbar looks like and how do you want to change it?
BN
2020년 5월 5일
편집: BN
2020년 5월 5일
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
2020년 5월 5일
Create colorbar using these lines
colorbar
colormap(summer)
caxis([min(z) max(z)])
BN
2020년 5월 7일
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
2020년 5월 7일
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)
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)