【问题标题】:Plotting a single value with gnuplot?用 gnuplot 绘制单个值?
【发布时间】:2015-02-09 10:08:47
【问题描述】:

我有以下 csv 文件:

timestamp,unit,maximum,average
Thu Feb 05 14:54:00 GMT 2015,Percent,13.0,4.503333333333333

当我在其上运行以下 bash 脚本时:

#!/bin/bash

graph_results() {
  input=cpu-samples.csv
  output=cpu-samples.png
gnuplot <<- eor
  set terminal png 
  set output '${output}'
  set style data linespoints
  set xdata time
  set timefmt '%b %d %H:%M:%S GMT %Y'
  set format x '%b %d %Y %H:%M'
  set datafile separator ","
  set xlabel "timestamp"
  set ylabel "percent"
  set xtics rotate
  plot '< tail -n +2 ${input} | cut -f 1 --complement -d" "' using 1:3 title 'Maximum', '' using 1:4 title 'Average'
eor
}

graph_results

我收到以下错误:

bash-4.1$ ./run_gnuplot 
Could not find/open font when opening font "arial", using internal non-scalable font
Warning: empty x range [4.76463e+08:4.76463e+08], adjusting to [4.71699e+08:4.81228e+08]

生成的图表上有正确的数据点和时间戳,但图表中的 x 轴日期值不正确。

当 csv 文件中包含多个值时它可以正常工作,但当只有一个值时则不行。我意识到错误消息是因为使用一个值,x 轴的开始和结束范围是相同的(如此处所述http://gnuplot.10905.n7.nabble.com/empty-x-range-td1650.html)。

我知道我可以设置以下范围:

set xrange

但由于我使用的是自定义日期格式,而且我不知道开始日期和结束日期应该是什么,因为生成的数据是动态的,因此不确定我该如何使用?

【问题讨论】:

    标签: bash csv gnuplot


    【解决方案1】:

    使用自定义timefmt 时,xrange 也必须使用以下格式指定:

    set xrange ["Feb 04 00:00:00 GMT 2015":"Feb 09 00:00:00 GMT 2015"]
    

    或者您可以使用整数,即设置 xrange 的时间戳。对于 5.0 版,下面的行等同于上面的字符串设置:

    set xrange [1423008000:1423440000]
    

    请注意,直到 4.6 版 gnuplot 使用 2000 年 1 月 1 日作为其时间戳的时间参考,并且仅从 5.0 开始使用标准 Unix 时间戳。所以在 4.6 之前,第一行中字符串设置的等效命令是

    set xrange [476323200:476755200]
    

    【讨论】:

    • 谢谢@Christoph。有没有办法告诉 gnuplot 只绘制一个值,因此 x 轴将只显示一个值,而不是 gnuplot 尝试为 x 轴创建开始和结束值? (因为我的数据是动态的,所以将 xrange 设置为正确的格式对我来说很棘手)
    • 您可以使用set xtic (...) 手动设置此单个刻度,但是您必须从某个地方获取值...set xtic ('Feb 05 2015 14:54' 'Feb 05 14:54:00 GMT 2015')(我目前无法尝试)
    猜你喜欢
    • 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
    相关资源
    最近更新 更多