How can I easily and automatically arrange text objects on a figure so they are not placed on top of each other or plots?

35 views (last 30 days)
I have a script that generates a plot, and I want to overlay on the axis text labels that show calculated values that derive from the plotted data. Notably this is not just a legend showing the labels for the plotted series, but a series of text items each of which has it's own formatting applied (or perhaps one text box which properly applies the formatting I want). If I simply call text multiple times i have to guess numbers get it to place the ext where i want on the plot which is visually unappealling, requires a lot of trial and error to place properly. I would like a way to automatically space them out properly and neatly, much the way the legend function but for arbitrary strings with arbitrary formatting.
Right now I am using something similar to this example:
figure;
plot(randn(2));
str1 = sprintf('Anjou _{%s}', 'pears');
str2 = sprintf('teddy^{%s}', 'bears');
str3 = sprintf('chocolate _{%s}', 'eclairs');
label1 = text(1.2,-0.2, str1, 'FontName', 'Arial', 'Color', 'r', 'FontSize', 14);
label2 = text(1.2,-0.5, str2, 'FontName', 'Times New Roman', 'Color', 'b', 'FontSize', 8);
label3 = text(1.2,-0.7, str3, 'FontName', 'Helvetica', 'Color', 'g', 'FontSize', 20);
Is there an easy way to have them automatically spaced and aligned without having to do it by hand?
  7 Comments
DGM
DGM on 6 Aug 2022
As far as I can tell, this is an underdefined problem. You can't really optimize anything unless you can define what the optimal conditions are. Beside from that, I don't know of any built-in tool to do text object packing. There might be something on FEX that would be relevant. This does sound like the sort of problem that surely someone has attempted before, but my intuition isn't worth much.
That said, if you want to try to roll something of your own, the text objects should have an "extent" property that describes the bounding box of the text object. You might be able to use that to test for collision with line objects, etc. The compactness of results from that strategy might be limited by the fact that the text does not fill the bounding box. If you want to only consider the space occupied by the characters, the packing strategy might get pretty complicated.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Aug 2022
The significantly easier solution to this problem is to use annotation 'arrow' and 'text' to put labels outside of the plot that point to appropriate lines. You can run into issues with ensuring that annotation lines do not cross each other (if you want to impose that requirement), but picking text boxes outside of the plot that do not clash with each other becomes much easier than trying to interpolate lines and curves to ensure that text labels inside the box do not overlap the graphics objects.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!