Matching scatter marker size to pixel size

I'm trying to set the size of markers in my scatter plot to the size of a pixel in my plot. At the moment, I'm calculating the marker size using the following code:
% Setting the size of square markers to 1px^2
s=1.0;
currentunits = get(gca,'Units');
set(gca, 'Units', 'Points');
axpos = get(gca,'Position');
set(gca, 'Units', currentunits);
markerWidth = s/diff(xlim)*axpos(3); % Calculate Marker width in points
s = markerWidth^2;
So, I'm adjusting the value of s from 1 px to its size in points^2, which is how the marker size is defined. But the output is not quite giving me what I want:
What's going on here? Why are the marker sizes still smaller than the image sizes?

4 Comments

Do you want pixel units (as stated in your question title) or point units (as stated in your question)? A point is 1/72 of an inch. On Mac's a pixel is also 1/72 but on windows a pixle is 1/96 of an inch.
Hi Adam,
The marker size in a scatter plot uses points^2. I want my marker to be 1pixel^2, so I'm trying to convert 1pixel^2 to points squared. Is that clear?
And by pixels, I'm referring to the 100 squares in my grid
I want the markers (colored squares) to completely cover the squares in the grid.

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 24 Oct 2019
Edited: Adam Danz on 26 Dec 2023
Scatter & markersize method
This is a demo for your marker size idea. However, I don't recommend this method (see note below).
axh = axes('XTick',0:1:10,'YTick',0:1:10); % Assumes square axes
grid on
axis equal
xlim([0,10])
ylim([0,10])
hold on
% Get plot size in pixel units
originalUnits = axh.Units;
axh.Units = 'Points';
axSizePix = axh.Position(3:4);
axh.Units = originalUnits;
% Compute square pixel per 1 unit of data
pixPerUnit = axSizePix ./ [range(xlim(axh)),range(ylim(axh))]; %pixels per unit
scatter(axh, 6.5, 7.5, ceil(max(pixPerUnit))^2, 'b', 'Marker','s','MarkerFaceColor', 'flat')
scatter(axh, 3.5, 3.5, ceil(max(pixPerUnit))^2, 'r', 'Marker','s','MarkerFaceColor', 'flat')
Note that as soon as you resize the figure or axes, you'll lose the fit of each marker to the grid!
For that reason, it may be better to add rectangles that are in data units and will therefore resize with the figure and axes.
Rectangle method
% Define your data points
xy = [6.5, 7.5;
3.5, 3.5;
2.5, 7.5];
% Define your colors
colors = [1 0 0;
0 1 0;
0 0 1];
% Create axes
axh = axes('XTick',0:1:10,'YTick',0:1:10); % Assumes square axes
grid on
axis equal
xlim([0,10])
ylim([0,10])
hold on
% Plot rectangles
rh = arrayfun(@(i) rectangle('position',[xy(i,:)-.5, 1, 1], 'FaceColor',colors(i,:),'EdgeColor', 'none'), 1:size(xy,1));
% The rectanlge size [xy(i,:)-.5, 1, 1] is based on a square grid
% with unit size = 1.

6 Comments

Glad I could help! (Go with the rectangle method!)
Sir, i also need to match my marker size to grid cells. But the problem is my squares are moving so i cant define a position. And i dont want them to change according to scale,i need to use simple functions. Any ideas of how to do it? Thank you.
@rumeysa create a digital image (3-D array) and assign the pixel colors. Display with imshow using the 'InitialMagnification' option if you have just a few pixels on a side.
@rumeysa The rectangle function is fairly simple. Are you having trouble updating the position of the rectangle? See also polyshape, Polygon, and images.roi.Rectangle
its some part of my code thats how i wrote it. since im a beginner i cant use these functions, otherwise our proffessor will cut points that he didnt teach us. i think ithis makes it difficult for us to develop ourselves, but that's the way it is. i just want them to look like this in every scale.
% initial positions and velocities of particles
particle_positions = ones(num_of_particles, 2) * num_of_cells / 2 + 0.5
handle = plot(particle_positions(:, 1), particle_positions(:, 2), 'bs', 'MarkerSize',33, 'MarkerFaceColor', 'b');

Sign in to comment.

More Answers (0)

Asked:

on 24 Oct 2019

Commented:

on 27 Dec 2023

Community Treasure Hunt

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

Start Hunting!