Outputting text on subplot with mixed normalised/normal coordinates
2 views (last 30 days)
Show older comments
I have subplot on a figure and i want to place some text outside the plot as below (i.e. the text '80th pct' and '20th pct'

I started off with
text(1,1,'Your String','Units','normalized')
Whilst I need the x coordinate to be at the extreme, the y coordiante depends on the y value of the green dotted line (that I know).
So I'm not sure how to mix the cooridnates so x is normalised but y is fixed by a variable. Or maybe there is a better way. (I have another 6 subplots on this figure)
0 Comments
Accepted Answer
Walter Roberson
on 21 Jan 2016
There might be a different way, this is what comes to mind:
label_offset_pixels = 15; %space in pixels between axes box and text to write
ax = gca;
old_units = get(ax, 'Units');
set(ax, 'Units', 'pixels');
ax_pos = get(ax, 'Position');
set(ax, 'Units', old_units);
label_x_pos_pixels = ax_pos(1) + ax_pos(3) + label_offset_pixels;
probe_text = text(label_x_pos_pixels, 1, 'test', 'Units', 'pixels');
set(probe_text, 'Units', 'data');
probe_pos_data = get(probe_text, 'Position');
label_x_pos_data = probe_pos_data(1);
delete(probe_text);
text(label_x_pos_data, y20, '20th pct');
text(label_x_pos_data, y80, '80th pct');
What this does is find the right boundary of the axes in pixels, add a margin, convert that to data units, and use the data units for the text position.
There is a considerably shorter way to find the normalized desired y coordinate, using the axes YLim properties, but that does not give you a fixed spacing between the box and the text. Though now that I think of it, you could do that, convert the result to Pixels, add the offset... yes, I guess that would be shorter in spirit
label_offset_pixels = 15; %space in pixels between axes box and text to write
ax = gca;
ax_ylim = get(ax, 'Ylim');
normalized_y20_80 = ([y20, y80]-ax_ylim(1))./(ax_ylim(2)-ax_ylim(1));
pct_text = text([1, 1], normalized_y20_80, {'20th pct', '80th pct'}); %data units is default
set(pct_text, 'Units', 'pixel');
pct_text_pos = get(pct_text, 'Position');
set(pct_text, {'Position'}, cellfun(@(xyz) xyz+[label_offset_pixels 0 0], pct_text_pos,'Uniform', 0));
set(pct_text, 'Units', 'data');
3 Comments
Walter Roberson
on 22 Jan 2016
label_offset_pixels = 15; %space in pixels between axes box and text to write
ax = gca;
ax_ylim = get(ax, 'Ylim');
normalized_y20_80 = ([y20, y80]-ax_ylim(1))./(ax_ylim(2)-ax_ylim(1));
pct_text = text([1, 1], normalized_y20_80, {'20th pct', '80th pct'}, 'Units', 'normal');
set(pct_text, 'Units', 'pixel');
pct_text_pos = get(pct_text, 'Position');
set(pct_text, {'Position'}, cellfun(@(xyz) xyz+[label_offset_pixels 0 0], pct_text_pos,'Uniform', 0));
set(pct_text, 'Units', 'normal');
More Answers (0)
See Also
Categories
Find more on Axes Appearance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!