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?

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

It is hard to know how to answer your question, without actually seeing the plot you created.
In general, you cannot simply prevent aliasing in lines plotted through a discrete grid of pixels. That is what your monitor uses, a discrete regular grid of pixels.
Thank you for your response. Ideally, the grid will be printed. The trouble seems to arise when I convert the figure into a tif file.
Cathy, let me be direct: Attach a screenshot! Probably no one will answer unless you do that because, like John said, we can't see what you're seeing. How are they being warped? Are they just the normal jaggies, or are the lines really warped/bent?
Perhaps if you supplied a resolution when you used the print() command ?
If you have the Computer Vision toolbox, you could use insertShape to draw the lines into an array, after which you could imwrite() the array. This would permit you to draw with any necessary resolution.
So the lines should not have this pattern, ideally the grids should look somewhat square (not perfectly square). This is only a snapshot that is 100x zoomed in of the overall grid.
And thank you, Walter, I will look into that. I have not tried using the print command and I will look into insertShape.
I doubt the variation in row and column spacing would be anywhere near that amount if it were due to aliasing on a pixel basis. I mean they're like 20 or 100 rows/columns away from where they should be. I think it's due to the way your code lays them down. Please give the source code you used to create this image.
@Cathy: please show us the code that you used, by clicking the paperclip button in a new comment.
I edited the description to include the code. Thanks
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)

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.

Asked:

on 19 Feb 2018

Edited:

on 24 Feb 2018

Community Treasure Hunt

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

Start Hunting!