如何用特定的y值将数据点标记为其对应的x值?额外的困难是x值应以小时为单位,而不是日期。
我有以下数据: data.csv:
2019-01-04-11:02:24,1.468
2019-01-04-12:40:54,1.212
2019-01-04-15:16:22,1.123
2019-01-04-17:04:43,1.067
2019-01-04-18:51:45,0.994
2019-01-04-19:51:21,0.919
2019-01-04-20:46:40,0.8
我将其绘制为:
set datafile separator ","
set terminal png size 600,300 enhanced font 'Verdana,10' linewidth 1
set output 'data.png'
set xdata time
MyTimeFormat = "%Y-%m-%d-%H:%M:%S"
set timefmt MyTimeFormat
set format x "%tH"
set xlabel "hours"
set ylabel "Volt"
set grid ytics xtics mxtics
StartTime = strptime(MyTimeFormat,"2019-01-04-11:01:31")
EndTime = strptime(MyTimeFormat,"2019-01-04-20:47:17")
plot 'data.csv' u (timecolumn(1)-StartTime):2 w lines lw 1 notitle,\
'' u (timecolumn(1)-StartTime):2:1 w labels notitle
这给出了这个情节:
让我们假设我有兴趣显示单个y值0.89。如何在小时图上用相应的x值标记交点,如此图所示:
我知道问题是我的数据中不存在该点,但是仍然有可能吗?如果不是,那么如何使用接近所需值的现有y值(例如,该值)进行处理:
2019-01-04-19:51:21,0.919
答案 0 :(得分:1)
检查以下内容。三元运算符在这里为您提供帮助。 低于某个阈值的第一个值的标签被绘制为标签。 当然,最好在阈值处插值,也可以这样做,但需要更多编码。
### threshold value as label
reset session
$Data <<EOD
2019-01-04-11:02:24,1.468
2019-01-04-12:40:54,1.212
2019-01-04-15:16:22,1.123
2019-01-04-17:04:43,1.067
2019-01-04-18:51:45,0.994
2019-01-04-19:51:21,0.919
2019-01-04-20:46:40,0.8
EOD
set datafile separator ","
#set terminal png size 600,300 enhanced font 'Verdana,10' linewidth 1
#set output 'data.png'
set xdata time
MyTimeFormat = "%Y-%m-%d-%H:%M:%S"
set timefmt MyTimeFormat
set format x "%tH"
set xlabel "hours"
set ylabel "Volt"
set grid ytics xtics mxtics
StartTime = strptime(MyTimeFormat,"2019-01-04-11:01:31")
EndTime = strptime(MyTimeFormat,"2019-01-04-20:47:17")
set multiplot layout 2,1
OffsetY = 0.1
plot $Data u (timecolumn(1)-StartTime):2 w lp lt 7 lc rgb "red" notitle,\
'' u (timecolumn(1)-StartTime):($2+OffsetY):(sprintf("%.2f",(timecolumn(1)-StartTime)/3600)) w labels notitle
Threshold = 1.1
a = NaN
Flag = 1
plot $Data u (timecolumn(1)-StartTime):2 w lp lt 7 lc rgb "blue" notitle,\
'' u (a = timecolumn(1)-StartTime):\
($2+OffsetY):\
(($2<Threshold) & (Flag==1) ? (Flag=0, sprintf("%.2f|%g",a/3600,Threshold)) : "") w labels notitle
unset multiplot
### end of code
这将导致:
添加:
另一种更复杂的改进解决方案是在第一遍中找到兴趣点(POI),进行插值,最后添加线条和标签。
### threshold value as label
reset session
$Data <<EOD
2019-01-04-11:02:24,1.468
2019-01-04-12:40:54,1.212
2019-01-04-15:16:22,1.123
2019-01-04-17:04:43,1.067
2019-01-04-18:51:45,0.994
2019-01-04-19:51:21,0.919
2019-01-04-20:46:40,0.8
EOD
set datafile separator ","
# set terminal png size 600,300 enhanced font 'Verdana,10' linewidth 1
# set output 'data.png'
set xdata time
MyTimeFormat = "%Y-%m-%d-%H:%M:%S"
set timefmt MyTimeFormat
set format x "%tH:%tM"
set xlabel "hours"
set ylabel "Volt"
set grid ytics xtics mxtics
# settings
Threshold = 0.89
LabelOffsetY = 0.1
Flag = 0
LastX = LastY = TempX = TempY = POIx = NaN
# formula for interpolation and finding the threshold x value
Interpolate(x0,y0,x1,y1,xi) = y0 + (y1-y0)/(x1-x0)*(xi-x0)
ThresholdX(X,Y) = ((LastY>=Threshold) & (Y<Threshold) & (Flag==0)) ? \
(Flag=1, POIx = Interpolate(LastY,LastX,Y,X,Threshold)) : NaN
set table $Dummy
plot $Data u ($0 == 0 ? (StartTime = timecolumn(1), 0) : LastX = TempX, TempX = timecolumn(1)-StartTime, LastY = TempY, TempY = $2, ThresholdX(TempX,TempY)):\
(TempY) w table
unset table
# lines and label
set arrow 1 from graph 0, first Threshold to POIx,Threshold lc rgb "red" nohead
set arrow 2 from POIx,graph 0 to POIx,Threshold lc rgb "red" nohead
set arrow 3 from POIx,Threshold+LabelOffsetY*0.8 to POIx,Threshold lc rgb "black" lw 1.5
set style textbox opaque
set label 1 at POIx,Threshold+LabelOffsetY sprintf("%.2f/%.2f", POIx/3600, Threshold) boxed front
# plotting data
plot $Data u (timecolumn(1)-StartTime):2 w lp lt 7 lc rgb "blue" notitle
### end of code