简短的问题:如何_在gnuplot中从变量名中分配的gnuplot中显示(下划线)字符?
细节:我有类似下面的代码:
items = "foo_abc foo_bcd bar_def"
do for [item in items] {
set title item
set output item.eps
plot item."-input.txt" using 1:2 title item with linespoints
}
Run Code Online (Sandbox Code Playgroud)
除了标题从foo_abc更改为foo a bc 之外,这对gnuplot工作正常.我不知道是否要使用转义字符,因为我不希望它在文件名中.我尝试了几种不同的选择,单引号和双引号,但我还没找到我需要的东西.
mgi*_*son 17
生成标签的大多数gnuplot命令接受一个noenhanced关键字,这将阻止gnuplot仅为该字符串使用增强文本.在这种情况下,只需执行以下操作即可:
set title item noenhanced
Run Code Online (Sandbox Code Playgroud)
另一种方法是创建一个函数,该函数在将字符串传递给时将从字符串中删除不需要的文本set output:
remove(x,s)=(i0=strstrt(s,x),i0 ? remove(x,s[:i0-1].s[i0+strlen(x):]):s)
# Makes me wish gnuplot syntax was more pythonic :-p
#TODO: Write a `replace` function :-). These just might go into my ".gnuplot" file...
Run Code Online (Sandbox Code Playgroud)
我使用内联函数来查找x字符串中第一次出现的索引s.然后我通过字符串连接和切片删除该事件,并再次递归调用该函数以删除下一次出现.如果找不到索引(strstrt返回0),那么我们只返回放入的字符串.现在你可以这样做:
set output remove('\',item)
set title item
Run Code Online (Sandbox Code Playgroud)
and*_*ras 16
如果您正在使用增强型eps终端,那么您首先需要转义下划线.今天还有另一个相关的问题,它解释了这个问题.设置终端时,请尝试:
set terminal postscript noenhanced <whatever else here...>
Run Code Online (Sandbox Code Playgroud)
这对我有用(Arch linux,gnuplot 4.7.0).如果增强型终端是必不可少的,下面是我找到的部分解决方案.假设下划线总是出现在字符串中的相同位置.
set terminal postscript enhanced
items = 'foo\_abc foo\_bcd bar\_def'
do for [item in items] {
set output item[1:3].item[5:*].'.eps'
set title item
plot sin(x)
}
Run Code Online (Sandbox Code Playgroud)
这样您就可以转义下划线而不会出现文件名中的\.注意'items'字符串使用单引号; 有关详细信息,请参阅先前链接的问