Image and two bubblecharts in one plot

4 vues (au cours des 30 derniers jours)
Robben Hundt
Robben Hundt le 17 Nov 2022
Commenté : Voss le 17 Nov 2022
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.

Réponse acceptée

Voss
Voss le 17 Nov 2022
Modifié(e) : Voss le 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
  2 commentaires
Robben Hundt
Robben Hundt le 17 Nov 2022
Problem solved. Thanks Voss
Voss
Voss le 17 Nov 2022
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 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 commentaires
Image Analyst
Image Analyst le 17 Nov 2022
Déplacé(e) : Voss le 17 Nov 2022
@Robben Hundt you have to paste in the code, then highlight the code then click the code icon. I did the second snippet for you so you can see the difference and see how the second one now has a "Copy" link on it in the upper right of the shaded code black.
Do you have any pictures of what you'd like to see?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Robben Hundt
Robben Hundt le 17 Nov 2022
Will do. Next time I' prepare a properly fromatted question

Connectez-vous pour commenter.

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by