平滑边缘等高线图
Smooth edge contour plot
你好
我想用 2D 格式的 2 个变量(纬度和经度)表示数据。该值由颜色表示,2 个变量作为 2 轴,我正在使用 contourf 函数绘制我的数据。所有数据都来自一个 xlsx 文件,我把它放在一个矩阵中。
Locations = xlsread('Availability results.xlsx');
column_numberloc = 1; % Column in the locations file containing the number of the locations
column_latitude = 2; % Column in the locations file containing the latitude of the locations
column_longitude = 3; % Column in the locations file containing the longitude of the locations
column_availability = 4; % Column in the locations file containing the availability of the locations
min_latitude = min(Locations(:,column_latitude));
max_latitude = max(Locations(:,column_latitude));
min_longitude = min(Locations(:,column_longitude));
max_longitude = max(Locations(:,column_longitude));
max_availability = max(Locations(:,column_availability));
min_availability = min(Locations(:,column_availability));
longitude = Locations(:,column_longitude);
latitude = Locations(:,column_latitude);
Contour = zeros(23,17);
for numerofile=1:204
[coord_x,coord_y] =transformation(Locations(numerofile,column_latitude),Locations(numerofile,column_longitude));
Contour(coord_x,coord_y) = Locations(numerofile,column_availability);
end
for i=1:23
for j=1:17
if Contour(i,j) == 0
Contour(i,j) = NaN;
end
end
end
cMap=jet(256);
figure(1);
x = linspace(min_longitude,max_longitude,17);
y = linspace(min_latitude,max_latitude,23);
newpoints = 100;
[xq,yq] = meshgrid(linspace(min(x),max(x),newpoints),linspace(min(y),max(y),newpoints ));
Contourq = interp2(x,y,Contour,xq,yq,'linear',max_availability);
[c,h]=contourf(xq,yq,Contourq,100);
%[c,h]=contourf(x,y,Contour,50);
set(h, 'edgecolor','none');
colormap(cMap);
cb=colorbar;
caxis([min_availability max_availability]);
transformation 函数允许我将所有数据放在 Contour 矩阵中,因为它将经度和纬度与行和列相关联。
我为每个等于零的数据设置了一个 NaN 以便更好地查看我的数据,我得到了这个:
interpolation_linear
很好,但我希望此数据接近于:
Without interpolation
所以,我决定 将线性插值更改为 'nearest' 插值 ,我得到了这个:
interpolation_nearest
我可以看到更多数据,但等高线图不如线性插值那样平滑。
我看过很多关于如何制作平滑等高线图的帖子(这就是我找到函数 'interp2' 的方式)但我认为我的问题来自 NaN 数据,它阻止我获得平滑等高线在 NaN 值和其余值之间的边缘绘制,如第一张图像,但有足够的数据,如第三张图像。
我的问题是:你知道如何通过最近的插值获得具有足够数据的平滑边缘轮廓图,但具有像第一张图像一样漂亮的视觉效果?
非常感谢
由于您是在正方形网格上进行插值,因此可以直接使用 imagesc
显示二维图像。
优点是可以访问图像对象的AlphaData
属性,可以作为显示遮罩。
r=rand(50); % random 50x50 array
r(11:20,11:20)=NaN; % some hole filled with NaN
imagesc(r) % show the image, with NaN considered as the lowest value in color scale
imagesc(r,'AlphaData',~isnan(r)) % show the image, with NaN values set as fully transparent
您还可以:
- 先设置一个显示掩码
- 用一些有意义的值(最接近的非 NaN 值?)替换零或 NaN
- 使用
interp2
进行插值,甚至可以使用 'cubic'
参数来提高平滑度
- 由于
AlphaData
中设置的显示掩码,仅显示图像的有意义部分。
你好
我想用 2D 格式的 2 个变量(纬度和经度)表示数据。该值由颜色表示,2 个变量作为 2 轴,我正在使用 contourf 函数绘制我的数据。所有数据都来自一个 xlsx 文件,我把它放在一个矩阵中。
Locations = xlsread('Availability results.xlsx');
column_numberloc = 1; % Column in the locations file containing the number of the locations
column_latitude = 2; % Column in the locations file containing the latitude of the locations
column_longitude = 3; % Column in the locations file containing the longitude of the locations
column_availability = 4; % Column in the locations file containing the availability of the locations
min_latitude = min(Locations(:,column_latitude));
max_latitude = max(Locations(:,column_latitude));
min_longitude = min(Locations(:,column_longitude));
max_longitude = max(Locations(:,column_longitude));
max_availability = max(Locations(:,column_availability));
min_availability = min(Locations(:,column_availability));
longitude = Locations(:,column_longitude);
latitude = Locations(:,column_latitude);
Contour = zeros(23,17);
for numerofile=1:204
[coord_x,coord_y] =transformation(Locations(numerofile,column_latitude),Locations(numerofile,column_longitude));
Contour(coord_x,coord_y) = Locations(numerofile,column_availability);
end
for i=1:23
for j=1:17
if Contour(i,j) == 0
Contour(i,j) = NaN;
end
end
end
cMap=jet(256);
figure(1);
x = linspace(min_longitude,max_longitude,17);
y = linspace(min_latitude,max_latitude,23);
newpoints = 100;
[xq,yq] = meshgrid(linspace(min(x),max(x),newpoints),linspace(min(y),max(y),newpoints ));
Contourq = interp2(x,y,Contour,xq,yq,'linear',max_availability);
[c,h]=contourf(xq,yq,Contourq,100);
%[c,h]=contourf(x,y,Contour,50);
set(h, 'edgecolor','none');
colormap(cMap);
cb=colorbar;
caxis([min_availability max_availability]);
transformation 函数允许我将所有数据放在 Contour 矩阵中,因为它将经度和纬度与行和列相关联。
我为每个等于零的数据设置了一个 NaN 以便更好地查看我的数据,我得到了这个: interpolation_linear
很好,但我希望此数据接近于: Without interpolation
所以,我决定 将线性插值更改为 'nearest' 插值 ,我得到了这个: interpolation_nearest
我可以看到更多数据,但等高线图不如线性插值那样平滑。
我看过很多关于如何制作平滑等高线图的帖子(这就是我找到函数 'interp2' 的方式)但我认为我的问题来自 NaN 数据,它阻止我获得平滑等高线在 NaN 值和其余值之间的边缘绘制,如第一张图像,但有足够的数据,如第三张图像。
我的问题是:你知道如何通过最近的插值获得具有足够数据的平滑边缘轮廓图,但具有像第一张图像一样漂亮的视觉效果?
非常感谢
由于您是在正方形网格上进行插值,因此可以直接使用 imagesc
显示二维图像。
优点是可以访问图像对象的AlphaData
属性,可以作为显示遮罩。
r=rand(50); % random 50x50 array
r(11:20,11:20)=NaN; % some hole filled with NaN
imagesc(r) % show the image, with NaN considered as the lowest value in color scale
imagesc(r,'AlphaData',~isnan(r)) % show the image, with NaN values set as fully transparent
您还可以:
- 先设置一个显示掩码
- 用一些有意义的值(最接近的非 NaN 值?)替换零或 NaN
- 使用
interp2
进行插值,甚至可以使用'cubic'
参数来提高平滑度 - 由于
AlphaData
中设置的显示掩码,仅显示图像的有意义部分。