I have a data file with 100 columns and thousands of rows, but I want to be able to select one single row, and in that row, use the even columns as the inputs for the X-axis and the odd columns as inputs for the Y-axis for a plot. Is there any way I can do that in gnuplot?
问题:
回答1:
So here the script I've done to plot a given line from a given data file with odd column as x-axis and even column as y-axis.
#!/usr/bin/gnuplot
set term pdf
set output "plot.pdf"
line_number=1
data_file="data.dat"
set xrange[0:10]
set yrange[0:10]
table_file="/tmp/gnuplot_tab.dat"
set table table_file
plot for[i=1:*:2] "<(sed -n '".line_number."p' ".data_file.")" u i:i+1
unset table
unset key
plot table_file
Let's explain this script:
First we specify the line number with line_number
and the data filename with data_file
. The effect of set table table_file
is, as specified by gnuplot documentation, the print of points' coordinates in the file ̀table_fileinstead of ploting them with
plot`command.
The plot for[i=1:*:2]
plots for each i
beginning from 1
, ending when no more column can be ploted and incrementing by 2
at each iteration. The idea is to take the columns i
(i.e. odd) and i+1
(i.e. even) as coordinates (or use the inverse i+1:i
to take even for x-axis and odd for y-axis).
The part "<(sed -n '".line_number."p' ".data_file.")"
is inspired from Gnuplot plotting data from a file up to some row and selects the line you specified as a file. Since set table
has been done, this plot
command saves each coordinate to a new file. This is a trick to transform the line in a two column file.
Finally the script disable the ̀set tableto then plot the saved file
table_file`
I tested it with the following data file changing the line number from 1
to 2
:
10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0
0 10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 10 0