Tags Python , Qt

When it comes to plotting and drawing graphs, Python has quite a few options. In this post I want to focus on the two I've had experience with, so far. After a short presentation of the libraries, I will compare them on various criteria which I found important.

The contestants

matplotlib

matplotlib is arguably the most popular and best known Python plotting package out there. Armed with NumPy, SciPy and matplotlib, Python programmers aim at the domain currently occupied by the uncontested heavyweight of numeric computing - Matlab. And quite frankly, Python doesn't fare too badly in this contest.

So matplotlib has been designed from the ground up to support a huge range of plots, and be particularly suitable for scientific plotting. It provides publication-quality plotting, generating high-resolution anti-aliased images that can (and have been) incorporated in real reports and scientific articles.

PyQwt

PyQwt is a different beast. It provides Python bindings to the C++ Qwt library, which is a set of classes extending the Qt framework with plotting and other widgets for scientific and engineering applications.

While not aimed at scientific publication, PyQwt is relatively light-weight, integrates very well with PyQt and provides additional widgets that can be useful for some applications that require plotting.

Visual appeal

Without question, matplotlib produces prettier plots. It uses the Agg 2D rendering engine as its backend and produces the best looking plots I've seen from any tool or library.

PyQwt's plots are homelier, but still look pretty good. There's an option to render the plots in an anti-aliased manner as well (using the RenderAntialiased option), which makes the plots look much better (at the price of performance), though still not quite like matplotlib.

Plot options

matplotlib has built-in support for much more plot types than PyQwt. Although when you have a good framework (and PyQwt is one) it's not difficult to program any type of plot, matplotlib delivers most types you could (and couldn't) think of out-of-the-box.

Also, if math is your thing, you'll be happy to discover that matplotlib can render LaTeX-typed equations onto the plots.

Integration with GUIs

Since plotting is an inherently visual activity, integration with a GUI is important for a plotting library.

matplotlib knows how to play with Tk, wxPython and PyQt quite well. But it's a separate library, with its own types, conventions and needs.

PyQwt is seamlessly integrated into the PyQt model. The pens you use to plot in it are PyQt pens, the events and slots of the plot are the same events and slots of the GUI, and everything fits together just perfectly. This is very useful if integration with PyQt is what your application needs the most.

Interaction

For some plots, interaction is important: you want to allow the user to navigate the plot - zoom in and out of areas, have a cursor with a known location, and so on. matplotlib provides built-in interaction with its toolbar that can be easily attached to a plot. Just a few lines of code, and you have a pretty good means of interaction.

PyQwt is different in this aspect. Though it doesn't have a built-in toolbar widget, here its great integration with PyQt shows its strength. Since PyQwt is so tightly tied into the Qt GUI and event system, it's possible to create very complex interactions with it. While matplotlib also has an event capability that can be tied to the host GUI's, it's much less natural and less convenient to work with.

So if you need just the basic integration, matplotlib has it built-in, and in PyQwt you will have to spend a bit more effort. But if you need a complex interaction, PyQwt is better.

Documentation

matplotlib is much better documented than PyQwt. For PyQwt you just have the examples and the class reference of Qwt (the C++ library) to look at. For matplotlib you have a whole handbook almost 500 pages long, and several other helpful documents.

But this is mostly because matplotlib is much more complex. As I've mentioned before, PyQwt is so integrated with PyQt that you don't need that much documentation to use it. It's basically another PyQt widget you can use in your application - the learning curve is much gentler.

Performance

Here, in my experience, PyQwt wins big. matplotlib is a large and heavy library, and adding it to an application considerably increases its load time. PyQwt is much lighter and faster as well. If distributing applications is important, keep in mind that the executables produced (say, with py2exe or PyInstaller) with PyQwt will be much smaller, and will load more quickly.

If you build PyQwt on your own, you can also keep numpy support out, which will make the library even smaller. Unfortunately, the pre-built binaries for Windows come with numpy support obligatory.

Extra capabilities

matplotlib is a library for plotting, period. It contains a huge variety of different plot types that will satisfy ultimately all of your plotting needs.

PyQwt, on the other hand, aims at other goals as well. It contains other widgets useful for scientific and engineering apps, such as dials, knobs, wheels and sliders. These can be very useful for some applications, especially in the domain of test&measurement software (as a competition to LabView or Matlab).

Conclusion

I think that there's place for both libraries in the toolkit of a Python programmer, especially if PyQt is his GUI of choice.

If you need to incorporate a plot into a complex GUI, and provide interesting interaction between the plot and the rest of the widgets (especially dynamic updating), PyQwt is very suitable for the task. It's lightweight (which helps speed up large applications), very well integrated with PyQt and has a set of additional widgets you can find useful.

On the other hand, whenever you need to crunch some numbers with Python, matplotlib is indispensable. Armed with a good interactive terminal (such as Pydee or IPython) and matplotlib, mathematical explorations are as easy as with Matlab, and the produced plots can be saved to images and incorporated into reports and documents.

Also, if you don't use PyQt but prefer wx or Tk instead, then matplotlib is perhaps your best option for GUI-integrated plots as well.


Comments

comments powered by Disqus