Matplotlib/VTK_Integration - SciPy wiki dump (original) (raw)
Just in case you would ever like to incorporate matplotlib plots into your vtk application, vtk provides a really easy way to import them.
Here is a full example for now:
1 from vtk import * 2 3 import matplotlib 4 matplotlib.use('Agg') 5 from matplotlib.figure import Figure 6 from matplotlib.backends.backend_agg import FigureCanvasAgg 7 import pylab as p 8 9 10 importer = vtkImageImport() 11 importer.SetDataScalarTypeToUnsignedChar() 12 importer.SetNumberOfScalarComponents(4) 13 14 15 imflip = vtkImageFlip() 16 imflip.SetInput(importer.GetOutput()) 17 imflip.SetFilteredAxis(1) 18 19 20 cube = vtkCubeSource() 21 22 cubeMapper = vtkPolyDataMapper() 23 cubeMapper.SetInput(cube.GetOutput()) 24 25 cubeActor = vtkActor() 26 cubeActor.SetMapper(cubeMapper) 27 28 29 cubeTexture = vtkTexture() 30 cubeTexture.InterpolateOn() 31 cubeTexture.SetInput(imflip.GetOutput()) 32 cubeActor.SetTexture(cubeTexture) 33 34 ren = vtkRenderer() 35 ren.AddActor(cubeActor) 36 37 renWin = vtkRenderWindow() 38 renWin.AddRenderer(ren) 39 40 iren = vtkRenderWindowInteractor() 41 iren.SetRenderWindow(renWin) 42 43 44 fig = Figure() 45 canvas = FigureCanvasAgg(fig) 46 ax = fig.add_subplot(111) 47 ax.grid(True) 48 ax.set_xlabel('Hello from VTK!', size=16) 49 ax.bar(xrange(10), p.rand(10)) 50 51 52 w,h = 1024, 1024 53 dpi = canvas.figure.get_dpi() 54 fig.set_figsize_inches(w / dpi, h / dpi) 55 canvas.draw() 56 57 58 extent = (0, w - 1, 0, h - 1, 0, 0) 59 importer.SetWholeExtent(extent) 60 importer.SetDataExtent(extent) 61 importer.SetImportVoidPointer(canvas.buffer_rgba(0,0), 1) 62 importer.Update() 63 64 iren.Initialize() 65 iren.Start()
To have the plot be a billboard:
1 bbmap = vtkImageMapper() 2 bbmap.SetColorWindow(255.5) 3 bbmap.SetColorLevel(127.5) 4 bbmap.SetInput(imflip.GetOutput()) 5 6 bbact = vtkActor2D() 7 bbact.SetMapper(hmap)
From zunzun Fri Aug 19 07:06:44 -0500 2005 From: zunzun Date: Fri, 19 Aug 2005 07:06:44 -0500 Subject: Message-ID: 20050819070644-0500@www.scipy.org
from http://sourceforge.net/mailarchive/forum.php?thread_id=7884469&forum_id=33405
If pylab is imported before vtk, everything works fine:
import pylab, vtkpython pylab.ylabel("Frequency\n", multialignment="center", rotation=90) n, bins, patches = pylab.hist([1,1,1,2,2,3,4,5,5,5,8,8,8,8], 5) pylab.show()
If however vtk is imported first:
import vtkpython, pylab pylab.ylabel("Frequency\n", multialignment="center", rotation=90) n, bins, patches = pylab.hist([1,1,1,2,2,3,4,5,5,5,8,8,8,8], 5) pylab.show()
then the Y axis label is positioned incorrectly on the plots.
From earthman Tue Oct 25 15:21:14 -0500 2005 From: earthman Date: Tue, 25 Oct 2005 15:21:14 -0500 Subject: Message-ID: 20051025152114-0500@www.scipy.org
The reason for this is that vtk comes with it's own freetype library, and this is the one being used if vtk is loaded first. Worse symptoms could be errors about fonts not being found. This is typically solved by importing vtk after other packages which might use freetype (pylab, wxPython, etc).
From mroublic Tue Jan 10 11:26:45 -0600 2006 From: mroublic Date: Tue, 10 Jan 2006 11:26:45 -0600 Subject: One more change I had to make Message-ID: 20060110112645-0600@www.scipy.org In-reply-to: 20050819070644-0500@www.scipy.org
When I first tried this, I had the error:
Traceback (most recent call last): File "MatplotlibToVTK.py", line 61, in ? importer.SetImportVoidPointer(canvas.buffer_rgba(), 1) TypeError: buffer_rgba() takes exactly 3 arguments (1 given)
I had to add 0,0 to the import line: importer.SetImportVoidPointer(canvas.buffer_rgba(0,0), 1)
I'm using VTK from CVS using the 5_0 Branch from around November 2005
The above code didn't run on my system. I had to change the following line: fig.set_figsize_inches(w / dpi, h / dpi) into: fig.set_figsize_inches(1.0*w / dpi, 1.0*h / dpi)
SciPy: Cookbook/Matplotlib/VTK_Integration (last edited 2015-10-24 17:48:23 by anonymous)