Book review: Matplotlib for Python Developers

Matplotlib for Python Developers, Sandro Tosi

Let me get the boring disclaimer-y bit out of the way first: I didn’t buy this book – Packt Publishing sent me a free copy in return for me writing a review of it. They’ve not made any specific requests about the contents of my review, just that I should write a review. And, in fact, I should give them props for not hassling me for not getting around to writing this until now – about eight weeks after they sent it. So either they forgot they sent it to me, or they are terribly patient. I’m choosing to assume it’s the latter 🙂

With that out of the way… matplotlib is a graphing library. I’ve used it a lot for my CurrentCost energy monitoring app – all of the graphs in the app are created using matplotlib. And while it is very powerful, it can be a little tricky to get your head around. I still struggle with getting the graphs exactly how I want them even now.

That was why I agreed to do this review – I liked the sound of the book and thought it’d be useful for me.

Overall, it’s a really good book. It starts off going through all the basics, both in terms of explaining the ideas and concepts, and in the practical steps needed to get set up. I’m not normally a big fan of reading text books, preferring to figure stuff out for myself as I go along, but found that even here I picked up tips that I never knew and subtle features that I’d missed.

A nice flow-chart kicks off the detail into producing different types of graphs and chart. It starts from asking “What would you like to show?” (e.g. comparison; distribution; composition; relationship; etc.).

Following this through for what you want to show, and what type of data you have, leads you to a suggestion for the type of graph that is most appropriate for your needs (e.g. line histogram; waterfall chart; pie chart; stacked column chart; scatter chart; etc.). For someone like me who doesn’t have the world’s strongest maths background, this was a really useful guide.

From there, it starts walking you through examples for how to implement the main graph types in matplotlib. Everything is well explained, with plenty of code samples and screenshots to illustrate the point. And the examples are very rich – it doesn’t just show you how to do the basic graph, it goes into detail into all the different things you can do to change the axes, labels, legends, and annotations. There was a lot of syntax here that had taken me a long time to figure out for myself, and I think this would’ve been a useful book to have had when I was starting the CurrentCost app!

There is a chapter for each main front-end. I’ve only used wxPython, so I only really read this, and skimmed the chapters on GTK and Qt. It’s good that it covered each of the different options in detail, though. There is also a chapter on writing matplotlib programs for the web, explaining how to use it with CGI and mod_python, as well as frameworks like Django and Pylons. I’d not tried using matplotlib for the web before,

My favourite bit was a discussion, walkthrough and sample of using matplotlib for real-time graphs. This is something I tried to do in my CurrentCost app – show a graph of home energy usage that updates when every reading is received every six seconds. But I couldn’t figure out how to do it properly in matplotlib and couldn’t find an example or explanation anywhere on the web. The cludgy hack I went for in the end was to just draw the graph, then when the next reading is received throw it away and draw a new one. It’s pretty icky, and unnecessarily resource intensive. And has been the source of a number of complaints about the feature from users.

So I was particularly pleased to find a walkthrough of how to create a real-time updating plot in matplotlib. Each line or two has a short paragraph of text explaining what is being done, so this should be very useful when I get the chance to rewrite my hacky code.

Finally, the book ends with a chapter covering interactions between matplotlib and other things “in the real world” – such as plotting data from a database, or plotting geographical data using maps from a third-party library Basemap.

Disclosure: http://cmp.ly/1

Tags: , , ,

One Response to “Book review: Matplotlib for Python Developers”

  1. Nancy Wise says:

    This is great! Thanks for the article. I am a newbie at django and this will be a big help.