File Exchange Data space to figure units conversion

version 1.2.0.0 (105 KB) by Michelle Hirsch

Michelle Hirsch (view profile)

A utility function to simplify use of annotation function

Updated 17 Aug 2015

The annotation function, which allows you to programmatically add a wide range of annotations to your figure, requires coordinates to be specified in normalized figure units. I have found that I almost always want to specify my annotations in data space (i.e., based on the values of data displayed in an axes).
This utility function converts coordinates in data space into normalized figure coordinates, for input to annotation. Some annotations require you to specify (x,y) pairs, while others require a 4 element position vector. This function supports both syntaxes.

Here's a simple example:
% Create some data
t = 0:.1:4*pi;
s = sin(t);

% Add an annotation requiring (x,y) coordinate vectors
plot(t,s);ylim([-1.2 1.2])
xa = [1.6 2]*pi; % X-Coordinates in data space
ya = [0 0]; % Y-Coordinates in data space
[xaf,yaf] = ds2nfu(xa,ya); % Convert to normalized figure units
annotation('arrow',xaf,yaf) % Add annotation

Note: I believe annotation was introduced in MATLAB 7.

Curt Laubscher

Michelle Hirsch

Michelle Hirsch (view profile)

Thanks for the comment, Ulrich. This utility works with standard zooming - e.g. the zoom command, the zoom toolbar button, etc. It is not designed to work with camera interactions at all.

Ulrich Reif

Ulrich Reif (view profile)

The program ignores zooming:
plot(-2:2,[-2:2].^2,'-o')
camva(camva/2), grid on
[u,v] = ds2nfu(-1,1);
annotation('textarrow',[u u],[v v])

J.R.

J.R. (view profile)

Since this doesn't work with different aspect ratios, I downloaded plotboxpos from:

http://www.mathworks.com/matlabcentral/fileexchange/9615-plotboxpos

This file returns the position of the plotted axis region with the aspect ratio included. I changed line 85 from

axpos = get(hAx,'Position');

to

axpos = plotboxpos(hAx);

Now it works with axis image, equal or square.

Daniel Burke

Daniel Burke (view profile)

Really great 'simple' solution! Thanks very much

Anton Daneyko

Jan Neggers

Jan Neggers (view profile)

works well, thank you

Chris Godau

Chris Godau (view profile)

Very useful, thank you. I found that changing the axis (e.g. through axis equal) only after adding annotations avoids any problems with the aspect ratio.

Natan

Natan (view profile)

Please consider adding functionality for the case PlotBoxAspectRatio is not [1 1 1] (in case axis image for example)

Lenka

thanks a lot

Chani

Fernando

Abraham Asfaw

Abraham Asfaw (view profile)

Thanks for the inspiration! I've created a version of this file with support for logarithmic axes. Here's the link.

http://www.mathworks.com/matlabcentral/fileexchange/42475

Deepika Mohankumar

Deepika Mohankumar (view profile)

Got what I was looking for a long time.. Thanks.

Hannes

Hannes (view profile)

perfect tool fpr annotation, thank you!

Michael

Michael (view profile)

Doesn't work with "axis square".

Nikolas

Nikolas (view profile)

I am using Matlab R2009b and I used this function but the coordinates that gives me back are flipped. Why?

Yuri K

Michel

Michel (view profile)

To manage log scale I have added the following line after line 86:
-----Begin code--------
if strcmp(get(hAx,'Xscale'),'log')
axlim(1:2)=log10(axlim(1:2));
x=log10(x);
end

if strcmp(get(hAx,'Yscale'),'log')
axlim(3:4)=log10(axlim(3:4));
y=log10(y);
end
----End Code----

This works for coordinates but I didn't check for position vector. Other than that it works for me.

Maxie

Maxie (view profile)

Very useful!
Can add text at the tail of the arrow using 'textarrow' ?
Thanks!

Yan Luo

Yan Luo (view profile)

working for me. great

Matt Brunner

Works fine for me. Thanks for sharing.

Till Junge

Your function doesn't work with equal axes. example:
function bla()
close all
a=pi/4;
r=0:1e-2:1;
f=@(r) 1/2*a*log(1+(tan(a*r)).^2);
createfigure(r,f(r),f)

function createfigure(X1, Y1,f)
%CREATEFIGURE(X1,Y1)
% X1: vector of x data
% Y1: vector of y data

% Auto-generated by MATLAB on 17-Mar-2008 14:49:27

% Create figure
figure1 = figure('XVisual',...
'0x22 (TrueColor, depth 24, RGB mask 0xff0000 0xff00 0x00ff)');

% Create axes
axis1=axes('Parent',figure1);
box('on');
grid('on');
hold('all');
axis equal
ylim([0,.3])
xlim([0,1])

% Create plot
plot(X1,Y1);
r=.7
the=r*pi/4
[vpx,vpy]= ds2nfu(gca,[r r],[0,f(r)]);
annotation(gcf,'arrow',vpx,vpy);
[nx,ny] = ds2nfu(gca,[r r-.2*sin(the)],[f(r) f(r)+.2*cos(the)]);
annotation(gcf,'arrow',nx,ny);

Scott McClure

This function does not deal with log axes.

Tim Mullen

Benoît Valley

This a nice and useful code, but it do not work if DataAspectRatioMode or PlotBoxAspectRatioMode are set to 'manual'. So it would be even nicer if this code would be improved to managed such case.

Scott Hirsch

Lucian -

Sorry about that! I was using a syntax introduced in R13. Easy enough fix - change '&&' to '&' in line 58 (2 times). I tested in R12, and it works with this fix. This should work for slightly older versions, too. Note that the examples won't work, though, since ANNOTATION was introduced sometime after R12.

Lucian P

Doesn't work in R12a

Vivek Goyal

Easy to use, and seems to work correctly. As soon as I learned of the annotation function, I realized that I needed this coordinate conversion function. Thanks.

Axiom .

Works as advertised. A necessity if you prefer to do your annotations in code rather than editing the plot. Thanks!

kerr hatrick.

very useful util. for purposes of automatic plot annotation. thx++ mr Hirsch.