Matching scatter marker size to pixel size (2024)

27 views (last 30 days)

Show older comments

Sam Nadjari on 24 Oct 2019

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size

Commented: nctt on 27 Dec 2023

Accepted Answer: Adam Danz

Open in MATLAB Online

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:

Matching scatter marker size to pixel size (2)

What's going on here? Why are the marker sizes still smaller than the image sizes?

4 Comments

Show 2 older commentsHide 2 older comments

Adam Danz on 24 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759811

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759811

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.

https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#budumk7_sep_shared-Units

Sam Nadjari on 24 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759821

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759821

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?

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759824

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759824

And by pixels, I'm referring to the 100 squares in my grid

Sam Nadjari on 24 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759833

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759833

I want the markers (colored squares) to completely cover the squares in the grid.

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Adam Danz on 24 Oct 2019

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#answer_398107

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#answer_398107

Edited: Adam Danz on 26 Dec 2023

Open in MATLAB Online

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')

Matching scatter marker size to pixel size (8)

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.

Matching scatter marker size to pixel size (9)

6 Comments

Show 4 older commentsHide 4 older comments

Sam Nadjari on 24 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759910

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759910

Thanks so much!

Adam Danz on 25 Oct 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759924

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_759924

Glad I could help! (Go with the rectangle method!)

nctt on 23 Dec 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_3007722

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_3007722

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.

Image Analyst on 23 Dec 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_3007812

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_3007812

@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.

Adam Danz on 26 Dec 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_3009622

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_3009622

@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 on 27 Dec 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_3010416

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/487262-matching-scatter-marker-size-to-pixel-size#comment_3010416

Open in MATLAB Online

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.Matching scatter marker size to pixel size (16)

% 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)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsImagesRead, Write, and Modify Image

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Tags

  • image processing
  • markersize
  • scatter
  • pixels
  • points

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Matching scatter marker size to pixel size (17)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Matching scatter marker size to pixel size (2024)
Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 6182

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.