Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Plot of data representing particles of various size and various properties (e.g. temperature)

Asked by Maurice on 18 Apr 2011

hi there

I have a model that handles particles and lets them react. in the end, I have the coordinates in [x,y] where the particles center lies, it's radius and for instance its temperature.

I would now like to plot that particle at [x,y], as a dot representing its size (e.g. with MarkerSize?) and color by temperature.

Is there anything around that can actually do this? The problem is also that the particle size should be realistically displayed.

thank you for your help! maurice

0 Comments

Maurice

Products

No products are associated with this question.

3 Answers

Answer by Patrick Kalita on 18 Apr 2011

You should be able to do this with the scatter command. The first two inputs are the x and y locations. The third input is the size of the marker to be drawn. And the fourth controls the color. The only tricky bit would be to scale the temperature data to a range of appropriate marker sizes.

Here's a very simply example that just scales the T data so that the third input argument is in the range [0 100]:

x = rand(1, 10);
y = rand(1, 10);
T = x + y;
scatter(x,y,T./max(T) * 100, T,'filled')

1 Comment

Maurice on 20 Apr 2011

Thank you, works nice!

Patrick Kalita
Answer by Teja Muppirala on 18 Apr 2011

I think the downside with using SCATTER is that the marker doesn't change size with zooming. Using PATCH might be another option:

N = 100; %Number of points
r = 0.2*rand(N,1); %Radii
T = rand(N,1); %Temperatures
p = randn(N,2); %Positions
colordef(figure,'black');
q = linspace(0,2*pi,32)';
C = [cos(q) sin(q)];
hold all;
for n = 1:numel(r)
    patch(r(n)*C(:,1)+p(n,1), r(n)*C(:,2)+p(n,2),T(n),'edgecolor','none');
end
axis equal;
set(gca,'Clim',[0 1]);
alpha 0.9
colorbar

1 Comment

Maurice on 20 Apr 2011

Thank you for your help, this is what I was looking for.

You saved me a lot of work figuring out how to do it.

THANKS

Teja Muppirala
Answer by Artur Palha on 29 Jan 2013
Edited by Artur Palha on 29 Jan 2013

A small change to the option Teja Muppirala gave can give a considerable speed up, especially when plotting many data points. The alternative is to plot all the patches at once in the following way:

N = 1000; %Number of points
r = 0.2*rand(N,1); %Radii
T = rand(N,1); %Temperatures
p = randn(N,2); %Positions
figure(1)
clf(1)
colordef(1,'black');
q = linspace(0,2*pi,32)';
C = [cos(q) sin(q)];
hold all;
% Teja Muppirala approach
tic
for n = 1:numel(r)
    patch(r(n)*C(:,1)+p(n,1), r(n)*C(:,2)+p(n,2),T(n),'edgecolor','none');
end
toc
axis equal;
set(gca,'Clim',[0 1]);
alpha 0.9
colorbar
hold off
figure(2)
clf(2)
colordef(2,'black');
hold all;
% faster approach
tic
patch(bsxfun(@plus,bsxfun(@times,r',repmat(C(:,1),[1 N])),p(:,1)'), bsxfun(@plus,bsxfun(@times,r',repmat(C(:,2),[1 N])),p(:,2)'),T','edgecolor','none');
toc
axis equal;
set(gca,'Clim',[0 1]);
alpha 0.9
colorbar
hold off

I made this change because I need to plot thousands of data points. for 1e5 data points I get a speedup of about 30x.

0 Comments

Artur Palha

Contact us