help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Put Label or something else in rel coordinats on a plot


From: Stefan Pofahl
Subject: Put Label or something else in rel coordinats on a plot
Date: Thu, 3 Dec 2009 20:18:43 +0100

Hello,

I don't know if there is a clever way to give a position
on a plot in relative coordinates x = [0 .. 1], y = [0 .. 1].

So I wrote my own function that does it:

You call it like that:
[x_abs, y_abs] = mylib_rel_position(gca(), [x_rel, y_rel])

Maybe it is of interest for others:

Regards,

Stefan

****
function [x_pos, y_pos] = mylib_rel_position(hd_gca, xy_pos)
  ## Purpose calc rel. coordinates in absolute coordinats in x- /
y-range of an existing graph
  ## 1st argument is the graphics handle of the axis object hd_gca = gca();
  ## 2nd is an 1x2 vector of the rel. position, both coordinats are in [0 .. 1]
  x_pos = 0;  ## to be sure to have an output
  y_pos = 0;
  ## Input check:
  if(nargin < 2)
    error("not enough arguments, 1st: axis handle; 2nd: [rel x
position, rel y position]");
  endif
  if (! ishandle(hd_gca))
    error("1st argument is no axis handle");
  endif
  if(max(size(xy_pos)) < 2)
    error("2nd argument needs to be an two element array / vector");
  endif
  ## Main Part
  ## check range
  xy_pos(1) = min(max(xy_pos(1), 0), 1); ## xy_pos(1) has to be in the
range of [0,1]
  xy_pos(2) = min(max(xy_pos(2), 0), 1);
  ## ---
  x_lim = xlim(hd_gca);
  y_lim = ylim(hd_gca);
  x_pos_rel = xy_pos(1);
  y_pos_rel = xy_pos(2);
  x_pos = x_lim(1) + x_pos_rel*(x_lim(2) - x_lim(1));
  y_pos = y_lim(1) + y_pos_rel*(y_lim(2) - y_lim(1));
endfunction ## [x_pos, y_pos] = mylib_rel_position(hd_gca, xy_pos)
-- 
Tel.: 0731-3805149
Ochsensteige 48
89075 Ulm


reply via email to

[Prev in Thread] Current Thread [Next in Thread]