Short question:
How do I display the _
(underscore) character in a title in gnuplot that is assigned from a variable name in gnuplot?
Details: I have something like the following code:
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
}
This works fine with gnuplot except that the title get changed from foo_abc to fooabc. I don't know if I want to use an escape character because I don't want that to be in the file name. I've tried a couple of different options with single vs. double quotes but I haven't found what I need yet.
Instead of foo_abc
, write foo\\\_abc
.
Most gnuplot commands which generate labels accept a noenhanced
keyword which will prevent gnuplot from using enhanced text for just that string. In this case, it should be sufficient to just do:
set title item noenhanced
An alternative is to create a function which will remove the unwanted text from the string when passing it to 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...
I use an inline function to find the index of the first occurrence of x
in the string s
. I then remove that occurrence via string concatenation and slicing and recursively call the function again to remove the next occurence. If the index isn't found (strstrt
returns 0) then we just return the string that was put in. Now you can do:
set output remove('\',item)
set title item
The underscore comes from treating titles as "enhanced text". Turn that off using
set key noenhanced
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With