Reputation: 15
I am using a csv file containing 3 columns with data as follows:
Country name, Year, Population value
Afghanistan, 2000, 8774440
Afghanistan, 2001, 8774441
Afghanistan, 2002, 8774442
Germany, 2000, 18774440
Germany, 2001, 18774442
Germany, 2002, 18774444
I am attempting to plot the population growth , i.e year as x axis and the values as y axis
I am new to gnuplot and was wondering if it is possible to plot the 2nd and 3rd columns using the first column's value as an argument/parameter
I know it is possible to plot particular rows using sed like
plot "<(sed -n '0,2p' p.csv)" using 2:3 with lines
but this only works when hard coded into my script
I have attempted using awk to get it working but to no avail
plot " <(awk '{$1=='Afghanistan'}' p.csv" using 3:4 with lines
any help would be appreciated
Upvotes: 1
Views: 196
Reputation: 26158
No need for sed or awk. You can use the ternary operator as filter (see help ternary
). There should be similar questions around (about filtering by numbers), however, which I can't find right now.
Code:
### plot data filtered with ternary operator
reset session
set datafile separator comma
$Data <<EOD
Afghanistan, 2000, 8774440
Afghanistan, 2001, 8774441
Afghanistan, 2002, 8774442
Germany, 2000, 18774440
Germany, 2001, 18774442
Germany, 2002, 18774444
EOD
mySelection = "Afghanistan"
myFilter(n) = strcol(n) eq mySelection ? $3 : NaN
set format y "%.0f"
set key top left
set xtics 1
plot $Data u 2:(myFilter(1)) w lp pt 7 title mySelection
### end of code
Result:
Upvotes: 3