I am plotting a grid with multiple lines. The spacing between each line is warped and inaccurate in Matlab due to the numerous data points. How do I make the lines properly spaced?

11 views (last 30 days)
I have 80,802 lines going horizontally and vertically. It appears that there is aliasing going on. How can I prevent aliasing of the figure? The lines are not evenly spaced/there is a function. It looks normal when I decrease the number of data points. This might be a problem when I save the figure as an image, so how can I get around that?
%%graph horizontal and vertical lines. Make the values square andd then
%%limit the value so that the vertical height is shorter than the
%%horizontal width.
xangle = [-20:.1:20];
yangle = [-11:.1:11];
dis = 228;
x_squareleg = dis*tan(xangle*pi/180);
y_squareleg = dis*tan(yangle*pi/180);
%keyboard
%\%plot a horizontal line for every value within the list of xangle
for val = 1:length(x_squareleg)
h = plot(x_squareleg, [ones(1,length(x_squareleg))*x_squareleg(val)], 'k');
%the ones function produces length # of ones, which is multiplied
%by the value of the spacing between y axis in order to make a
%horizontal line
% disp(x_squareleg(val))
hold on
end
%plot a vertical line for every value within the list of xangle
for val = 1:length(x_squareleg)
v = plot([ones(1,length(x_squareleg))*x_squareleg(val)], x_squareleg, 'k');
hold on
%disp(x_squareleg(val))
end
set(gcf,'Units','centimeters', 'Position', [0, 0, 83*2, 44.3*2], 'PaperUnits', 'centimeters', 'PaperSize', [83*2, 44.3*2]);
axis equal %matlab does not default to equal axis
axis off
set(groot,'DefaultFigureGraphicsSmoothing','off') % makes the lines individually not automatically smoothed
set(h,'AlignVertexCenters','on')
set(v,'AlignVertexCenters','on')
%set lines to be even in appearance
xlim([min(x_squareleg) max(x_squareleg)])
ylim([min(y_squareleg) max(y_squareleg)])
%print('-bestfit','BestFitFigure','-dpdf')
hold off
  9 Comments
Walter Roberson
Walter Roberson on 23 Feb 2018
When I use the posted code on my system, the output looks fine.
I cannot say for sure that the output grid is completely regular, but I can say that if it is not completely regular, then the possible errors are very much of the same magnitude as I would expect to experience with my aging eyes.
Note that I have a fairly large display, 2580 by 2048 or something like that, so the graphics system is not needing to try to map multiple logical pixels to the same physical pixel.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 19 Feb 2018

Bob Blaine
Bob Blaine on 23 Feb 2018
Hi Cathy,
I think you are right, I think this is a result of the way that antialiasing works. On my system, MATLAB uses 8 samples per pixel for antialiasing. If a line covers 4 of these pixels, then the line color will contribute to half of the color of the pixel. The other half of the coloring will come from the background or whatever else covers that pixel. Similarly, if the line only covers 2 samples, it will only contribute 25% of the color.
MATLAB lines are sized to cover 1 pixel, but unless you place the line exactly so the vertices land right in the middle of the pixels, the line will contribute to coloring 2 rows of pixels. Also, if the spacing of the lines doesn't exactly match the spacing of the pixels, the number of samples in each row of pixels the coloring of the line can vary in what looks like an interference pattern.
Now cross the lines like you did, and you've created a moire pattern. You can kind of see what's happening if you look at it with Magnifier.

Community Treasure Hunt

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

Start Hunting!