bash 使用 gnuplot 从 CSV 生成图形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28281139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate a graph from CSV using gnuplot
提问by Rory
I'm getting an error:
我收到一个错误:
Skipping data file with no valid points
跳过没有有效点的数据文件
when trying to generate a graph from a csv file using gnuplot, any help is much appreciated!
尝试使用 gnuplot 从 csv 文件生成图形时,非常感谢任何帮助!
sample.csv
样本.csv
timestamp,unit,maximum,average
Thu Jan 29 10:57:00 GMT 2015,Percent,22.96,7.723999999999999
Thu Jan 29 10:52:00 GMT 2015,Percent,62.79,26.227999999999998
Thu Jan 29 10:47:00 GMT 2015,Percent,46.54,15.075999999999999
run_gnuplot.sh
run_gnuplot.sh
#!/bin/bash
gnuplot << eor
set terminal png
set output 'output.png'
set style data linespoints
set datafile separator ","
set xlabel "timestamp"
set ylabel "percent"
plot "sample.csv" using 1:3 title "Maximum" using 1:4 title "Average"
eor
Error:
错误:
bash-4.1$ ./run_gnuplot.sh
Could not find/open font when opening font "arial", using internal non-scalable font
gnuplot> plot "sample.csv" using 1:3 title "Maximum" using 1:4 title "Average"
^
line 0: warning: Skipping data file with no valid points
gnuplot> plot "sample.csv" using 1:3 title "Maximum" using 1:4 title "Average"
^
line 0: x range is invalid
采纳答案by Christoph
You must tell gnuplot, that the first column is to be treated as time data with
您必须告诉 gnuplot,第一列将被视为时间数据
set xdata time
You also must give the time format for parsing the first column. Unfortunately, gnuplot doesn't support reading in the day of the week.
您还必须提供解析第一列的时间格式。不幸的是,gnuplot 不支持在一周中的某一天阅读。
Either change the format of the data written to the file, or filter the day of the week with an external tool like cut
:
更改写入文件的数据格式,或使用外部工具过滤星期几,例如cut
:
set xdata time
set timefmt '%b %d %H:%M:%S GMT %Y'
set datafile separator ','
plot '< tail -n +2 sample.csv | cut -f 1 --complement -d" "' using 1:3, '' using 1:4
The tail
part to skip the first line isn't necessary with version 5.0.
tail
版本 5.0 不需要跳过第一行的部分。