For the past month or so I’ve been working away at something that in hindsight looks deceptively simple – but nonetheless taught me loads of new techniques along the way: I wanted to plot data on a map in such a way that it would reflect ratings by province, taking into account the number of widgets per province (we’ll call them widgets so I don’t give away the real data :)).
…In the beginning I was challenged by how to get the map
….Then by how to calculate with reasonable accuracy the latitudes and longitudes for plotting onto the chart
…Then it was how to best represent the data – I started with a normal scatter plot but there were way too many overlapping markers in the large provinces for the data to be meaningful
…Then it was understanding how the pie scatterplot was really constructed and what it was doing in the background so that I could translate it appropriately for my data
…Then it was how to manipulate my data to be compatible with this format
…Then it was to generalise my code so that it would work for differing numbers of pie segments, province sizes, % results
…And a very annoying glitch where certain pie slices were seemingly randomly generated bigger than others – of course it was not random…
…There was something to keep my mind buzzing every night!
Some of the building blocks that I ended up using were:
- Google maps API
- requests library for communicating with Google maps
- io library for decoding the image content returned
- And then a wonderful matplotlib guide to doing scatterplots with pie markers
Having achieved the result I want, I’m going to take the next few days to break it all down for myself so that (hopefully) I can extract the concepts that were most useful – and that could be re-used next time!