# How can I create a bubble different size plot?

63 views (last 30 days)
Luis Lopez on 11 Dec 2015
Commented: 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

arich82 on 11 Dec 2015
Edited: arich82 on 11 Dec 2015
[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.
x = i_Engine_Speed;
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;
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 CommentsShowHide 1 older comment
Luis Lopez on 11 Dec 2015
Great, this solve my issue perfectly. Thanks!!

Prasad Kalane on 11 Dec 2015
Edited: 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)

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)
centerX = x(ind);
centerY = y(ind);
'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)
centerX = x(ind);
centerY = y(ind);
'Curvature',[1,1],...
'FaceColor','none');
end
axis equal
Joseph Cheng on 11 Dec 2015
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.

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.
arich82 on 11 Dec 2015
[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.

Mike Garrity on 11 Dec 2015
Do you mean something like this?
Here's how I did that:
% 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
% 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
##### 2 CommentsShowHide 1 older comment
Mike Garrity on 14 Dec 2015
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.