Gnuplot计算斜率并转换为标签对齐的角度

时间:2017-04-07 05:09:36

标签: gnuplot

我正在与同事的Gnuplot脚本合作进行一些我们所做的内部报告,而且我正在学习很多关于这个我过去只涉及的这个很棒的工具。

我正在重构代码,以便我可以更改驱动绘图的全局变量,例如年度目标等。我设法让Gnuplot根据目标自动调整图表大小。易:

set yrange [0:${COURSETARGET}*1.2]
set y2range [0:${ATTENDANCETARGET}*1.2]

我现在想做的是,根据箭头的斜率自动调整标签的旋转角度(例如,绘制年度目标的梯度的箭头),但我没有想法如何做到这一点。

我有一个箭头from graph 0, graph 0 to graph 1, second ${TARGET},因此根据目标是什么(从bash变量中输入),从左下角到右下角的一个斜率。

我手动将标签(“目标线”)旋转了16度,现在这是正确的,但如果我的图表区域发生变化,我将不得不通过反复试验找出角度。试。

所以在开始我的三角测量之前,我想我会问Gnuplot是否有任何内置的方法来获取箭头并返回其渐变,我可以从中计算出对齐标签所需的旋转角度在线。

enter image description here

1 个答案:

答案 0 :(得分:1)

也许不是最优雅的方式,但可以举例如下:

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 the ratio of y/y2-axis "display" length to 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 from graph 0, graph 0 to graph 1, second y2Ref

#the tangent of the angle of interest is given as dy/dx*r, where
#dy is the fraction of the y2axis (one side of the triangle), dx is
#the fraction of the x-axis spanning the bottom side of the triangle
#(dx=1 in this particular case since the arrow is drawn across the
#entire graph) and r is used in order to convert between the "display
#units on x/y axes". Result is 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" at x0, second y0 rotate by alpha offset 0, char 1.*cos(alpha)

plot 1/0 #just to show the plot

这会产生

enter image description here