Code covered by the BSD License  

Highlights from
Data space to figure units conversion

3.93333

3.9 | 15 ratings Rate this file 53 Downloads (last 30 days) File Size: 2.24 KB File ID: #10656

Data space to figure units conversion

by Scott Hirsch

 

06 Apr 2006 (Updated 06 Jan 2010)

A utility function to simplify use of annotation function

| Watch this File

File Information
Description

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.

Acknowledgements
This submission has inspired the following:
axescoord2figurecoord
MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (17)
05 Oct 2006 kerr hatrick.

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

12 Oct 2006 Axiom .

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

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.

06 Dec 2006 Lucian P

Doesn't work in R12a

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.

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.

25 Apr 2007 Tim Mullen  
19 Feb 2008 Scott McClure

This function does not deal with log axes.

17 Mar 2008 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);

24 Sep 2008 Matt Brunner

Works fine for me. Thanks for sharing.

20 Mar 2009 Yan Luo

working for me. great

27 May 2009 Maxie

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

16 Aug 2010 Michel

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.

03 Sep 2010 Yuri Kotliarov  
06 Jul 2011 Nikolas

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

03 Aug 2011 Michael

Doesn't work with "axis square".

07 Feb 2012 Hannes

perfect tool fpr annotation, thank you!

Please login to add a comment or rating.
Updates
06 Jan 2010

Updated copyright.

Tag Activity for this File
Tag Applied By Date/Time
annotation Scott Hirsch 22 Oct 2008 08:21:33
customization Scott Hirsch 22 Oct 2008 08:21:33
units Scott Hirsch 22 Oct 2008 08:21:33
normalized Scott Hirsch 22 Oct 2008 08:21:33
data Scott Hirsch 22 Oct 2008 08:21:33
figure Scott Hirsch 22 Oct 2008 08:21:33
annotation Nikolas 06 Jul 2011 09:17:09
customization Nikolas 06 Jul 2011 09:17:11
data Nikolas 06 Jul 2011 09:17:12
figure Nikolas 06 Jul 2011 09:17:14
normalized Nikolas 06 Jul 2011 09:17:15
units Nikolas 06 Jul 2011 09:17:17
arrow Hannes 07 Feb 2012 12:47:56
annotation Hannes 07 Feb 2012 12:47:56
coordinates Hannes 07 Feb 2012 12:47:56
unit conversion Hannes 07 Feb 2012 12:47:56
ds2nfu Hannes 07 Feb 2012 12:47:56

Contact us at files@mathworks.com