Use full power of Python in Slicer

The wx version dies on this line:

fig, ax = plt.subplots()

But using Agg directly works on the mac as shown in the example below.

(For context, this example is only slightly updated from something I put on the Slicer3 wiki page almost 10 years ago when we first started playing with python packages in Slicer).

import matplotlib
matplotlib.use ( 'Agg' )
from pylab import *

t1 = arange(0.0, 5.0, 0.1)
t2 = arange(0.0, 5.0, 0.02)
t3 = arange(0.0, 2.0, 0.01) 

subplot(211)
plot(t1, cos(2*pi*t1)*exp(-t1), 'bo', t2, cos(2*pi*t2)*exp(-t2), 'k')
grid(True)
title('A tale of 2 subplots')
ylabel('Damped')

subplot(212)
plot(t3, cos(2*pi*t3), 'r--')
grid(True)
xlabel('time (s)')
ylabel('Undamped')
savefig ( 'MatplotlibExample.png' )

import Slicer
r = vtk.vtkPNGReader()
v = vtk.vtkImageViewer()
r.SetFileName( 'MatplotlibExample.png' )

v.SetColorWindow(255)
v.SetColorLevel(128)
v.SetInputConnection(r.GetOutputPort())
v.Render()