I want to rotate a label to make it parallel to the arrow defined by
set arrow 1 from x,y to x+dx,y+dy
To calculate the angle of this arrow in the canvas coordinate system I need to take into account the different scales of x and y axes:
theta = atan(ratioxy*dy/dx)*180/pi,
where ratioxy is the ratio of the lengths of one unity, in y and x axes. By using this ratio I can write the properly rotated text as
set label 1 "Rotated text" at x,y left rotate by theta
So, my question is:
How can I determine the ratioxy between the length of one unit, as measured in the y and x axes?
Following a comment by @Christoph, show var GPVAL_TERM
reveals interesting variables set by gnuplot
after a plot:
GPVAL_TERM_XMIN = 440
GPVAL_TERM_XMAX = 6159
GPVAL_TERM_YMIN = 300
GPVAL_TERM_YMAX = 4639
GPVAL_TERM_XSIZE = 6400
GPVAL_TERM_YSIZE = 4800
GPVAL_TERM_SCALE = 1
So the ratio plotratio=(GPVAL_TERM_XMAX-GPVAL_TERM_XMIN)/(GPVAL_TERM_YMAX-GPVAL_TERM_YMIN)
gives the plotting zone aspect ratio. You also need to have the ratio of your plotting ranges, which is rangeratio=(GPVAL_XMAX-GPVAL_XMIN)/(GPVAL_YMAX-GPVAL_YMIN)
.
The ratio you want is ratioxy=rangeratio/plotratio
In order to have the variables set, you need to have a first pass for gnuplot. For this, use a macro:
MYPLOT='"file.dat" using (whatever(column(1)):(function(column(2))) with lines'
stats @MYPLOT
plotratio=(GPVAL_TERM_XMAX-GPVAL_TERM_XMIN)/(GPVAL_TERM_YMAX-GPVAL_TERM_YMIN)
rangeratio=(GPVAL_X_MAX-GPVAL_X_MIN)/(GPVAL_Y_MAX-GPVAL_Y_MIN)
set arrow 1 from x,y to x+dx,y+dy
theta = atan2(rangeratio/plotratio*dy,dx)*180/pi
set label 1 "Rotated text" at x,y left rotate by theta offset sin(theta),cos(theta)
plot @MYPLOT
atan2
allows you to have dx=0
. The offset
allows to have the text above the arrow and not across it.