Search code examples
matlabplotmatlab-figure

Efficiently Plot Markers of varying individual size


The plot function allows us to plot all markers with a constant size s.

figure;
x = -10 : 10;
y = x .^ 2;
s = 10;
plot(x, y, 'bo', 'MarkerSize', s);

Suppose instead we would like each marker to have some individual size sx. For example, sx = abs(x) + 1.

One way to achieve this would be to use a for loop.

figure;
x = -10 : 10;
y = x .^ 2;
sx = abs(x) + 1;

hold on;
for i = 1 : length(x)
    plot(x(i), y(i), 'bo', 'MarkerSize', sx(i));
end

This works well enough for small number of x. However, what if x is larger? For example, x = -100 : 0.01 : 100.

This now takes significantly longer, while plot(x, y, 'bo', 'MarkerSize', 100) would still complete almost instantly. Ideally, we would be able to do something such as plot(x, y, 'bo', 'MarkerSize', sx) where sx is a vector of sizes with each entry in sx corresponding to an entry in x and y. Unfortunately, that would give an error of Value not a numeric scalar.

Is there an efficient way to plot markers where each marker have varying individual sizes?


Solution

  • What you're trying to do is possible using scatter instead of plot as follows:

    scatter(x,y,abs(x)+1)