Matching scatter marker size to pixel size

14 次查看(过去 30 天)
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 个评论
Sam Nadjari
Sam Nadjari 2019-10-24
And by pixels, I'm referring to the 100 squares in my grid
Sam Nadjari
Sam Nadjari 2019-10-24
I want the markers (colored squares) to completely cover the squares in the grid.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-10-24
编辑:Adam Danz 2023-12-26
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 个评论
Adam Danz
Adam Danz 2023-12-26
@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
nctt
nctt 2023-12-27
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');

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by