I wrote this to get familiar with GTK, Glade and matplotlib. This post is not a walkthrough or tutorial of using either of them. I wanted to write down some notes. The Flu Data Viewer is an example or a code I could copy from for other coding (So this code is put in Public Domain), so it would unlikely be updated in the future. The flu data is from Google Flu Trends.

This program draws one or more countries’ flu data in one figure. It downloads data from Google and saves data to fludata.csv in current directory. I was thinking to do some process, but I didn’t know what I can do and I wasn’t really interested in this.

Here is a screenshot:

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJAHK2lU5zebsK7Gzi_giS0ufYKrMmrNtq1eMw0cjPkqyJja_s4K8q1Pwnc_U5_iCU8IAIrmPwysgaeIy96IkeFWCLYYhL-cXaRDxhGNsU2JTgq7Ej6vO1QijsQ1ML7P18hRvKeZWMJVo/s640/2009-10-18-174725_905x630_scrot.png

You can download the code at Google Code.

1   Window.visible

When I first time use Glade to create the UI, I didn’t know the window.visible is False by default, so you have to either set it in Glade or run window.show().

2   gtk.glade.XML(gladefile, windowname)

windowname must match window.name in Glade. It’s obvious but I thought one Glade XML one window, I set it to something else, therefore my window never show up. One Glade XML could have many windows.

3   missing int type data in csv

3.1   Loading using matplotlib.mlab.csv2rec

d = dict(zip(fields, [lambda value: int(value) if value != '' else nan]*len(fields)))
self.rec = mlab.csv2rec(CSV_FILENAME, converterd=d)

Where fields is a list of field names. If a data is missing, it will be an empty string '', I think using NaN (Not a Number, numpy.nan) may be a better idea to represent it than 0 (zero).

There is another way to deal with missing data by giving missingd to csv2rec, but the version of matplotlib on my computer doesn’t have it. It should like converterd but with values which you want to assign when data is missing.

3.2   Feeding to gtk.TreeView

If you set int type to gtk.ListStore, then it will not accept numpy.nan. So str type may be okay to use.

4   Multiselection mode in gtk.TreeView

treeview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)

5   Don’t forget NavigationToolbar

Users still need a way to zoom in/out.

from matplotlib.backends.backend_gtkagg import NavigationToolbar2GTKAgg as NavigationToolbar

vbox.pack_start(canvas, True, True)
vbox.pack_start(NavigationToolbar(canvas, window), False, False)

I hope I could find a way to do data line tracing. It’s hard to tell Y=? when X=123.