How to create a 3D Surface Plot Animation

In this blog, we will show how to create an animation of a 3D surface where the surface color changes over time. A LabTalk script is required to make this video, as it is very hard for Origin to provide a GUI solution for all the different situations where users will want to animate their graphs. The final video, which shows change of pressure on a surface, will be as below:

 

 

We have put together an Origin Project (OPJ) so you can try this yourself. The OPJ includes

  • A matrix book with the surface height in a sheet named Height and Pressure data collected every 5 hours as multiple matrices in a sheet named Pressure
  • A surface plot with Z data from the Height  where the color map for the the surface is from the starting pressure data from the first matrix of the Pressure sheet

You can download the OPJ file (3D Surface Plot Animation.zip, 1450 KB) to get started.

 

Quick Start

First let’s walk through the steps needed to generate this animation video:

  1. Open the OPJ file
  2. Click Shift+Alt+3 to open the Script window
  3. Copy all lines of the LabTalk script shown below and paste them into the Script Window
  4. Select all lines of script in the Script Window, then press Enter to execute the script.

Note: Origin also provides other ways to organize and execute LabTalk scripts, For example, you can run the script from a file or from a button on Graph. To learn more ways to execute LabTalk scripts, please refer to this page.

 

What does the LabTalk Script do

The following key steps are carried out by the script:

  1. Update the color map of the surface plot with the next matrix
  2. Use the sec -pw command to wait for the surface to finish redrawing
  3. Add the graph image as a new frame to a video
  4. Export the video

Detailed explanation is given below.

Video Setting

The Video Writer(vw) object, is a LabTalk object that is used for creating videos in Origin. First we need to define the compression method with FourCC code

Following code is to set the video file name as “3D_Surface_Plot_Animation.avi” in the User Files folder

%Y is the string register reserved for the User File Folder path.

 

Set the color map of the surface plot with another matrix

Origin provide two options for color map of the surface plot in GUI: Fill piece by piece (piecewise) and Contour fill from matrix

contour_fill_from_matrix

Please note that for both methods,  to set from from the dialog, only matrix objects in the same matrix sheet can be selected, which requires the dimension and X/Y value of the matrix for color map to be the same as the matrix for surface

Using LabTalk, on the other hand, we can use the “set %C -b3c dataset” command to specify another matrix for the contour fill. Then this matrix can have dimensions and X/Y values different from the matrix for surface.

By the way, Origin 2015 does not support using LabTalk command for the Fill piece by piece option yet. We will consider implementing this in the next version.

 

Creating Each Frame of the Video

Most of the work is in the for loop to create each frame of the video. To specify a particular matrix as the contour fill color map, we will use range variable constructed from the matrix indexed with the $( ) substitution notation, which can convert a numeric value into a string:

In the above line, the $(ii) will be substituted by 1, 2…11 for the 1st, 2nd…11th matrix in the Pressure sheet. Each time the filled color will be mapped to the corresponding matrix.

vw.WriteGraph writes out the graph as a frame in the video. %H is the a string register reserved for the current window name

Close the video

In the end, we use  vw.Release()  to close the video. and save it to the User File Folder.

 

LabTalk Scripts with Comments

It may actually be easier to read the explanation as comments in the code, so we have expanded the original script with comments below:

 

For more examples of programmatically created movies, including Origin Projects and code, please visit the Animation Gallery on our website

Leave a Reply

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