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

Gnuplot计算坡度并转换为标签对齐的角度,gnuplot,Gnuplot,我正在和一位同事的Gnuplot脚本一起做一些内部报告,我对这个很棒的工具了解了很多,我过去只接触过这个工具 我正在重构代码,这样我就可以更改驱动绘图的全局变量,比如年度目标等等。我已经设法让Gnuplot根据目标自动调整图表的大小。简单: set yrange [0:${COURSETARGET}*1.2] set y2range [0:${ATTENDANCETARGET}*1.2] 我现在想做的是,根据箭头的斜率自动调整标签的旋转角度(例如,一个绘制每年课程目标梯度的箭头),但我不知道如

我正在和一位同事的Gnuplot脚本一起做一些内部报告,我对这个很棒的工具了解了很多,我过去只接触过这个工具

我正在重构代码,这样我就可以更改驱动绘图的全局变量,比如年度目标等等。我已经设法让Gnuplot根据目标自动调整图表的大小。简单:

set yrange [0:${COURSETARGET}*1.2]
set y2range [0:${ATTENDANCETARGET}*1.2]
我现在想做的是,根据箭头的斜率自动调整标签的旋转角度(例如,一个绘制每年课程目标梯度的箭头),但我不知道如何做到这一点

我在图0中有一个箭头
,从图0到图1,第二个${TARGET}
,因此根据目标是什么(从bash变量输入),从左下角到绘图右侧的一个点有一个斜率

我已经手动将标签(“目标线”)旋转了16º,目前大致正确,但如果我的图表区域发生变化,我将不得不通过反复尝试来计算角度

因此,在我开始研究三角函数之前,我想问一下Gnuplot是否有任何内置的方式来获取箭头并返回其梯度,从中我可以计算出将标签与直线对齐所需的旋转角度


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

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
这就产生了


我认为这很优雅。除了角度之外,我还喜欢计算标签的位置。谢谢<代码>设置大小比率r修改图形的宽度。通常不是问题,除非我有一个多点图,下面的图有不同的y刻度。如果我可以使用图形坐标返回宽度,我可以在
atan(x)
函数中使用它。@Jangari为了获得正确的对齐,在
atan
中输入计算的宽度/高度应该确实在屏幕坐标中,
设置大小比率的另一种方法是明确指定图像的宽度/高度以及打印边距,然后手动计算x/y轴的长度(以像素为单位)(因此也是它们的比率)…通过一些实验,multiplot无疑让我感到厌烦。显式地将绘图大小设置为1,0.6,并在我的atan函数中使用该值(
atan(0.6/1.2)
,其中1.2是绘图与Y轴箭头高度的比率)返回26º。太远了。因为我将原点设置为0,0.4,所以我认为绘图垂直压缩因子为0.6。因此,
atan(0.6*0.6/1.2)
=>
atan(0.3)
返回16.7º。谢谢你的帮助!