fill() function leaves gaps. What am I doing wrong? How do I correct this?

조회 수: 3 (최근 30일)
I am trying to plot some 2D data in a pcolor plot. It should be confined to a circular region in the center of the plot and I am trying to make the region outside this circle white using the fill function. However, I find that there a radial lines of color still - as if the fill function is leaving gaps. What am I doing wrong? How can I correct this?
My code is as follows:
clc;
clear all;
close all;
%% Set Global Font to Latex
set(groot,'defaulttextinterpreter','latex');
set(groot, 'defaultAxesTickLabelInterpreter','latex');
set(groot, 'defaultLegendInterpreter','latex');
%% Import data
saveOutput = false;
folder = pwd;
filename1 = "slice0.mat";
filepath1 = strcat(folder,'\',filename1);
imp1 = load(filepath1);
rawData1 = imp1.Expression1;
x1 = 1000*rawData1(:,1);
y1 = 1000*rawData1(:,2);
b1 = rawData1(:,3);
xpp1 = reshape(x1,[31 31]);
ypp1 = reshape(y1,[31 31]);
bpp1 = reshape(b1,[31 31]);
%% Plotting
xlab = 'y (mm)';
ylab = 'x (mm)';
fontSize = 12;
% Get data for the two circles
theta = 0.00000:0.01:2*pi;
r1 = 7.75;
r2 = 15;
xp1 = r1*cos(theta);
yp1 = r1*sin(theta);
xp10 = r2*cos(theta);
yp10 = r2*sin(theta);
figx0 = 1;
figy0 = 1;
figscale = 16;
figwidth = figscale;
figheight = figscale;
% Begin figure
fig1 = figure('Units','centimeters',...
'Position',[figx0,figy0,figwidth,figheight],...
'PaperPositionMode','auto',...
'Renderer', 'Painters');
h = pcolor(xpp1,ypp1,bpp1);
set(h, 'EdgeColor', 'none');
axis square
colormap(hot(256));
colorbar
caxis([0 1.8]);
set(gcf, 'Color', 'w');
shading interp
xlabel(xlab,'Interpreter','latex','FontSize', fontSize)
ylabel(ylab,'Interpreter','latex','FontSize', fontSize)
hold on
plot(xp1,yp1,'w')
plot(xp10,yp10,'w')
% Fill in the area by defining the polygon that is defined by tracing the inner circle and then back along the outer circle
fill([xp1 flip(xp10)],[yp1 flip(yp10)],'w')
% Cover up the connecting line in the same color
h = line([xp1(1) xp10(end)],[yp1(1) yp10(end)]);
set(h,'Color','w')
set(gca,'layer','top')
grid off
%% Export
exFilename = "slices";
if saveOutput
exfolder = folder;
filename = exFilename;
exfilepath = strcat(exfolder,'\',filename,".pdf");
exfilepathEPS = strcat(exfolder,'\',filename,".eps");
% export_fig(fig1,exfilepath)
% saveas(gcf,exfilepath);
% print(exfilepath,'-dpdf');
print(exfilepathEPS,'-depsc2');
end
and the resulting figure:
where you can see that outside the circular region there are gaps where the fill function has missed bits.
Note that there are no errors.
Thanks in advance!

채택된 답변

Image Analyst
Image Analyst 2022년 8월 18일
I would not use pcolor. I would use image or imshow. Create a digital image then scan it pixel by pixel and figure out what the color value should be. This will ensure there are no "missed" pixels. Doing it like you've done will "miss" some pixels, as you have already observed.
  댓글 수: 2
simon swarbrick
simon swarbrick 2022년 8월 19일
Thank you for this good idea. However, there is one disadvantage to this: I do not get a smooth circular edge. It is a 31x31 grid of pixels so the edge is quite clearly made of squares - even with interpolation.
Image Analyst
Image Analyst 2022년 8월 19일
Well that's the resolution of your raw data. If you want a smoother display, you could always use interp2 to interpolate your data up to a larger size.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by