Why does copyobj() fail to copy over title fontsizes (and also colorbars and colormaps)?
35 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Matt J
el 26 de Abr. de 2021
Editada: Benjamin Kraus
el 1 de Jul. de 2022
I am attempting to copy a set of subplot axes from one figure to another with copyobj. Everything seems to copy over quite nicely except for the title FontSizes, as illustrated in the example below. Does anyone know why?
h1=figure;
ax(1)=subplot(1,2,1);
plot(1:5,'rx:'); axis square
title('A','FontSize',15);
ax(2)=subplot(1,2,2);
plot(rand(1,5),'sb--'); axis square
title('B','FontSize',15);
h2=figure;
for i=1:2
copyobj(ax(i),h2);
end
4 comentarios
Respuesta aceptada
Benjamin Kraus
el 21 de Jun. de 2022
Matt,
The issue with copyobj and the FontSize on axes is a known bug. The issue relates to there being two sources of truth: the FontSize on the axes and the FontSize on the title. When you set the FontSize on the axes, MATLAB force that change down to the Title, whether you've manually specified the FontSize on the title or not.
ax = axes;
ax.Title.String = 'Title';
ax.Title.FontSize = 30;
ax.FontSize = 15; % This resets the FontSize on the Title.
ax.Title.FontSize % This has been updated to reflect the new FontSize * TitleFontSizeMultiplier
The issue with copyobj is the sequence in which MATLAB is (internally) copying properties from the old axes to the new axes. MATLAB copies the title's FontSize before we copy the axes FontSize, so when the axes FontSize value is copied it replaces the value copied from the old title to the new title.
Unfortunately, I don't have any good suggestions for workarounds, except to separately copy the text object (not a great workaround, but it works):
h1 = figure;
ax = axes;
title('A','FontSize',15);
h2 = figure;
ax2 = copyobj(ax,h2);
h3 = figure;
ax3 = copyobj(ax,h3);
ax3.Title = copyobj(ax.Title,ax3); % Separately copying the text object after copying the axes will preserve title properties.
3 comentarios
Benjamin Kraus
el 1 de Jul. de 2022
Editada: Benjamin Kraus
el 1 de Jul. de 2022
"why colorbars are figure children rather than axes children": That's a good question. I'm not sure why that design was chosen back when it was decided (it long predates my time at MathWorks), but if I had to guess it is due in large part to the fact that prior to R2014b colorbars and legends were just axes in disguise, and there was no functionality that allowed you to make an axes the parent of another axes.
I also agree that the colorbars (and legends) should be children of the axes. If there were an easy way to change that design, I would.
However, when you put an axes + colorbar into a TiledChartLayout, the colorbar is a child of the TiledChartLayout. Basically, in all scenarios currently, axes and colorbars (and legends) are siblings that share a parent, whether that parent is a figure, panel, or TiledChartLayout.
Más respuestas (0)
Ver también
Categorías
Más información sobre Subplots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!