Review: Matplotlib for Python Developers by Sandro Tosi
Summary: I’d recommend this for people interested in adding Matplotlib functionality to GUI and web applications, and for those who need a bit more information on how to do advanced plotting with Matplotlib. Most general users will be able to get the information they need from the Matplotlib website.
Reference: Tosi, S., 2009, Matplotlib for Python Developers, Packt Publishing, Birmingham, UK, 293 pages,Â Amazon Link Publisher’s Website
This book is designed to teach readers how to do two things: make graphs with matplotlib, and embed matplotlib into GUI and web-based applications. My primary focus in my working life is on creating graphs using Python directly, rather than embedding graphs in other applications, although I was interested to learn about the latter, as this may come in useful in a few years time (for example, when building GUI front-ends for some of my modelling code). I think this book succeeds more in the latter objective than the former – as much of the first half of the book seems to be repeating information that is easily available on the Matplotlib website – where it is actually explained and phrased better!
Starting at the beginning, the book explains what matplotlib is, and gives a list of good reasons for using Matplotlib. However, there is then a fairly long section on output formats and Matplotlib backends. This is fairly technically detailed, and not totally easy to understand, so presenting it before the ‘Getting Started with Matplotlib’ chapter seems rather strange. This ‘Getting Started with Matplotlib’ section covers the basics of plotting including plotting lines, changing axes, adding gridlines and creating legends. It goes on to explain how to save graphs to files, how to use the interactive plot display window, and how to use the ipython pylab mode. After this, though, there is another strange arrangement of chapters – with a detailed section on matplotlib configuration files. This, again, is fairly detailed, and not hugely useful (as I haven’t yet seen any examples online where people have chosen to modify the configuration files), and it is strange to have it before the (far more useful) section on customising plots.Â This section is fairly good, covering how to change line styles and colours, polar plots, and plotting text and annotations. However, there is no mention of how to include more than one plot in a figure – which seems a bit of a strange absence.
The next chapter deals with object-oriented Matplotlib – that is, accessing the constituent objects directly rather than through the PyPlot commands. This is interesting, particularly as it isn’t explained particularly well on the Matplotlib website (that really just has an explanation of all of the methods in each class), but it could do with more information on the ‘big picture’ – a basic class diagram showing how all of the difference classes (Figure, Axes etc). I was pleased to see the inclusion of a section on plotting dates – something which I find I need to do quite often, but which often isn’t covered in tutorials and books.
The first part of the book now seems finished, and the book starts the section about integrating Matplotlib with various GUI and web frameworks. However, there is still one more useful chapter for those who aren’t interested in web or GUI programming, and that is the last chapter – ‘Matplotlib in the Real World’ – which covers a number of examples of using Matplotlib in the real world. Again, this seems a slightly strange arrangement of chapters, as none of the content of this chapter depends on anything covered in the GUI and web programming chapters. As for the examples, they are fairly good examples of real-world usage, and use freely-available data from the internet. As with all real-world examples of code – the code is more complicated than it possibly should be in a teaching book – but it does show you how to do everything from obtaining the data to producing the final plot. This chapter also contains a fairly detailed section on plotting maps in Matplotlib – again, a useful section, but it feels like a rather unusual inclusion.
It would be unfair of me to make much comment on the chapters of the book on integrating Matplotlib with GTK+, Qt4 and WxWidgets as the author emphasises that the chapters will not teach you the basics of programming in these GUI frameworks, and I have no experience in using them. However, I understood most of the content of these chapters without having any experience with the GUI frameworks, which bodes well for how useful the chapters will be for those with more experience. I have more experience with web programming, and found this chapter very useful. It gave me a number of ideas for how I could include matplotlib plotting in an interactive data visualisation web-application I am hoping to develop for a monitoring system that I have designed. I was originally planning to use CGI scripts to run a python script which wrote its output to a file, which was then displayed in the page, but through reading this chapter I found a far better way to do it – by using Matplotlib’s functionality to send the output directly to stdout – which, in this case, is the webpage itself. Useful tips are also given about using StringIO to a similar thing.
Overall it is the second half of the book which is really useful – particularly the sections on using Matplotlib with GUI applications, web applications, and the real-world usage examples. The first two chapters seem to repeat, in a more confusing way, the Matplotlib tutorial available on the project’s website. I must admit to being slightly concerned at the standard of English throughout the book, which may hint at poor proof-reading. Examples include “e is the most common letter in the English writings” and “Matplotlib has a plotting function ad hoc for dates” – not a major problem, but something that grates on me when I’m reading.
(Disclaimer: I was given a free copy of this book by Packt Publishing)