matplotlib

Matplotlib is a library with a very 'Matlab' like environment for generating publication quality figures. You can output to raster formats (e.g., PNG, TIFF, JPG) immediately in a pop-up window or to vector (e.g, EPS, PS) formats that are saved as files. The style of graphs and syntax for creating them, especially when combined with the added Python language constructs through NumPy will be very familiar to users of Matlab.

Here are two ways you can use matplotlib plot in your Vizard script

1. Save a plot of your Vizard data to one of the various file formats supported or display a matplotlib figure using a GUI toolkit (e.g. wxPython). The limitatation of displaying an interactive figure (i.e., using matplotlib's show() command) is that it is a blocking command and Vizard will halt until the window is closed. Saving the figure to file will not block the Vizard update loop.

2. Apply a matplotlib figure as a texture within your Vizard world using the vizmatplot.py module. This avoids using matplotlib's show command and allows us to load a plot as a texture in Vizard and update in in real time. Watch the short video to see this in action. After following the matplotlib installation instructions in this article you can download the vizmatplot.py module with demos here.

To get matplotlib working within your Vizard script follow the installation instructions and then run the example code. Links to the home web pages of the packages are included below.

Installation:

  • Matplotlib requires the installation of a few 3rd party packages before it can be used with Vizard. Below is a list of the required packages and a link to instructions on how to install each of them:
    • NumPy, which adds sophisticated arrays and mathematical functions to Python
    • wxPython, a GUI toolkit that the matplotlib figure is displayed in
  • Go to the matplotlib download page and download the latest version of the matplotlib installer. Use the table below to determine which version of Python your Vizard installation uses:
    • Vizard 2.x uses Python 2.3
    • Vizard 3.x uses Python 2.4
    • Vizard 4.x uses Python 2.7
    • Vizard 5.x uses Python 2.7
  • Run the installer. It should automatically detect Vizard's Python installation. If you have multiple Python installations on your computer, make sure you select the Vizard Python installation.

Configuring:
You need to modify line 31 in the file called matplotlibrc, which is found in the Vizard30binlibsite-packagesmatplotlibmpl-data folder. Change it to read:
backend: WXAgg

Examples:
In this example a plot is generated from some Vizard data and then saved to a PDF file

import viz
import vizinfo
import viztask

viz.go()

info = vizinfo.add('A matplotlib figure will be saved as a PDF file in thencurrent directory after the spacebar is pressed')
info.translate(0.8,0.98)

#set sample rate in hz
SAMPLE_RATE = 60

#set max number of samples
MAX_SIZE_OF_ARRAY = 600

#create arrays to store data and fill with zeros
array_time, array_height = [],[]
 
for i in range(0,MAX_SIZE_OF_ARRAY):
    array_time.append(0)
    array_height.append(0)

###---physics simulation---###
viz.phys.enable()
ball = viz.add('ball.wrl', scale = [2,2,2], pos=(0,10,25), euler = [180,0,0])
ballphys = ball.collideSphere(bounce = 1.5)
ground = viz.add('tut_ground.wrl', pos = [0,0,25])
ground.collidePlane()

#task function that adds to ball height and time arrays every frame
import time
starttime = time.time()
def getData():

    for i in range (0,MAX_SIZE_OF_ARRAY):
        array_time[i] = time.time() - starttime
        array_height[i] = ball.getPosition()[1]
        yield viztask.waitTime(1.0/SAMPLE_RATE) #wait at least a frame before setting new data points
           
#call our task function
data = viztask.schedule(getData())

#take the arrays and create a simple plot of time and ball height and save it as a
#PDF file in the current script directory
from pylab import *
def savePlot():
   
    plot(array_time, array_height, linewidth=1.0)
    axis([0,10,0,10])
    xlabel('time (s)')
    ylabel('height (m)')
    title('matplotlib and Vizard')
    grid(True)
    savefig( "Vizard_Plot.pdf", format='pdf' )

#save a plot of our data when spacebar is pressed
vizact.onkeydown(' ', savePlot)