Gnuplot calculate slope and convert to angle for label alignment -
i'm working colleague's gnuplot script internal reporting do, , i'm learning lot awesome tool have dabbled in past.
i'm refactoring code can change global variables drive plot, annual targets , on. have managed gnuplot resize chart automatically based on targets. easy:
set yrange [0:${coursetarget}*1.2] set y2range [0:${attendancetarget}*1.2]
what now, automatically adjust angle of rotation of label based on slope of arrow (e.g., arrow plots gradient of annual target of courses delivered), have no idea how 1 that.
i have arrow from graph 0, graph 0 graph 1, second ${target}
, slope bottom-left point on right-side of plot depending on target (fed in bash var).
i've manually rotated label ('target line') 16ยบ, right now, if chart area changes, i'll have figure out angle trial-and-error on again.
so before start boning on trigonometry, thought i'd ask whether gnuplot has built-in way of taking arrow , returning gradient, calculate angle of rotation needed align label line.
perhaps not elegant way, 1 example proceed as:
reset set terminal pngcairo set output 'fig.png' unset key r = 0.75 xmin = 10. xmax = 120. ymin = 0. ymax = 100. y2min = 500. y2max = 1000. y2ref = 800. set angles degrees #explicitly set ratio of y/y2-axis "display" length x-axis length set size ratio r #set ranges set xr [xmin:xmax] set yr [ymin:ymax] set y2r [y2min:y2max] set y2tics set arrow graph 0, graph 0 graph 1, second y2ref #the tangent of angle of interest given dy/dx*r, #dy fraction of y2axis (one side of triangle), dx #the fraction of x-axis spanning bottom side of triangle #(dx=1 in particular case since arrow drawn across #entire graph) , r used in order convert between "display #units on x/y axes". result in degrees because of: set angles degrees. alpha = atan((y2ref - y2min)/(y2max - y2min) * r) x0 = (xmin + xmax)/2 y0 = (y2ref - y2min)/(xmax - xmin)*(x0 - xmin) + y2min set label "some label" @ x0, second y0 rotate alpha offset 0, char 1.*cos(alpha) plot 1/0 #just show plot
this produces
Comments
Post a Comment