MATLAB Answers

0

How can I create a bubble different size plot?

Asked by Luis Lopez on 11 Dec 2015
Latest activity Commented on by Mike Garrity
on 14 Dec 2015
Hi,
I have two vectors with too many data (10000 x 1), I want to create a different size bubble plot, I guess I'd to reduce that data to two new vectors for data plot and one more for bubble size but I'm not sure. Hope you can suggest me.
Thanks

  0 Comments

Sign in to comment.

5 Answers

Answer by arich82
on 11 Dec 2015
Edited by arich82
on 11 Dec 2015
 Accepted Answer

[Edit to correct labels; also, note that no toolboxes are necessary.]
See if this works for you. It combines the heat map and bubble plot I mentioned earlier.
load('speed_load.mat');
x = i_Engine_Speed;
y = i_Instantaneous_Percent_Load;
xbins = 600:100:2200;
ybins = 0:10:100;
% map vals to inds
% over-range vals are truncated instead of extrapolated
xi = interp1(xbins, 1:numel(xbins), x, 'nearest', numel(xbins));
yi = interp1(ybins, 1:numel(ybins), y, 'nearest', numel(ybins));
% make 2-d hist (heatmap) using accumarray
H = accumarray([yi(:), xi(:)], 1, [numel(ybins), numel(xbins)]);
% generate plot
hf = figure;
ha = axes;
ylabel('Instantaneous Load [%]')
xlabel('Engine Speed [RPM]');
axis([xbins(1), xbins(end), ybins(1), ybins(end)]);
hold(ha, 'all');
% plot heat map
hi = imagesc(xbins, ybins, H); % could use pcolor, but edges are wonky
% set(ha, 'YDir', 'normal'); % correct y-axis direction after imagesc
% plot circles
[Xbins, Ybins] = meshgrid(xbins, ybins);
ind = H(:) > 0;
scatter(Xbins(ind), Ybins(ind), H(ind).^1.1, [0, 0, 1])

  2 Comments

[Edit to correct labels.]
Of course, you could always plot it without the heat map...
load('speed_load.mat');
x = i_Engine_Speed;
y = i_Instantaneous_Percent_Load;
xbins = 600:100:2200;
ybins = 0:10:100;
% map vals to inds
% over-range vals are truncated instead of extrapolated
xi = interp1(xbins, 1:numel(xbins), x, 'nearest', numel(xbins));
yi = interp1(ybins, 1:numel(ybins), y, 'nearest', numel(ybins));
% make 2-d hist (heatmap) using accumarray
H = accumarray([yi(:), xi(:)], 1, [numel(ybins), numel(xbins)]);
% generate plot
hf = figure;
ha = axes;
ylabel('Instantaneous Load [%]')
xlabel('Engine Speed [RPM]');
axis([xbins(1), xbins(end), ybins(1), ybins(end)]);
% plot circles
[Xbins, Ybins] = meshgrid(xbins, ybins);
ind = H(:) > 0;
scatter(Xbins(ind), Ybins(ind), H(ind).^1.1, [0, 0, 1])
grid(ha, 'on');
Great, this solve my issue perfectly. Thanks!!

Sign in to comment.


Answer by Prasad Kalane on 11 Dec 2015
Edited by Prasad Kalane on 11 Dec 2015

I am not getting your Total idea. but try using following
>> plot(rand(1,10),'bo','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)
>> hold on
>> plot(rand(1,8),'ro','MarkerEdgeColor','k','MarkerFaceColor','m','MarkerSize',20)

  0 Comments

Sign in to comment.


Answer by Joseph Cheng
on 11 Dec 2015

you are best off not using markersize. Markersize is unitless and there is not many explanations on how to set it to correspond to the y or x axis. it can be used if you want to display the items relatively. for instance big vs medium vs small dots. One way around this is to use rectangle. in the example i have below i show markersize not working and in figure 2 use of rectangle.
x = [1 10 100];
y = zeros(size(x));
r = x/2;
figure(1), clf, hold on
for ind = 1:numel(x)
plot(x(ind),y(ind),'bo','markersize',r(ind));
end
figure(2),clf,hold on
for ind = 1:numel(x)
radius = r(ind);
centerX = x(ind);
centerY = y(ind);
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],...
'Curvature',[1,1],...
'FaceColor','none');
end
axis square
only issue here is that you'll have to make the axis square or equal
x = randi(2000,1,40);
y = randi(4000,1,numel(x))-2000
r = randi(500,1,numel(x))
figure(2),clf,hold on
for ind = 1:numel(x)
radius = r(ind);
centerX = x(ind);
centerY = y(ind);
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],...
'Curvature',[1,1],...
'FaceColor','none');
end
axis equal

  1 Comment

use my answer above in conjunction with accumarray(). see http://www.mathworks.com/matlabcentral/answers/129646-collate-data-in-grid for additional information on how to accomplish this.

Sign in to comment.


Answer by Luis Lopez on 11 Dec 2015

I'll be more specific, I'd two vectors, (same lenght), I want plot first versus second variable but the more repetitive values should be shown as bigger bubble. I think data from vectors should be agruped in ranges before plot. Hope my idea is clear.

  1 Comment

[Edit. Meant to ask: what do you want done with values over 100%? Is 101.172% meaningful, or do you want bins to limit it to 100%?]
A couple of notes: your data has 10589 points; your x-data i_Engine_Speed has 5431 unique values; your y-data i_Instantaneous_Percent_Load has only 254 unique values. However, using unique(..., 'rows'), you have 9169 unique points.
Since you seem to have much greater resolution in x than y, you'll need to choose your bin sizes carefully...
I wonder if a heat-map might be more appropriate than the circle plot. If you have a more recent version of Matlab and the appropriate toolboxes (I do not), you might consider bar3 or a HeatMap; otherwise, you can search the FEX (I've never used any of these functions).
If you're really set on circles, however, you should be able to do this with the third input to scatter.

Sign in to comment.


Answer by Mike Garrity
on 11 Dec 2015

Do you mean something like this?
Here's how I did that:
% Load data
load C:\Users\mgarrity\Downloads\speed_load.mat
% File appears to only have a single dependent var, so make up another
extra_var = randn(size(i_Engine_Speed));
% Histogram the engine speed, and keep the bins
[~,edges,bin] = histcounts(i_Engine_Speed);
nbins = length(edges)-1;
% Turn bin edges into bin centers
x = conv(edges,[.5 .5],'valid');
% Make two arrays to accumulate Y and Size into
y = zeros(1,nbins);
s = zeros(1,nbins);
for i=1:nbins
% Pull out the values for engine speeds in the current bin
mask = bin==i;
tmp_y = i_Instantaneous_Percent_Load(mask);
tmp_s = extra_var(mask);
% Accumulate the values. Use whatever function you like here
y(i) = mean(tmp_y);
s(i) = mean(tmp_s);
end
% Come up with a scale factor for size. Scatter wants points^2.
min_size = 1;
max_size = 200;
range = [min(s) max(s)];
s = min_size + (s - range(1)) * (max_size-min_size) / (range(2)-range(1));
% Plot using scatter
scatter(x,y,s)
grid on
ylabel('Percent load')

  2 Comments

Yes, that's what I'm looking for, however my Matlab version R2013b doesn't have the hiscount function. How can I replace that function?
The histcounts function was an upgraded "replacement" for an earlier function named histc. You should be able to use that in R2013b to do the same thing. It didn't have as many options, but it should do fine for this job.

Sign in to comment.