8

gnuplot 学习小计3-字符串 读取文件

 3 years ago
source link: https://blog.csdn.net/yanxiangtianji/article/details/8826801
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本次要点:

字符串显示、连接、单双引号。

数据文件中的注释符、间隔符。

1,字符串操作:

打印字符串命令:

print

print 'abc'
print 123
print "xxx"

采用“.”号来连接字符串。

set title 'abc'.'xxx'
另一个比较有用的用法是先定义一个字符串变量,然后每次去修改这个变量,例如:
name='1_2'
set title 'data in '.name
plot 'datafile_'.name.'.txt'

name='1_3'
...
这样每次只用修改一下name变量,其他绘图函数都不用变。这样非常方便我们绘制大量性质相同的图片。

单引号的字符串是不识别转义符的,即一个原始字符串。

双引号字符串是识别转义符的,所以要使用“\n”的话,记得要用双引号字符串。

2,文件操作

像上次说的默认情况下的注释符是“#”,但是对于有些已经生成或下载的数据,再改起来很麻烦。可以手动指定文件中的注释符号。
set datafile commentschar "!"
这样就指定了使用感叹号作为注释符,若行首是“!”则略过该行。
还可以指定多种注释符,例如我即想用“#”,同时还想用“!”,可以这样写:
set datafile commentschar "#!"
这也就是说,gnuplot不支持组合的注释符,即注释符必须是一个字节。
显示注释符所用的关键字有所不同:
show datafile comments
gnuplot默认是使用的空白符(空格、制表符)进行分隔的,但是打开csv文件的时候,我们需要指定它改用逗号分隔。上次我学到的是使用格式说明方法:
plot 'a.csv' using 1:2 "%lf,%lf"  
其实这种方法在这里用,有点大材小用了。

gnuplot可以指定数据文件的分隔符:

#使用逗号
set datafile separator ","
#恢复到默认状态:
set datafile separator
#使用空白符(默认):
set datafile separator whitespace
#显示分隔符:
show datafile separator

缺值(missing value)

有的时候文件中有一些位置是没有记录到数据的,例如某些探测器失灵的时候,在数据文件中我们可以使用诸如“NaN”、“-”之类的记号表示空缺。那么我们需要告诉gnuplot我们使用的记号,让gnuplot自动过滤掉这些东西,而不影响其他数据的读取和理解。

set datafile missing "NaN"<span style="font-family:宋体, Arial;font-size:14px;color:#666666;"><span style="line-height: 19px; background-color: rgb(241, 241, 241);">
</span></span><pre name="code" class="plain">set datafile missing "-"
<pre name="code" class="plain">set datafile missing "0"  #某些特殊情况下我们使用了某个数字来表示缺值


官网文档中明确指出:默认情况下datafile missing是没有设置任何值的,作为补充默认情况下gnuplot将所有出现在应该是数字的地方的不可解析的字符串(诸如Inf, NaN是可以解析有意义的字符串)都作为缺值。

plot命令的有两种对待缺失值的方式:

plot "datafile" using 1:2 w lp      # continuous
plot "datafile" using 1:($2) w lp   # discontinuous
前者直接忽略缺失值,但依然有直线相连;后者虽然也忽略缺失值,但缺失值前后是不相连的。

默认值(default value)

在数据缺失(missing)之后,有时需要指定一个默认值来让gnuplot在画图时可以用来自动代替它们,而不是忽略它们。
如果您知道怎么办,请在回复里告诉我,我可以和大家分享这个知识,先谢谢了!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK