【问题标题】:How to plot a table as png using gnuplot?如何使用gnuplot将表格绘制为png?
【发布时间】:2018-02-20 15:55:24
【问题描述】:

我在网上四处搜寻,但找不到足够的解决方案来解决我的问题。我有一些我的 cpu 使用情况随时间推移的 csv 填充数据(每天拆分)

Time CPU used%
1    1   35
2    1   28
3    1   20
4    1   45
5    1   15

对于我笔记本电脑中的每个 CPU。最重要的是,我有一些关于我从最后一天的使用情况的统计计算结果,例如

CPU Max% Avg%
1   98  17
2   99  23
3   95  10
etc.

使用 gnuplot 我可以绘制我的时间序列数据的线图并获得不错的结果。但是,我还想将统计数据添加为我的折线图下方的表格。

但我找不到使用 gnuplot 将原始数据绘制为表格的方法。有人知道方法吗?它不一定是 gnuplot,我只想使用 shell 命令自动生成绘图,所以 gnuplot 非常方便。

有人知道吗?

【问题讨论】:

  • 您可以将其中的一些添加为标签,但我建议使用适当的文档设置工具,例如HTML、Latex 等
  • 嗨@Thor,你有乳胶的例子吗?我也考虑将这种方式与 gnuplot 结合使用,但我不确定它是如何工作的。
  • 可能是这样的 - tex.stackexchange.com/a/146719
  • 这看起来也像good example。请注意,此答案使用 tikz 来绘制数据,这可能是一个好主意,因为您可以均匀化字体等。
  • Stack Overflow 是一个编程和开发问题的网站。这个问题似乎离题了,因为它与编程或开发无关。请参阅帮助中心的What topics can I ask about here。也许Super UserUnix & Linux Stack Exchange 会是一个更好的提问地点。

标签: linux shell plot gnuplot tabular


【解决方案1】:

尽管这个悬而未决的问题已有 3 年多的历史了,但 OP 或其他人可能仍会对它感兴趣。 尚不完全清楚您的完整数据是什么样子的,即是存储在单个文件中还是存储在单独的文件中。 所以,我假设了一些方便处理的东西。它当然可以适应其他格式。

  • 数据会有时间戳、CPU数量和CPU使用值。
  • 列分隔符是tab
  • 不同的 CPU 由两个空行分隔,这样您可以通过 index 寻址不同的块。
  • gnuplot 可以通过stats 进行统计。
  • 要将统计信息打印到绘图中(作为“表格”),只需使用标签(检查help label)。
  • 下面的例子是创建一些随机的虚拟数据。
  • 还可以轻松添加最低 CPU 使用率。

数据:(示例一个文件,实际上这里:一个数据块)

# date time             CPU     usage
2021-08-01 00:00:00     1       14.9574
2021-08-01 00:05:00     1       16.1165
2021-08-01 00:10:00     1       17.6627
2021-08-01 00:15:00     1       14.8799
...


2021-08-01 00:00:00     2       18.8313
2021-08-01 00:05:00     2       17.6595
2021-08-01 00:10:00     2       19.1912
2021-08-01 00:15:00     2       18.731
...


...
2021-08-01 00:00:00     8       39.9016
2021-08-01 00:05:00     8       37.8723
2021-08-01 00:10:00     8       40.2769
2021-08-01 00:15:00     8       38.9694

代码:

### plot time series and statistics label
reset session

myTimeFmt = "%Y-%m-%d %H:%M:%S"
t0 = strptime(myTimeFmt,"2021-08-01")
Ncpu = 8

# create some random test data
set print $Data
    p(t) = (_tmp=p0+rand(0)*6-3, _tmp>100 ? 100 : _tmp<0 ? 0 : _tmp)
    do for [cpu=1:Ncpu] {
        p0 = rand(0)*100
        do for [t=t0:t0+24*3600:5*60] {       # steps in 5 minutes for 1 day
            print sprintf("%s\t%d\t%g",strftime(myTimeFmt,t),cpu,p0=p(0))
        }
        print ""; print ""   # two empty lines
    }
set print

set datafile separator tab
set format x "%H:%M" time
set key out
set grid x,y
set xlabel sprintf("%s",strftime("Date: %Y-%m-%d",t0))
set ylabel "CPU usage / %" font ",12"

# create statistics:
set print $Statistics
    do for [i=1:Ncpu] {
        stats $Data u 3 index i-1 nooutput
        print sprintf("%d %.1f %.1f",i,STATS_max,STATS_mean)
    }
set print

set bmargin screen 0.25
myLabelPosY = 0.11

set label 1 at screen 0.02, screen myLabelPosY "\nMax:\nAvg:"
do for [i=1:Ncpu] {
    set label i+1 at screen 1./(Ncpu+1)*i, myLabelPosY \
       sprintf("CPU %d:\n%s%%\n%s%%",i,word($Statistics[i],2),word($Statistics[i],3)) 
}

plot for [i=1:Ncpu] $Data u (timecolumn(1,myTimeFmt)):3 index i-1 w l ti sprintf("CPU %d",i)
### end of code

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多