Posts Tagged ‘python’

How to customise the NavigationToolbar2 toolbar in matplotlib

Saturday, June 6th, 2009

My CurrentCost desktop software is written in Python, and uses the matplotlib library for plotting graphs.

I am a big fan of matplotlib – you can create some very cool graphs with it. It’s not without it’s issues… for example, I’ve still yet to work out how to do realtime graphing with it that isn’t massively inefficient and resource intensive. But that’s for another post 🙂

One of the nice things that you get with it is a toolbar that lets the user switch between different modes that the mouse supports – zooming, panning, and so on.

Adding the toolbar is straightforward – I create an object of the class NavigationToolbar2Wx and add it to a sizer which in turn I add to the graph Plot.

self.toolbar = Toolbar(self.canvas)
self.toolbar.Realize()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.EXPAND)
sizer.Add(self.toolbar, 0 , wx.LEFT | wx.EXPAND)
self.SetSizer(sizer)

The NavigationToolbar2 toolbar provides a number of features but it doesn’t exactly meet my needs so last night I had a go at customising it.

I wanted to:

  • remove the configure subplots button – as I don’t use subplots in my graphs
  • add a couple of new buttons – for different navigation abilities

I couldn’t find this documented clearly anywhere, so thought it was worth sharing the code that I came up with.

(more…)

Getting data from the CurrentCost CC128

Saturday, February 7th, 2009

I really need to stop blogging about CurrentCost stuff, this is starting to look like an obsession 🙂

But before I do that, I thought I’d share my experiences in writing a new CurrentCost parser.

What is this all about?
On the off-chance that anyone following me hasn’t already heard about CurrentCost a million times already, CurrentCost meters are domestic electricity monitors that tell you how much electricity your household is using. And they have a connection underneath that lets you grab the data from them.

As well as getting your current electricity usage, you can also get historical usage totals out of the serial port, which I’ve used in a variety of ways, such as in my Python CurrentCost software.

CurrentCost have now brought out a new model of their monitor – the CC128. The data format is different to the current models, so I wanted to try and write a new parser that could handle both the existing model and the new CC128 data.

(more…)

My HackDay hack – see where your friends are

Friday, October 24th, 2008

Today was IBM HackDay 6 – an internal HackDay run across IBM‘s many labs – and I managed to spend a bit of today hacking something together.

PhotobucketIn a nutshell, it’s kinda like Plazes, brightkite, dodgeball and others… find out where your friends currently are. And see where they are from your mobile.

The twist is that it gives you quite precise locations for friends within a known indoor campus – such as Hursley Park.

Hursley, like many IBM locations, is a campus, with thousands of employees in a 100 acre site.

What if you’re trying to find someone? Say you’re in a meeting, and a colleague hasn’t turned up yet. Where are they? Are they on their way?

Or you’ve arranged to meet a colleague for lunch or a coffee, and you seem to keep missing each other.

The idea of this hack was to build on the Hursley Maps tool to come up with some way for you to be able to quickly check where your friends are while you’re at work.

Okay… so it’s a fairly flimsy scenario. 🙂

But I’ve enjoyed playing with location-based services ideas before, and thought this would be an interesting twist. Plus, it was an excuse to play with Django which I’ve wanted to try since hearing about it at BathCamp.

(more…)

Comparing your CurrentCost data with others

Wednesday, October 1st, 2008

a Python CurrentCost appBetween holidays, meetings, work and other stuff, I put CurrentCost stuff to one side for a few weeks.

I’m back now, and thought I’d share my plans for the app. If nothing else, I figure it’s useful for me to refresh my memory and plan my next step!

(more…)

Programmatically accessing authenticated Google App Engine services

Friday, September 19th, 2008

This one has been bugging me for a few days, and with help from a very helpful Google engineer I’ve finally got this working, so I thought I’d share my code where the next poor soul to try and do it might find it!

The problem:
I’ve written a small web service which I am hosting on Google App Engine.

By adding “login: required” to specific services in the app’s app.yaml file, I can make sure that you need to login to access a service.

If you’re accessing the service from a web browser, this is fine – you get redirected to a Google login page, and after entering your username and password and hitting ‘submit’, you get sent on to the web service you wanted.

But how do you do that programmatically? I wanted to do it from a Python application on my desktop, without being able to navigate the HTML login form.

(more…)

CurrentCost app … take 2!

Saturday, August 30th, 2008

a Python CurrentCost appI’ve been talking about it for a while, but I finally got around to spending some time working on a CurrentCost app.

The original code was written in C# using .NET 3.5, and I used WPF (Windows Presentation Foundation) to draw the graphs. For a number of reasons this proved unpopular and I got a ton of emails saying how this was no good for them. So I decided to start again.

The new app is written in Python – using wxPython and matplotlib to create the graphs.

a Python CurrentCost appUsing py2exe, I’ve been able to compile the whole thing (combining my script with a Python interpreter and a copy of all of the third-party libraries I’ve used) into a Windows executable that will (hopefully!) run on any Windows computer, without needing installing or requiring any pre-requisites.

I can also make the Python script itself available, making it something that Linux users could run as well. This means I get the low-overhead Windows experience I wanted, together with the ability to make it cross-platform. Neat!

I’ve been able to produce some more interactive graphs – the graphs in the new app can be zoomed in and out, panned, moved around, printed, and exported to images. As the amount of data in the app builds up, I can see this becoming very useful.

(more…)

Our Mashed 08 hack: CurrentCost Live

Monday, June 23rd, 2008

Yesterday was the end of Mashed 08 – the annual London hackday from BBC Backstage.

I saw last week that there was going to be a “social responsibility” category in the hack challenge, and decided that a CurrentCost hack was in order!

Together with Rich, we spent a day trying to hack together a competitive challenge based around CurrentCost, encouraging people to reduce their home electricity usage by making it into a game they can play with their friends.

Here are a few notes based on the presentation I gave at the end.

(more…)

Accessing MySQL from Perl on SlugOS

Tuesday, May 27th, 2008

I’ve written about my CurrentCost meter that I’m using to monitor my home electricity usage, and the small home server that I’ve set up to collect the data.

Yesterday, I decided to make a start on collecting the data. My plan was to copy what Nick had done and create a MySQL database to store the info, with a table to store a timestamp and the watt reading from the CurrentCost meter.

CREATE TABLE currentcostdl (  
    time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,  
    power FLOAT NOT NULL,  
    primary key(time));

I started with Nick’s Python scripts, but didn’t get very far.

The server, a NSLU-2 (or “slug”) is running SlugOS.

The problem was that there is no python-mysqldb package in the SlugOS repositories. I did try downloading the source for it from sourceforge to build it myself, but struggled to get the dependencies I needed to make it – urllib in particular was a big pain.

I also tried the debian package but again dependencies on SlugOS got in the way.

So I gave up on that and decided to do it myself using Perl – Perl and MySQL had to be easier, right?

Erm… not so much 🙂

(more…)