Get from Ico-github-logo

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Data space to figure units conversion

  • ds2nfu(varargin)
    DS2NFU Convert data space units into normalized figure units.
  • View all files
4.3 | 24 ratings Rate this file 66 Downloads (last 30 days) File Size: 105 KB File ID: #10656 Version: 1.2

Data space to figure units conversion



06 Apr 2006 (Updated )

A utility function to simplify use of annotation function

| Watch this File

File Information

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.


This file inspired 3 D Data Space To Figure Space Conversion, Axescoord2figurecoord, Normalized Coordinates For Annotations, 3 D Data Space Coordinates To Normalized Figure Coordinates Conversion, Coord2norm, and Simulating The Ramsey Cass Koopmans Model Using Matlab And Simulink.

Required Products MATLAB
MATLAB release MATLAB 7.2 (R2006a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (28)
04 Dec 2015 J.R.

J.R. (view profile)

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

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

axpos = get(hAx,'Position');


axpos = plotboxpos(hAx);

Now it works with axis image, equal or square.

Comment only
21 Aug 2015 Daniel Burke

Really great 'simple' solution! Thanks very much

08 Jun 2015 Anton Daneyko

06 Nov 2014 Jan Neggers

works well, thank you

18 Aug 2014 Chris Godau

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.

23 Apr 2014 Natan

Natan (view profile)

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

Comment only
30 Mar 2014 Lenka

Lenka (view profile)

thanks a lot

13 Mar 2014 Chani

Chani (view profile)

13 Sep 2013 Fernando

11 Jul 2013 Abraham Asfaw

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

12 Jun 2012 Deepika Mohankumar

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

07 Feb 2012 Hannes

Hannes (view profile)

perfect tool fpr annotation, thank you!

03 Aug 2011 Michael

Doesn't work with "axis square".

06 Jul 2011 Nikolas

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

Comment only
03 Sep 2010 Yuri K

Yuri K (view profile)

16 Aug 2010 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')

if strcmp(get(hAx,'Yscale'),'log')
----End Code----

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

27 May 2009 Maxie

Maxie (view profile)

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

20 Mar 2009 Yan Luo

working for me. great

24 Sep 2008 Matt Brunner

Works fine for me. Thanks for sharing.

17 Mar 2008 Till Junge

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

function createfigure(X1, Y1,f)
% 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
axis equal

% Create plot
[vpx,vpy]= ds2nfu(gca,[r r],[0,f(r)]);
[nx,ny] = ds2nfu(gca,[r r-.2*sin(the)],[f(r) f(r)+.2*cos(the)]);

19 Feb 2008 Scott McClure

This function does not deal with log axes.

25 Apr 2007 Tim Mullen

10 Jan 2007 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.

08 Dec 2006 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.

Comment only
06 Dec 2006 Lucian P

Doesn't work in R12a

06 Dec 2006 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.

12 Oct 2006 Axiom .

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

05 Oct 2006 kerr hatrick.

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

06 Jan 2010 1.1

Updated copyright.

17 Aug 2015 1.2

Moved source to GitHub.

Contact us