Reputation: 21
I am new to gnuplot. I have csv file like this:
category| date | value
a |2016-04-01 | 0,2
a |2016-04-02 | 0,31
a |2016-04-03 | 0,14
....
a |2016-04-11 | 0,4
b |2016-04-01 | 0,32
b |2016-04-02 | 0,31
....
b |2016-04-10 | 0,15
C |2016-04-01 | 0,15
C |2016-04-02 | 0,23
.....
And so on
How do I plot data for each category on different graph in one command?
For example, if I have 9 distinct categories, then I have 9 graphs where x axis is for date, and y axis is for value.
Upvotes: 2
Views: 811
Reputation: 2187
You can plot the file several times using plot
s for-loop. Each time, you only regard x values where column one fits the loop variable (check help ternary operator
):
set decimalsign ","
set xdata time
set format x "%Y-%M-%D"
plot for [cat in "a b c d"] datafile using ($1 eq cat ? $2 : NaN):3
Gnuplot cannot connect the points (e.g. with linespoints
) if there are invalid points in between. So you'll have to sort the file beforhand.
Upvotes: 1
Reputation: 242443
I used Perl to reformat the input for gnuplot.
#! /bin/bash
input=$1
tmp=$(mktemp)
perl -aF'/\|/' \
-ne 'print "\n\n" if $last ne $F[0] && $. > 2;
$last = $F[0];
$F[-1] =~ s/,/./;
print "@F" if $. > 1;
' "$input" > "$tmp"
categories=($( cut -f1 -d\| "$input" | uniq ))
{
cat <<EOF
set term png;
set xdata time;
set timefmt '%Y-%m-%d';
plot '$tmp' index 0 using 2:3 with lines title "${categories[1]}" \\
EOF
i=1
while (( i < ${#categories[@]}-1 )) ; do
printf ', "" index %d using 2:3 with lines title "%s"' $i "${categories[++i]}"
done
} | gnuplot > "$input".png
rm "$tmp"
Upvotes: 0