2
votes

I'm brand new to Gnuplot and want to be able to graph a huge amount of data that looks like this:

Description violFine state
"Red Light Violation" $75.00 MD
"No Stop/Park Handicap" $502.00 MD
"Red Light Violation" $75.00 MD
"No Stop/Park Handicap" $502.00 MD
"Red Light Violation" $75.00 MD
"Red Light Violation" $75.00 MD
"Red Light Violation" $75.00 VA
"All Other Stopping or Parking Violations" $32.00 MD
"Red Light Violation" $75.00 MD
"Red Light Violation" $75.00 MD

As you can see, the top line is the names of the columns and I have many duplicate string values in the "Description" column. What I want to do is add up all the "violFine" numbers per unique "Description" and plot it with the "Description" on the x-axis and the total of the "violFines" on the y-axis. I've made a graph to illustrate what I'm talking about accessible at this link: http://i.imgur.com/NtZsZCR.jpg
(Sorry, I would've made it available on this page if I had enough reputation points).

Any help with going about this would be awesome! Thanks!

1

1 Answers

1
votes

This sort of data processing task isn't well suited for gnuplot. Luckily, gnuplot is happy to let you use other tools to process the data and then pipe the result in. Here, I would use python:

from collections import defaultdict
import csv
import sys

d = defaultdict(list)
with open(sys.argv[1]) as fin:
    next(fin)  #remove the first line which doesn't contain data
    reader = csv.reader(fin,delimiter=' ',quotechar='"')
    for row in reader:
        d[row[0]].append(float(row[1][1:]))

for k,v in d.items():
    print '"{0}"'.format(k),sum(v)

Now in gnuplot, you can plot this as:

plot '< python script.py datafilename' using (column(0)):2:xtic(1) with lines