是否可以使用 gnuplot 打印我在图表旁边绘制的数据?
如果我有一个文本文件input.txt
:
#x y
1 2
2 5
3 6
4 7
我做 plot 'input.txt'
我想像往常一样绘制它,并在图旁边打印表格。这可能吗?
注意:我使用的是 Windows,我想格式化输出。
最佳答案
有点晚了,但是OP要求Windows...所以,简而言之:
data = system('type yourfile.dat') # Windows
在Windows中,如果给出路径,需要注意\
、空格和双引号"
。
数据: SO22225051.dat
#x y
1 2
2 5
3 6
4 7
脚本:
适用于 Linux 和 Windows 的解决方案。 gnuplot>=5.2.0 的版本 1,gnuplot>=4.6.0 的版本 2。
### place data as table/text in graph
reset
FILE = 'SO22225051.dat'
set rmargin 15
set label 1 at screen 0.9,0.7 font "Courier New,12"
# Version 1: Windows & Linux using system() command;
# GPVAL_SYSNAME only available for gnuplot>=5.2.0
getData(f) = GPVAL_SYSNAME[1:7] eq "Windows" ? \
system(sprintf('type "%s"',f)) : \
system(sprintf('cat "%s"',f)) # Linux/MacOS
Data = getData(FILE)
set label 1 Data
plot FILE u 1:2 w lp pt 7 lc rgb "red"
pause -1
# Version 2: gnuplot-only, platform-independent, working at least with gnuplot>=4.6.0
Data = ''
set datafile commentschar ''
set datafile separator "\t"
stats FILE u (Data=Data.strcol(1)."\n") nooutput
set datafile commentschar # restore default
set datafile separator # restore default
set label 1 Data
plot FILE u 1:2 w lp pt 7 lc rgb "red"
### end of script
结果:
版本 1 和 2 之间的唯一区别是,在版本 2 中,gnuplot 将删除每个数据行的前导空格。
关于gnuplot - 如何在 gnuplot 中的图形旁边打印输入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22225051/