6

I have a datafile that looks like this

#index name1 name2 name3
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

I want to plot 3 lines:

plot "data" using 1:2 with lines,\
...

This works ok, except for the line labels. How can I specify the column names in the datafile?

2 Answers 2

14

If you have gnuplot 4.2 or newer, you can do this fairly easily. You will need to get rid of the comment marker in the first line though.

With a file like this:

index name1 name2 name3
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

the following will do what you want:

set key autotitle columnheader
plot 'a.dat' u 1:2 w l, '' u 1:3 w l, '' u 1:4 w l

If I do set term dumb before plotting, I get a nice ascii plot. I love gnuplot!

7 ++----------+----------+-----------+-----------+----------+---------$$$
  +           +          +           +           +         name1$****** +
  |                                                       $name2 ###### |
  |                                                 $$$$$$ name3 $$$$$$ |
6 ++                                          $$$$$$                  ###
  |                                     $$$$$$                  ######  |
  |                               $$$$$$                  ######        |
  |                         $$$$$$                  ######              |
5 ++                   $$$$$                  ######                  ***
  |              $$$$$$                 ######                  ******  |
  |        $$$$$$                 ######                  ******        |
  |  $$$$$$                 ######                  ******              |
4 $$$                  #####                  ******                   ++
  |              ######                 ******                          |
  |        ######                 ******                                |
  |  ######                 ******                                      |
3 ###                  *****                                           ++
  |              ******                                                 |
  |        ******                                                       |
  +  ******   +          +           +           +          +           +
2 ***---------+----------+-----------+-----------+----------+----------++
  1          1.5         2          2.5          3         3.5          4
1
  • The comment marker was messing me up too, there are some old tutorials out there that recommend having a comment in the first line of the datafile, that might be the source of the confusion
    – jrh
    Commented May 31, 2019 at 19:07
0

columnheader(n)

More verbose / flexible than set key autotitle columnheader:

plot 'my.dat' using 1:2 title columnheader(2), \
    '' using 1:3 title columnheader(3)

Tested on Gnuplot 4.6, Ubuntu 15.10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.