специфическая для метки gnuplot точка данных со значением x - PullRequest
0 голосов
/ 05 января 2019

Как я могу пометить точку данных на определенном значении y соответствующим значением 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

Что дает этот участок:

enter image description here

Предположим, меня интересует отображение единственного значения y 0,89. Как пометить пересечение на графике соответствующим значением x в часах, как на этом графике:

enter image description here

Я знаю, что проблема в том, что точка не существует в моих данных, но возможно ли это в любом случае? Если нет, то как это сделать с существующим значением y рядом с требуемым значением, например, это значение:

2019-01-04-19:51:21,0.919

1 Ответ

0 голосов
/ 05 января 2019

Проверьте следующее. В этом вам поможет троичный оператор. Метка первого значения, которое ниже определенного порога, отображается в виде метки. Конечно, было бы лучше интерполировать значение на пороге, что также может быть сделано, но требует некоторого дополнительного кодирования.

### 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

Что приводит к:

enter image description here

Дополнительно:

Еще одно улучшенное, более сложное решение находит интересующую точку (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

enter image description here

...