Portecochon
Portecochon

Reputation: 45

Gnuplot titling - Using variables in title using sprintf

I have been doing some heavy plotting on gnuplot but i'm fairly new to it, i hope you guys can advise me. My problem is the following:
I want to plot several curves on the same graph and i want my .gp script to give titles to those curves cleverly. Each curve is the acoustic spectrum of a tuning fork that was excited at a particular frequency. My script introduces the vector u(n) which contains the values of said frequencies, and titre(n) is the title of curve number n. Indeed it doesn't work and my guess is that it is because of the bad formating in titre(n).

u(n)=( 865, 860, 855, 850, 441, 439, 437, 435, 433, 431, 429, 427, 425, 423, 421 )
titre(n) = sprintf("Excitation à %d Hz", u(n))
plot for [n=2:15] 'batt.res' u 1:n w l lw 1 lc palette frac n/15.0 title titre(n)

Instead of what i want, all of the titles are "Excitation a 421 Hz". The frequency is stuck at 421 Hz for each curve. How can i fix that?

All help appreciated :)

Upvotes: 3

Views: 3370

Answers (2)

user50619
user50619

Reputation: 353

To just set the title (which this thread name implies), sprintf can be used as well. Here is an example that graphs reactance for a given capacitor value against frequency, giving the capactitor value in the title.

set xrange[1:1e9] set xlabel 'frequency' set ylabel 'reactance' set grid set logscale x set logscale y C=10e-9 set title sprintf(" reactance for C=%g ", C); f(x)=1/(2*3.142*x*C) plot f(x) !sleep 10

Upvotes: 0

Christoph
Christoph

Reputation: 48390

Gnuplot doesn't have an array or list data structure, yet. So for such kind of iterations you must use a string which contains the labels separated by spaces:

u = "865 860 855 850 441 439 437 435 433 431 429 427 425 423 421"
titre(n) = sprintf("Excitation à %s Hz", word(u, n))
plot for [n=1:15] 'batt.res' u 1:n+1 w l lw 1 lc palette frac n/15.0 title titre(n)

Upvotes: 3

Related Questions