Plotting lines with missing datapoints for multidimensional data

二次信任 提交于 2021-02-10 06:59:19

问题


I'm trying to plot multiple lines representing GPU usage over time from a dataset which records data of multiple GPUs. Each row contains the timestamp, a GPU index and the usage in percent.

My dataset looks like this:

$ cat gpu.txt

#time   #index   # usage (%)
1,1,10
1,2,5
2,1,20
2,2,10
3,1,40
3,2,30

and this is my gnuplot script:

$ cat plot.gplot

set datafile separator ","
set   autoscale # scale axes automatically
unset log       # remove any log-scaling
unset label     # remove any previous labels
set xtic auto   # set xtics automatically
set ytic auto   # set ytics automatically
set title
set term png

set title "GPU usage"
set xlabel "Time"
set ylabel "Usage"

set output "gpu.png"

plot "gpu.txt" using ($2 == 1 ? $1 : NaN):($2 == 1 ? $3 : NaN) title 'GPU1' with linespoints ls 10 linecolor rgb "blue", \
     "gpu.txt" using ($2 == 2 ? $1 : NaN):($2 == 2 ? $3 : NaN) title 'GPU 2' with linespoints ls 10 linecolor rgb "red", \

Unfortunately, this only ever draws the singular datapoints, but no lines. I think this is because of "missing" datapoints - which is not the case obviously because I have the custom filters in place to plot usage data per GPU index. I tried to indicate this to gnuplot via the NaN value, but it doesn't seem to work.

Example output:


回答1:


This is kind of a recurring filtering data question. You can define linestyles and then use it in the plotting loop via ls i. Essential if you want connecting lines is the line: set datafile missing NaN. My minimal suggestion would be:

Code:

### filtering data
reset session

$Data <<EOD
#time   #index   # usage (%)
1,1,10
1,2,5
2,1,20
2,2,10
3,1,40
3,2,30
EOD

set datafile separator ","
set title "GPU usage"
set xlabel "Time"
set ylabel "Usage"

set key top left
set datafile missing NaN
myFilter(datacol,filtercol,value) = value==column(filtercol) ? column(datacol) : NaN

plot for [i=1:2] $Data u (myFilter(1,2,i)):3 w lp pt 7 title sprintf('GPU%d',i)
### end of code

Result:



来源:https://stackoverflow.com/questions/65341190/plotting-lines-with-missing-datapoints-for-multidimensional-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!