Construct a 'Bubble Plot' from a matrix

조회 수: 16 (최근 30일)
Himanshu Saxena
Himanshu Saxena 2021년 6월 8일
댓글: Himanshu Saxena 2021년 7월 16일
Hi,
I want to generate a bubble plot of a matrix. Such plots are possibe to make in 'R Studio'. I have attached an example image of the desired plot.
Is there a way to do so in MATLAB?
Thanks in advance.

채택된 답변

Adam Danz
Adam Danz 2021년 6월 12일
편집: Adam Danz 2021년 6월 14일
> I want to have a sequence of colors corresponding to the the size of circles.
Assuming an n-by-m matrix r should be plotted such that point r(i,j) should appear at x=i, y=j, use meshgrid to create x and y coordinates and then set x, y, and r to bubbleplot as vectors.
The size of the bubbles is defined by |r|and the colors are based r. That way the size of the bubble shows the strength of the correlation and the color shows the direction. That could be switched around, of course.
% Create correlation matrix r
rng default % for reproducibility
r = rand(5)*2-1;
r(logical(eye(5))) = 1
r = 5×5
1.0000 -0.8049 -0.6848 -0.7162 0.3115 0.8116 1.0000 0.9412 -0.1565 -0.9286 -0.7460 0.0938 1.0000 0.8315 0.6983 0.8268 0.9150 -0.0292 1.0000 0.8680 0.2647 0.9298 0.6006 0.9190 1.0000
% Create x and y coordinates and bubblechart
[x,y] = meshgrid(1:size(r,1), 1:size(r,2));
bubblechart(x(:),y(:),abs(r(:)),r(:))
% Cosmetics
colormap('jet')
grid on
set(gca,'xtick', 1:size(r,2), ...
'ytick', 1:size(r,1), ...
'YDir', 'Reverse'); % typically corr matrices use flipped y axes
xlabel('x index')
ylabel('y index')
cb = colorbar;
ylabel(cb, 'Correlation')
caxis([-1,1])
See comment below to add text labels to each bubble showing the correlation values.
  댓글 수: 5
Himanshu Saxena
Himanshu Saxena 2021년 7월 16일
Hi @Adam Danz,
I want to thank you again for this beautiful bubble plot. It worked for me that time perfectly when I had a data in square matrix.
But today it seems that there seems a litlle flaw of which I'm not sure:
set(gca,'xtick', 1:size(r,2), ...
'ytick', 1:size(r,1), ...
'YDir', 'Reverse', ... % typically corr matrices use flipped y axes
'XTickLabel', {'A' 'B' 'C' 'D' 'E'}) % <--- add labels
It seems you have mistakenly wrote '2' next to 'r' in command 'xtick', 1:size(r,2), .... which should be 'xtick', 1:size(r,1), owing to which the there occurs a swap between x and ytick for a non-square matrix.
Presently, I am working with a non-square matrix (6x14). I have attached the datafile and code I used and the plot I got. It can be noticed that row-a and column-14 value in the datafile does not match with the color code produced in the plot. The problem is that bubblechart produces wrong color code for the actual data. I don't know why this is happening.
Also, I have attached a datafile named "Datafile (no data in one of the rows or columns)" in which one row/one column is empty which usually occure for my correlation matrix when no correlation occurs of variable with any of the parameter. In this case, after reading the datafile matrix in MATLAB, the empty row/column is cmpletely ignored/removed form the bubblechart which I don't want to happen with the bubblechart I want.
Kindly suggest how to create the same bubblechart (which you provided me earlier) for a non-square matrix.
Thanks and regards,
Himanshu
Himanshu Saxena
Himanshu Saxena 2021년 7월 16일
Hi, I found my error. The error was due to my own understanding of using meshgrid and size(r,1) command.
In meshgrid(x,y), length(rows) is y and length(columns) is x.
In size(r,1), 1 is for rows and in size(r,2), 2 is for columns.
thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andres
Andres 2021년 6월 8일
Hi,
bubblechart
should help you with that.
  댓글 수: 4
Himanshu Saxena
Himanshu Saxena 2021년 6월 11일
Thanks Andres for your effort. Though it seems similar to the attached plot, it does not fulfill my need. I want to plot a correlation matrix data in the form of attached plot. Since such matrix would range from -1 to +1, I want to have a sequence of colors corresponding to the the size of circles. Since I know very basics of MATLAB, I cannot myself further lead your submitted commands to my requirements.
Andres
Andres 2021년 6월 12일
Thanks for your reply. Maybe you can give a small example of such a matrix and describe how the bubble plot should look like.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by