jake
jake

Reputation: 351

Set plot title inside loop

I am using a gnuplot plot loop to plot data from several plots together:

filenames = "my data files"
plot for file in filenames file.".txt" \
title file

Right now I'm using title file to set the plot title, but I'd like more control over the plot title without resorting to changing my file names. For example, in pseduocode, I'd like:

files = [first, second, third, fourth]
titles = [One title, second title, third title, fourth title]
plot for [n=1:4] files[n] titles[n]

Note that the titles consist of multiple words, so words(titles,n) is not an option. Is there another method I can use to give me more flexibility in my titles?

Upvotes: 1

Views: 1754

Answers (2)

Christoph
Christoph

Reputation: 48440

First of all: good news, the 5.0 version has limited support for quoting text parts for use with word and words.

With version 5.0RC3, the following works fine:

titles='"first title text" "second title text"'
plot x title word(titles, 1), 2*x title word(titles, 2)

A second 'hack' would work with the postscript terminal, in case you are using it, and encodes the space inside the title with its octal representation \040:

set terminal postscript eps enhanced
set output 'spaces.eps'
titles='first\040title\040text second\040title\040text'
plot x title word(titles, 1), 2*x title word(titles, 2)

A third version uses a replacement character for the spaces and a shell call to sed to insert the spaces after the splitting:

titles='first@title@text second@title@text'
sub(s) = system(sprintf("echo \"%s\" | sed 's/@/ /g'", s))
plot x title sub(word(titles, 1)), 2*x title sub(word(titles, 2))

You could also setup a function myword, which uses awk or similar to do the splitting directly. But that would probably require some fiddling with quote characters.

Upvotes: 4

sweber
sweber

Reputation: 2996

This is indeed possible using word(string_with_words, index) :

filenames = "my data files"
description= "one two three"

plot for [n=1:4] word(filenames, i) title word(description, i)

Upvotes: 0

Related Questions