Positioning bar and image in the same figure

I'm trying to print an image above a bar plot in the same figure, manually changing their position using the set command with the OuterPosition option.
This works only if the vertical coordinate of the image is at least 0.42, in fact if it is below this value then the image disappears.
How can I place the image below 0.42?
subplot(2,1,1)
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
set(gca, 'OuterPosition', [.3 .42 .4 .4]);
subplot(2,1,2)
bar(1:10)
set(gca, 'OuterPosition', [.3 0 .35 .23]);

4 commentaires

Rik
Rik le 1 Mai 2020
Why are you using the OuterPosition property? Is there a particular reason you aren't using the Position property?
giannit
giannit le 1 Mai 2020
I read to use OuterPosition in an answer here on this site, is it bad?
Rik
Rik le 1 Mai 2020
I tend to use the Position property. Also, you can store the handle generated by subplot, so you can avoid gca. You can also directly create the axes in the position you want it with the axes function.
I tried with this as you suggested, but image is still not showing
s1 = subplot(2,1,1)
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
set(s1, 'Position', [.3 .41 .4 .4]);
s2 = subplot(2,1,2)
bar(1:10)
set(s2, 'Position', [.3 0 .35 .23]);

Connectez-vous pour commenter.

 Réponse acceptée

Due to some reason, the subplot() command deletes the axes behind it. Also, as pointed out by Rik, you are setting the OuterPosition manually. It is better to use axes(). Although axes() also seems to delete the axes object behind it, so you need to hold() the axes. Try this
ax1 = axes();
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
set(ax1, 'OuterPosition', [.3 .35 .4 .4]);
hold(ax1);
ax2 = axes();
bar(1:10)
set(ax2, 'OuterPosition', [.3 0 .35 .23]);
You can also avoid hold ing the axes if you pass OuterPosition directly to the axes call
ax1 = axes('OuterPosition', [.3 .35 .4 .4]);
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
ax2 = axes('OuterPosition', [.3 0 .35 .23]);
bar(1:10)

3 commentaires

giannit
giannit le 1 Mai 2020
Thank you very much works like a charm!
Ameer Hamza
Ameer Hamza le 1 Mai 2020
I am glad to be of help.
Is also possible with subplot!
subplot('Position',[x y w h])
imshow( imread('https://i.imgur.com/TVlQhpj.jpg') )
subplot('Position',[x y w h])
bar(1:10)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Printing and Saving dans Centre d'aide et File Exchange

Produits

Version

R2019b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by