data science, digital politics, smart cities...|

Point size legends in matplotlib and basemap plots

Python’s matplotlib and basemap can do a lot. I think I still prefer R’s ggplot, but I much prefer manipulating data in Python and sometimes it’s more natural to do the analysis in the same language.

Recently I have been combining the two packages to create maps of events happening around the world. The size and colour of the marker on the map can be varied, meaning it’s possible to fit quite a lot of information in one graphic.

Meetup - Map

However one thing I really struggled with was the legend. If you use different colour points matplotlib makes it easy to add a colour bar, with something like:

c = plt.colorbar(orientation='vertical', shrink = 0.5)
c.set_label("My Title")

Shrink gives you a quick way of adjusting the size of the bar relative to the graphic.

However I couldn’t find an equivalent command which would give me a legend for the size of the points (something that ggplot does easily). After fiddling with get_label() for ages, and trying to capture and do something useful with the results of plt.scatter(), I finally came across this useful post, which basically says that this feature doesn’t really exist and if you want such a legend you have to make it yourself. However, the trick to doing it is quite simple – draw three or four points on your plot with location set to [], [], (so they won’t actually show up), each one representing a certain size in your scale. These points can then be passed to plt.legend with some hand written labels. Overall it looks something like this:

l1 = plt.scatter([],[], s=10, edgecolors='none')
l2 = plt.scatter([],[], s=50, edgecolors='none')
l3 = plt.scatter([],[], s=100, edgecolors='none')
l4 = plt.scatter([],[], s=200, edgecolors='none')

labels = ["10", "50", "100", "200"]

leg = plt.legend([l1, l2, l3, l4], labels, ncol=4, frameon=True, fontsize=12,
handlelength=2, loc = 8, borderpad = 1.8,
handletextpad=1, title='My Title', scatterpoints = 1)

The results:

map with legend

Well, I still think that should be easier, but at least it works and it also gives you a lot of flexibility with what goes on the legend.

Opening up local council meetings

Over the last week or so I gave a short comment on BBC South Today (as well as various radio stations) about a recent government move to allow anyone to record local council meetings and then post the results on the internet. A good idea I thought, though some councillors were apparently worried about being taken out of context (though in fairness I think the majority of councillors were also pretty supportive).

BBC South 2

A few of the journalists asked some interesting questions about how the move might affect the style of local politics, and how it all compared with the televisation of parliament which started in the late 1980s (and which many MPs fought bitterly). Unlike parliament where televisation is pretty regulated, anyone with a phone will be able to record in council meetings, which means that councillors will have to be a lot more on their guard I suppose; though they are also likely to be the ones who do most of the filming (either of themselves or of any opponents they can catch out). There may also be a move towards more soundbites: if you are looking for coverage, the key thing is not being recorded so much as saying something which the national press want to repeat. In the 1990s, for example, Teresa Gorman suggested “cutting the goolies off” sexual offenders, rather than calling for castration, because she knew it would put her on television (see this article).

Regardless of any style changes I think if it can provide a boost to the visibility of local democracy then the move will be worth it.

By |2014-08-11T14:00:27+00:00August 11th, 2014|Politics and Democracy, Social Media|0 Comments

Python and Social Media Data for the Social Sciences

In July I gave two short workshops at the OII’s Summer Doctoral Programme and also at the Digital Humanities at Oxford Summer School. I had two great groups of bright PhD students and postdocs to teach to. The sessions were only two hours long, and its a big challenge to teach some meaningful programming skills in such a period to complete beginners (in the end, I decided to walk them through a small example project of getting news articles from an RSS feed and checking how many times they have been shared on Facebook, providing most of the code myself). I also rely on lots of technology which I can’t fully control, which is a risk (I want to teach people to connect to things like the Facebook API, which means I need to rely on getting python working on their machine, on their machine connecting to the internet through the visitor wifi, and on the FB API being up and running during class). But the tech worked, mostly, and overall experience was really positive.


In the future however I strongly believe that social science needs a better way of integrating computer programming skills into undergraduate and postgraduate teaching, so that these doctoral level workshops can be more about mastering skills and less about training beginners. So I suppose the hope is that in a few years I won’t need to teach such courses any more, even if I do enjoy them.