Scatter plot text behind plotted data when image copied.

I have a figure containing a scatter plot overlaid with a fitted poly. I have added some text labels to the data.
When I copy the image and paste as enhanced metafile, the text appears behind the plotted data, even when I set the text object to 'top' and/or set the current axes to 'top':
t = text(x, y, str); % Adding a text label
uistack(t, 'top'); % Bring the text to the front
set(gca, 'Layer', 'top');
If I change the renderer to 'opengl', the image copies correctly, but the pasted image quality is very poor.
This behaviour is repeatable on any figure of this type.

2 件のコメント

Animesh
Animesh 2024 年 5 月 2 日
編集済み: Animesh 2024 年 5 月 2 日
Hey @Kirstie Beugger, can you attach the code as well as the data file (if any) for better understanding of the issue.
Kirstie Beugger
Kirstie Beugger 2024 年 5 月 2 日
編集済み: Voss 2024 年 5 月 2 日
Hi Animesh,
Thanks for considering my issue.
This is the original code used to add the text to the plot:
function [t] = addText(x,y,xtxt,ytxt,n)
figure(n)
hold on
legend('AutoUpdate','off')
plot(x,y,"ko")
str = {strcat(xtxt,'%'),ytxt};
t = text(x,y,str);
hold off
end
This is the (simplified) code used to create the figure in the first place:
figure(i)
clf
hold on
%plot the raw data scatter plots
s1 = scatter(data_x,data_y);
s1.MarkerEdgeColor = '#E1E1E1';
%Create a fit object for the data and plot it
[fitresult, ~] = createFit_poly2(data_x,data_y);
plot(fitresult_SandFx(0:max(data_y)), 'k')
%set the correct display for the y axis values
ax = ancestor(s3,'axes');
ax.YAxis.Exponent = 3;
ytickformat('%.0f')
title('Plot Title');
ylabel('y axis label');
xlabel('x axis label');
grid('on');
hold off
saveas(figure(i),strcat('figure ', string(i)))

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2024 年 5 月 2 日
編集済み: Adam Danz 2025 年 7 月 11 日

1 投票

Thanks for the info @Kirstie Beugger. This bug was fixed in R2025a.
For releases prior to R2025a:
When copying graphics in vector format the figure's renderer is switched to painters which causes the change in text position.
A workaround is to set the z coordinate of the text to a value greater than 0.
tobj = findobj(gca,'type','text');
for i = 1:numel(tobj)
tobj(i).Position(3) = 1;
end

3 件のコメント

Kirstie Beugger
Kirstie Beugger 2024 年 5 月 2 日
移動済み: Adam Danz 2024 年 5 月 3 日
Oh great, thanks Adam. I'm glad it's a bug, I've been wrestling with it for a while.
Kirstie Beugger
Kirstie Beugger 2024 年 5 月 6 日
移動済み: Adam Danz 2024 年 5 月 6 日
Mint, thanks a lot Adam!
For anyone else who needs to use this, Chat changed the code slightly:
tobj = findobj(gca, 'type', 'text');
for i = 1:numel(tobj)
tobj(i).Position(3) = 1;
end
Adam Danz
Adam Danz 2024 年 5 月 6 日
Thanks for correcting that, Kirstie. Forgot the numel(). I fixed my answer.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2023a

質問済み:

2024 年 5 月 2 日

編集済み:

2025 年 7 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by