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

2 views (last 30 days)
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!

Accepted Answer

Image Analyst
Image Analyst on 18 Aug 2022
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 Comments
simon swarbrick
simon swarbrick on 19 Aug 2022
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 on 19 Aug 2022
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!