line is filling two pixels with linewidth of 1 specified

4 views (last 30 days)
Depending on where I plot a line on my axes, the line thickness changes. I always want my line thickness to stay constant. Is there a way to ensure that when a line spans two pixels, it snaps to one set of pixels or the other?
app.UIAxes.XLim = [-30 30];
app.UIAxes.YLim = [-30 30];
l1 = line(app.UIAxes,[0 10],[6.3 6.3],'Color','c','LineWidth',1); % results in line spanning one pixel (looks good)
l1 = line(app.UIAxes,[0 10],[6.4 6.4],'Color','c','LineWidth',1); % results in line spanning two pixels (looks bad)

Answers (2)

Dana
Dana on 26 Aug 2020
Edited: Dana on 26 Aug 2020
Line widths are specified in points (1/72"), not pixels, and locations on a plot are expressed in the same units as the axes. When displaying a line on your screen, the computer must "convert" these values into pixels. The problem is, pixels are discrete: you can't use only part of a pixel.
So, for example, 1 pt might correspond to exactly 1.4 pixels (I'm making these numbers up, but hopefully you get the idea), and the location 6.3 on the vertical axis could correspond to 300 pixels up from the bottom of the figure, while 6.4 corresponds to 303.2 pixels from the bottom.
To plot the first line, the computer wants to draw a line across the figure, with the vertical location of the bottom and top of the line given by 300 and 301.4 pixels from the bottom. But since you can't draw part of the pixel, the computer rounds these numbers to 300 and 301. Thus, the first line is 1 pixel wide.
On the other hand, the vertical location of the bottom and top of the second line is suposed to be 303.2 and 304.6. In this case, the computer rounds to 303 and 305: a 2-pixel-wide line.
Note that this becomes less of an issue if you either display the figure in a higher-resolution setting (e.g., if you print it on a printer, which should have a much higher resolution than your screen), or if you make your figure significantly larger (e.g., maximize the figure window).

Brandon Baker
Brandon Baker on 26 Aug 2020
I haven't found MATLAB's built in anti-aliasing settings to be that effective; MATLAB does not spend much computing effort to anti-alias images, which can be as much as 90% of the work of a proper graphics pipeline.
Anders Brun ( https://www.mathworks.com/matlabcentral/profile/authors/1421525 ) created an antialiasing engine for better graphics in MATLAB.
Yair Altman created "export_fig" to perform a similar task for saving figures as bitmaps, etc.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!