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.

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.

python

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.

A “big data” approach to studying parliamentary scrutiny

I have a new article out in the British Journal of Politics and International Relations: In Search of the Politics of Security. In it, I take what could be called a big data approach to the study of parliamentary scrutiny, by scraping information on the passage of legislation from the UK parliament’s website. The website’s current incarnation is relatively recent and there isn’t that much legislation passed every year so I was only able to scrape information on around 150 successfully passed bills. However the information which does come out is quite rich – all recorded votes, amount of time it took to pass the legislation, links to debates and committee hearings, etc. So I still think of it as a kind of big data approach.

My question was pretty simple: does the UK parliament offer less scrutiny on legislation which relates to crime and national security? This emerges from my interest in securitization theory and security politics, which I must admit I have recently been drifting away from slightly (as the war on terror has died down I also think it is becoming slightly less relevant). The project started off as an attempt to measure the scale of this difference, based on what I perceive as a quite widespread assumption that legislators essentially roll over when the government wants to toughen up crime or security law. In the end however I found a relationship in the other direction – such legislation seems to get more attention and scrutiny. It’s a smallish dataset and a limited time period so the conclusions aren’t hard and fast, nevertheless I think it’s a bit of a challenge to the way security politics is often conceptualised.

Why do MOOC users meet face to face?

Last week Monica Bulger, Cristobal Cobo and I presented a paper at the ICA’s pre-conference on higher education innovation. Monica and Cris are the experts in this area and did most of the heavy lifting, but I was pleased to take part, mainly out of a professional curiosity about how Massively Open Online Courses may or may not be changing the face of higher education. In the paper we looked in particular at patterns of offline meetups amongst the users of these online courses, using data from the Meetup API (my role being to facilitate data gathering and manipulation). Meetup have an open and generous stance to API data, and after a bit of coding I was able to extract information on several thousand face to face meetings of students taking part in Coursera courses in over 100 countries around the world.

Meetup - Map

More clicks on Wordle produced a word cloud of the titles of each meetup, which I can’t resist because it looks so nice even if it probably isn’t a good way of doing science.Word Cloud - Titles

What does it all mean? Beyond showing the impressive worldwide reach of Coursera, and the fact that people like face to face interaction when they are learning, we are still deciding to be honest with you. Suggestions welcome.

Follow

Get every new post delivered to your Inbox.