#1150 datafile column heads and legend


Suppose the following file with column headers, called e.g. d.dat:

x y z
-2 4 0
-1 2 2
0 0 4
1 3 2
2 5 1

The legend of graphs
plot 'd.dat' using "x":"y"
plot 'd.dat' using "x":"y", '' using "x":"z"
shows legend with an x in all cases, instead of y and z.


  • Ethan Merritt

    Ethan Merritt - 2012-07-10

    The general rule "set key autotitle columnhead" uses is to take the column header from the first explicitly named column in the plot command. In your example command this is "X". The commands below, however, will show "X", "Y", and "Z" for the three titles;
    plot 'd.dat' using "X", '' using "Y", '' using "Z"
    plot for [COL in "X Y Z"] 'd.dat using COL
    plot for [i=1:3] 'd.dat' using i title column

    Feel free to suggest a more complicated rule if you can think of one that makes sense.

  • Petr Mikulik

    Petr Mikulik - 2012-07-10

    Instead of writing text of 1st column, I think the label of the plotted column should be written.
    Thus, if "x" is the 1st column header, then I propose that

    plot for [COL in "y z"] 'd.dat using COL
    plot for [COL in "y z"] 'd.dat using 1:COL
    plot for [COL in "y z"] 'd.dat using "x":COL

    will consistently write "y" or "z" as the key in all 3 cases.

  • Ethan Merritt

    Ethan Merritt - 2012-07-10

    The first two cases will already do that.
    The third case expands to
    plot ... using "X":"Y"
    just as in your original problem example. What rule should we pick that allows the program to choose "Y" rather than "X" as the title? Even more problematic is the case
    plot ... using "X":("Y"+"Z")
    The same problem occurs with the older syntax
    plot ... using 1:($2+$3) title columnhead
    The program currently uses the header of column 2 as a title, but this is not a great solution.

  • Petr Mikulik

    Petr Mikulik - 2012-07-11

    If using x:y, where y is a string, then use y as the label instead of x.

    plot ... using "X":("Y"+"Z")
    it should show the full command as in case of using 1:($2+$3).

    The following looks like yet another bug;
    plot for [ c in "y z" ] 'b.dat' using 1:(column(c)*10) title columnhead(1)
    plot for [ c in "y z" ] 'b.dat' using 1:(column(c)*10) title columnhead(1).columnhead(2)
    plot for [ c in "y z" ] 'b.dat' using 1:(column(c)*10) title columnhead(1).columnhead(2).columnhead(3)

    I would expect keys x, xy and xyz, but something different is shown.

  • Moorkh

    Moorkh - 2012-07-13

    the requirements of the OP are logical. we always put the independent variable on the horizontal axis. The graph consequently is of the non independent variable. ( think of sin(x) )

    A simple rule would be to name the graph as whtever is plotted on the y-axis

  • Ethan Merritt

    Ethan Merritt - 2012-07-14

    The attached patch is against the development branch (4.7), but it should apply to 4.6 also. It does two things:

    datafile.c: A very small patch that implements exactly what you request for the specific examples given. If the plot command uses string constants or string variables to specify the columns in the using spec, then the 2nd using spec is used for the plot title.

    parse.c: A non-obvious change that allows the columnheader to be found if the column is specified by a non-string variable. The test case that this addresses is
    plot for [N=1:3] 'd.dat' using 0:(column(N))

    Neither patch attempts to address the general case of
    plot ... using (column(<some expression>))
    nor the case of
    plot ... using ( <some expression using column() or $> )
    The program design makes this very difficult, since the column header layout is determined at the time the command is parsed, but the expressions will not be evaluated till later on when the command is executed.

  • Ethan Merritt

    Ethan Merritt - 2012-07-18
    • status: open --> closed-fixed
  • Petr Mikulik

    Petr Mikulik - 2012-07-25
    • status: closed-fixed --> open-fixed
  • Petr Mikulik

    Petr Mikulik - 2012-07-25

    It works fine, but with one exception when using a string as the first column:

    plot 'a.dat' u 1:(column('y')), '' u 'x':(column('y')), '' u (column('x')):(column('y'))

    The legend now shows "y", "x" and "x". It should show "y" three times.
    I think the x-column specification should be ignored, use only the y-column spec.

    Can you please fix this? Thanks.

  • Ethan Merritt

    Ethan Merritt - 2012-07-25

    > plot 'a.dat' u 'x':(column('y'))

    I do not know how to make this work the way you expect. The title substitution would have to be done inside the evaluation of f_column(), but at that point there is no record of which slot in the "using" spec the function was called from. Fortunately, I think that there is always an alternative available. Since you must already know the column heading, you can just provide it as a title directly:

    plot 'a.dat' using 'x':(column('y')) title 'y'
    foo = 'y'
    plot 'a.dat' using 'x':(column(foo)) title foo

  • Petr Mikulik

    Petr Mikulik - 2012-07-26

    I know about the "title" option, but it's just boring to write it explicitly if gnuplot can do it automatically.

    Is the autotitle set after or during evaluation of the "using" spec? If it is after, then take the string from the 2nd field if there are at least 2 fields, and from the 1st field if it is the only one.

    If the autotitle is evaluated during evaluation of the "using", then I don't understand why the results are different for
    using 1:(column('y')) and for 'x':(column('y')) as the 2nd field is always the same.

  • Ethan Merritt

    Ethan Merritt - 2012-07-26

    The variable df_key_title is set during the parsing stage, by routing plot_option_using(). But only the very simple cases, like a bare string constant, can be recognized at that stage. Evaluation of expressions, including evaluation of the column(foo) function, does not happen till later.

    > results are different for using 1:(column('y')) and for 'x':(column('y'))

    If df_key_title has not been set previously during the parsing stage, then evaluation of the column() routine will set it the first time it searches for a column header. But it does not know which field of the 'using' spec it was called from so I do not see a way for it to distinguish between the two cases in your example.

  • Petr Mikulik

    Petr Mikulik - 2012-07-26

    Aha, now I understand - also
    plot 'a.dat' u 1:(column('x')+column('y'))
    shows 'x'.

    Thus I propose: only if the syntax contains just a string instead of integer or expression here:
    plot 'a.dat' using whatever:'y'
    plot 'a.dat' using 'y'
    then show 'y', otherwise show the full command as "splot" does.

    Is this feasible?

    If not, then I would prefer the full command as plot and splot are always showing.

    That's because if
    plot 'refl.dat' using 'theta':'measurement', '' using 'theta':'fit'
    then I prefer to see rather the full command than twice 'theta'.

  • Ethan Merritt

    Ethan Merritt - 2012-11-16
    • status: open-fixed --> closed-fixed

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks