Ordering Scatter Plot Layers

Hi! I’m trying to do a scatter plot with a text annotation on the background. My question is how I can make the text appear bellow the bubbles, as opposed to on top of them? Here’s my code. (I’ve used random data for simplicity here)
vy = randi(80,[100,1]); % random data
vx = randi(50000, [100,1]); % random data
c = rand([length(vy),3]); % random colors RGB
figure('Position',[100 100 800 600], 'Color', 'w')
axes('Position',[0.1 0.15 0.8 0.8],'XScale','log', 'Color', 'w')
axis([200 100000 20 85]); % [xMin xMax yMin yMax]
ha = gca; % get current axes handle
set(ha, 'FontSize', 14, 'Color', 'w',...
'XColor', [0.5 0.5 0.5], 'YColor', [0.5 0.5 0.5])
set(ha, 'XTick', xTick)
set(ha,'XTickLabel',{'400';'1,000';'2,000';'4,000';
'10,000';'20,000';'40,000'})
grid on
set(ha, 'box', 'on')
ht = text('Units', 'normalized', 'Position',[0.5,
0.5],'String','Trial', ...
'FontUnits', 'normalized' , 'FontSize', 0.35 ,...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle',...
'Color',[0.6 0.8 0.8]);
hold on
hp = scatter(vx(:),vy(:),50,c,'filled'); % scatter plot
I’ve tried using uistack, but it doesn’t work :(. This is what I’ve tried:
uistack(ht,'bottom')
uistack(hp,'top' )
Any help will be greatly appreciated!! Thank you very much!!

 Accepted Answer

There's definitely something a little odd going on (or, at the very least, you can add me to the list of people who don't understand what's happening here).
When I simplify the call to the text command,
ht = text(500,45,'Trial','FontSize',192,'Color',[0.6 0.8 0.8]);
and use
uistack(ht,'bottom');
the 'Trial' text appears underneath the dots, as you expected. (I increased the size of the dots to 200, to exaggerate them.)
I have not fully investigated what's happening, but it seems to be something with the call to text(), for sure. I don't know which property or combination of properties might be causing he problem, but it does seem a bit buggy.
Another bit of weirdness I noticed is that if I plotted your figure:
print('-dpng','-r300','test.png')
the 'Trial' text does not appear centered in the figure at all, but is well off to the side, actually outside the figure.

4 Comments

Hi there!
Thanks so much for looking into this :).
It also seems buggy to me. I simplified the text function, as you suggested, and it works. The text appears underneath the dots. I then added only one property at a time to see which one would make the text appear on top. All properties seem well behaved, except:
...'Units', 'normalized',...
When this property is removed, the uistack function works perfectly and the text appears bellow the bubbles. But as soon as this property is included, uistack does not work, and the text only appears on top.
Do you know what I can do to fix this bug?
Thank you so much for your help!!! I really appreciate it!!!
oh and as for the command
print('-dpng','-r300','test.png')
I also don't get the text on the figure :(.
Personally, I would distill this into the simplest possible example that exhibits the problem, and submit it as a bug. You might want to wait to see if Walter responds to my comment on his answer, as he may have some additional insight to add.
Hi! Thanks so much for following up on this!!
I'll wait and see if Walter responds. If not, I'll submit it as a bug once I simplify it, as you suggested.
Bests :)

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 24 Jan 2012
The easiest way to get an object to appear below something else is to give the first object a more negative z coordinate than the second object.
If you happened to be using OpenGL (which would be necessary if you were using transparency or certain kinds of lighting) then this would be the approach you would have to take, as OpenGL does not obey stacking order when two objects fall in the same plane.

1 Comment

Out of curiosity, are you able to do what you propose for Susana's specific example? I tried the following, none of which worked for me:
- Change the z-coordinate of the text's position property
- Change the ZData property of the dots
- Change the order of the Children of the axes (which neither of us mentioned, but typically works)
- uistack() command
My Renderer for this figure is zbuffer. When I change the Renderer, using
set(gcf,'Renderer','OpenGL')
the plot remains the same except the dots actually change positions! This totally baffles me. My guess is that it is a bug related to the normalized units, as mentioned in one of Susana's comments to my answer.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!