PYTHON

3D ploter in python-tk with matplotlib.

Wishing to prove my assertion wrong on python-tk that piping python directly into tk/tcl interpreter is simple I tried to contradict myself by making a full GUI in matplotlib. Because, if you are not aware : matplotlib supports multi-target (Wx, Qt, gtk, tk, html) multi-platform widgets (Button, checkbox, text entry and so much more).

The challenge seemed pretty easy, to assemble an easy demo with simple example :

Thus, the added value resided in letting people fill in the min/max/step information for the relevant dimensions.

Without the colorbar I would have just been slightly annoyed by the slowness of the reaction of matplotlib as a GUI, but the colorbar posed a new challenge because it would either stack for each drawing or make plt.clf/ax.cla erase too much (see this great resource on when to use cla/clf in matplotlib).

So … I tried python-tk with matplotlib knowing all too well that you can embed natively matplotlib in tkinter interface.

And since it was working I kept it.

Here is a screenshot of the interface :

WARNING this code should not be let in inventive hands (such as bored teenagers) because there is an evil eval; it requires to be in care of consenting adults.

Some highlights of the code :

  • bidir python-tk requires setting the Popen PIPEs to non blocking and using select.select on the output
  • matplotlib is unusable in non blocking mode : once matplotlib has the focus you need to destroy it to plot another function
  • from np import * is evil, but it let you have access to all array oriented math function (sin, cos, exp, …)

This said, we have a pretty compact code of 128 lines of code that is pretty more reactive than using matplolib’s widget for a 3D ploter.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button