Image and two bubblecharts in one plot

4 views (last 30 days)
Hi everyone,
I have two different data sets (x,y,value) which should be displayed as bubble chart. As the values are substantially different, I want to scale the size of the bubbles for each data set individually and also use two legends. So far so good:
%some random data for the two bubble charts
nxx=5;
xx1 = rand(1,nxx);
yy1 = rand(1,nxx);
sz1 = rand(1,nxx);
xx2 = rand(1,nxx);
yy2 = rand(1,nxx);
sz2 = rand(1,nxx)*100;
figure(1)
ax11=gca;
bubblechart(ax11,xx1,yy1,sz1,[0.5 0 0.5],'MarkerFaceAlpha',0.30)
bubblesize(ax11,[5 80])
bd=bubblelegend(ax11,'Data 1','Style','telescopic'); %no worries about the legend location for now
bd.Location='northwest';
ax22 = copyobj(ax11,gcf);
bubblechart(ax22,xx2,yy2,sz2);
bubblesize(ax22,[5 25])
bd2=bubblelegend(ax22,'Data 2','Style','telescopic');
bd2.Location = 'southwest';
%%Hide the axes of the second bubblechart
ax22.Visible = 'off';
Additionally, I would like to plot these to bubblecharts on a background image. However, the legend of the first bubblechart disappears and also the bubblesize property of the second bubbleplot seems to overwrite the first one.
%some random data for the two bubble charts
nxx=5;
xx1 = rand(1,nxx);
yy1 = rand(1,nxx);
sz1 = rand(1,nxx);
xx2 = rand(1,nxx);
yy2 = rand(1,nxx);
sz2 = rand(1,nxx)*100;
figure(2)
axi=gca;
imagesc(0:1/(nxx-1):1,0:1/(nxx-1):1,rand(nxx)), hold on % some random image first
axi.Visible = 'off';
ax11=gca;
bubblechart(ax11,xx1,yy1,sz1,[0.5 0 0.5],'MarkerFaceAlpha',0.30)
bubblesize(ax11,[5 80])
bd=bubblelegend(ax11,'Data 1','Style','telescopic'); %no worries about the legend location for now
bd.Location='northwest';
ax22 = copyobj(ax11,gcf);
bubblechart(ax22,xx2,yy2,sz2);
bubblesize(ax22,[5 25])
bd2=bubblelegend(ax22,'Data 2','Style','telescopic');
bd2.Location = 'southwest';
%%Hide the axes of the second bubblechart
ax22.Visible = 'off';
hold off
I suspect that I messed up the axes objects but can't figure out how to fix it. By the way, I am not attached to the bubblechart function. I tried 'scatter' as well but there it is difficult to generate a legend based on the bubble size. Any hint is appreciated.

Accepted Answer

Voss
Voss on 17 Nov 2022
Edited: Voss on 17 Nov 2022
copyobj copies graphics object(s) and their descendants, so when you call copyobj after creating the image, you get a copy of the image in the new axes as well.
Does calling copyobj before creating the image give you something more like what you're after?
%some random data for the two bubble charts
nxx=5;
xx1 = rand(1,nxx);
yy1 = rand(1,nxx);
sz1 = rand(1,nxx);
xx2 = rand(1,nxx);
yy2 = rand(1,nxx);
sz2 = rand(1,nxx)*100;
figure
axi=gca; % axi and ax11 are the same axes object, by the way
ax11=gca;
ax22 = copyobj(ax11,gcf);
imagesc(0:1/(nxx-1):1,0:1/(nxx-1):1,rand(nxx)), hold on % some random image first
axi.Visible = 'off';
bubblechart(ax11,xx1,yy1,sz1,[0.5 0 0.5],'MarkerFaceAlpha',0.30)
bubblesize(ax11,[5 80])
bd=bubblelegend(ax11,'Data 1','Style','telescopic'); %no worries about the legend location for now
bd.Location='northwest';
bubblechart(ax22,xx2,yy2,sz2);
bubblesize(ax22,[5 25])
bd2=bubblelegend(ax22,'Data 2','Style','telescopic');
bd2.Location = 'southwest';
%%Hide the axes of the second bubblechart
ax22.Visible = 'off';
hold off

More Answers (1)

Image Analyst
Image Analyst on 17 Nov 2022
Format your code as code so that we can easily copy it and run it (which I didn't).
If you want to create plots and images inside other plots and images, see attached demos. Adapt as needed.
  3 Comments
Robben Hundt
Robben Hundt on 17 Nov 2022
Will do. Next time I' prepare a properly fromatted question

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!