问题
I'm trying to plot a bundle of graphs that are currently stored like this:
MyFile.txt
"ID01" 1 2 3 4 5
"ID02" 3 4 5 6 7 8 9
"ID03" 4 3 1 2 3 4
such that there is one line titled "ID01" that shows the first row only, another line titled "ID02" and so on. Bonus points if lines are displayed in different colors. Any help is much appreciated!
I got as far as this:
plot for[blockIndex=0:3] "MyFile.txt" matrix index blockIndex every ::1 w lp title "".blockIndex
which produces the below image if blank lines are inserted between rows in the data file (which I could do).
The remaining problems are
a) I absolutely do not get the ID-String to display as the title (columnheader apparently does not play well with matrix mode). I currently use the block index, which would work as a stopgap, but is not an option for the finished script. I do have the option of arranging the ID-Strings in the data file differently (i.e. separate comment line as per columnheader, ...) - the only requirement is really that all line data should be in a single file.
b) I get TONS of "matrix contains missing or undefined value" warnings, which significantly slows down displaying the image on screen.
回答1:
This is not the way gnuplot uses to save data. Your approach has also other problems, i.e. you don't actually have a matrix if the rows have different number of points etc. The gnuplot way of handling such data would be to separate points belonging to different lines with two blank lines, and add the respective header before:
"ID01"
1
2
3
4
5
"ID02"
3
4
5
6
7
8
9
"ID03"
4
3
1
2
3
4
And then plot the data with
set key autotitle columnheader left
plot for [i=0:2] 'MyFile.txt' using 0:1 index i with linespoints
Alternatively, if you prefer a more compact representation, you can save each line in a column. But then you must make sure, that shorter columns are filled up with empty values. For that one should use e.g. comma as field separators:
"ID01","ID02","ID03"
1,3,4
2,4,3
3,5,1
4,6,2
5,7,3
,8,4
,9,
and plot with
set key autotitle columnheader
set datafile separator ','
plot for [i=1:3] 'MyFile.csv' using 0:i with linespoints
来源:https://stackoverflow.com/questions/33563953/gnuplot-plot-row-wise-and-named-data-as-bundle-of-differently-colored-and-title