Python + Gnuplot = 4 Days saved 
I've tried doing graph plotting in Linux before and found it to be a bit of a nightmare.

All I ever wanted to do was get data from a comma separated value (.csv) file and plot it.

I used Octave + Gnuplot, as Octave was designed for mathmaticians I thought it was the best tool for the job. Turns out being able to add numbers together does not give you the ability to use Octave.

Using Octave together with Gnuplot I was able to plot a graph in about 4 days. (Do not take this as a criticism of Octave, but of me.)

Thankfully inbetween times I began to learn Python and now the time has come again for me to try my hand at plotting graphs myself.

Looking in Ubuntu's repositories I found there was a Gnuplot package for python ready to install. After installing and trying the demo that came with it I managed to plot some graphs. Even better there is a csv module built into python itself.

All in all it took about an hour to get some graphs out. Whoop! Thanks Python you saved me 4 days of pulling my hair out.

Here's the code


#! /usr/bin/env python

import csv
import Gnuplot
import sys

data = sys.argv[1]
title = sys.argv[2]

results = []

f = open(data, "r")
reader = csv.reader(f)

g = Gnuplot.Gnuplot(debug=1)
g.title(title)
g('set data style lines')

for i in reader:
a = []
for j in i:
a.append(float(j))
results.append(a)

g.plot(results)
g.hardcopy(title+".ps", enhanced=1, color=1)



I plan on extending the file to do some data analysis for me (integration, linear regression etc), but that will require me learning to count a few more numbers.

Enjoy.



[ 5 comments ] ( 66 views )   |  permalink
Solo Mapping 
I've written about Open Street Map before. Unfortunately since that Mapping Party I have been unable to contribute due to a lack of owning a GPS device.

This has been particularly frustrating as most of my area (Harborne, B17) is almost completely barren on OSM.

OSM were exhibiting at LRL this year, and after speaking to Andy Robinson I was allowed to borrow a Garmin Geko GPS device from the Open Street Map Foundation. I knew they had a stock of GPS devices, but I didn't know they were willing to lend them out! As long as you have someone they trust to vouch for your trustworthiness they are happy to let you have one.

Using the shiny Geko I was able to map a (in my opinion) large chunk of the surrounding area.

Before


After


It took me about 4 hours worth of cycling to get all the traces, and about another 2 hours of editing in JOSM to get it all tagged. I'm very pleased with the results. Even I was impressed by how much can be done by one person.

I would like to organise a mapping party for south Birmingham, along the lines of the one that I firsts attended. If anyone wants to give me a hand just leave a comment or email me.

Update:

I had to return the Geko of course, but I have purchased my own Geko 201 from Ebay for the princely sum of £51 plus £10 P&P. Have not done any serious mapping with it yet, just need to find a spare hour or two.

[ add comment ] ( 1 view )   |  permalink
Wordcount 1.2 
Yet more tweaking of my wee python script.

It can now take the variable from the command line.

$ python wordcount.py <file>




#! /usr/bin/python
import sys


def wordcount(a):
""" Wordcount will count how many instances there are of a word in a string, a. """
memory = [] # create memory
a = a.lower().split()

for i in a:
if i not in memory:
print "There are %(number)i instances of \" %(word)s \"\
in the string" % {"number": a.count(i), "word": i}
memory.append(i)

file = sys.argv[1]
a = open(file, "r").read()

wordcount(a)


[ add comment ] ( 1 view )   |  permalink
Wordcount 1.1 

#! /usr/bin/python


def wordcount(a):
memory = [] # create memory
a = a.lower()
a = a.split()


for i in a:
if i not in memory:
print "There are %(number)i instances of \" %(word)s \"\
in the string" % {"number": a.count(i), "word": i}
memory.append(i)




[ add comment ] ( 1 view )   |  permalink
Improved Python 
Currently sitting in a Python sprint feeling like a complete fraud. So I decided to go back and improve my old python script. Only to find that the "memorylength==0" was throwing up an error because I had not defined memorylength!

So with a bit of tweaking I have got the script to actually work. I suppose this could be called the 1.0 release. :)

I also though there may be a simpler way of doing the memory check bit, the core of the script. And manged to take out a whole 4 lines of code. Whoop.


#! /usr/bin/python

def wordcount(a):
memory = [] # create memory
a = a.lower()
a = a.split()


for i in a:
if i not in memory:
print i
print a.count(i)
memory.append(i)


[ add comment ] ( 1 view )   |  permalink

<<First <Back | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Next> Last>>