現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)