I have a csv file with 12 numbers which correspond to the 12 months. An example of the file is as follows:
$ cat data.csv
"3","5","6","5","4","6","7","6","4","4","3","3",
I'd like to plot these with the months in the x-axis using "January, February, March and so on."
I've found this script but I don't know how to input the months:
for FILE in data.csv; do
gnuplot -p << EOF
set datafile separator ","
set xlabel "xlabel"
set ylabel "ylabel"
set title "graphTitle"
plot "$FILE" using $xcolumn:$ycolumn
EOF
done
The expected output should be a plot where the x-axis is the month and the y-axis is the data from the csv file. Note that in the CSV file there aren't the months, just the numbers. That's why I am asking what is the best way to achieve this without having to enter them manually in the CSV or looping through an array. Is there any gnuplot function that adds the date and can be formatted?
Thank you
UPDATE: After reviewing OPs post and code some more, I'm guessing the desired format looks like:
January:"3",February:"5",March:"6",April:"5",May:"4",June:"6",July:"7",August:"6",September:"4",October:"4",November:"3",December:"3",
If this is the case, we can use the same solution (below) and pipe the final results through tr
to transpose the data back to a single-line/multi-column dataset, eg:
$ paste -d" " <(locale mon | tr ';' '\n') <(tr ',' '\n' < data.csv) | grep -v '^ $' | tr ' \n' ':,'
January:"3",February:"5",March:"6",April:"5",May:"4",June:"6",July:"7",August:"6",September:"4",October:"4",November:"3",December:"3",
And updating OPs code:
datfile=$(mktemp)
for FILE in data.csv
do
paste -d" " <(locale mon | tr ';' '\n') <(tr ',' '\n' < data.csv) | grep -v '^ $' | tr ' \n' ':,' > "${datfile}"
gnuplot -p <<-EOF
set datafile separator ","
set xlabel "xlabel"
set ylabel "ylabel"
set title "graphTitle"
plot "${datfile}" using $xcolumn:$ycolumn
EOF
done
'rm' -rf "${datfile}" > /dev/null 2>&1
Looks like gnuplot
can accept data in various formats, including the following:
January "3"
February "5"
March "6"
April "5"
May "4"
June "6"
July "7"
August "6"
September "4"
October "4"
November "3"
December "3"
NOTE: If OP determines this is not an acceptable file format then I'm sure we can come up with something else ... would just need the question updated with a sample of a valid file format showing months and numerics.
So if we can generate this data set on the fly we could then feed it to gnuplot
...
First we'll let locale
generate the months for us:
$ locale mon
January;February;March;April;May;June;July;August;September;October;November;December
Next we can transpose our single-line/multi-column datasets to multi-line/single-column datasets:
$ locale mon | tr ';' '\n'
January
February
March
April
May
June
July
August
September
October
November
December
$ tr ',' '\n' < data.csv
"3"
"5"
"6"
"5"
"4"
"6"
"7"
"6"
"4"
"4"
"3"
"3"
From here we can paste
these 2 datasets together, using a space as the column delimiter:
$ paste -d" " <(locale mon | tr ';' '\n') <(tr ',' '\n' < data.csv)
January "3"
February "5"
March "6"
April "5"
May "4"
June "6"
July "7"
August "6"
September "4"
October "4"
November "3"
December "3"
One last step would be to write this to a (tmp) file, eg:
$ datfile=$(mktemp)
$ paste -d" " <(locale mon | tr ';' '\n') <(tr ',' '\n' < data.csv) | grep -v '^ $' > "${datfile}"
$ cat "${datfile}"
January "3"
February "5"
March "6"
April "5"
May "4"
June "6"
July "7"
August "6"
September "4"
October "4"
November "3"
December "3"
NOTE: The grep -v '^ $'
is to get rid of the extra line at the end related to the last comma (,
) in data.csv
From here "${datfile}"
can be fed to gnuplot
as needed and once no longer needed deleted, eg:
$ gnuplot ... "${datfile}" ...
$ 'rm' -rf "${datfile}" > /dev/null 2>&1