OriginLab Corporation - Data Analysis and Graphing Software - 2D graphs, 3D graphs, Contour Plots, Statistical Charts, Data Exploration, Statistics, Curve Fitting, Signal Processing, and Peak Analysis     
 
Skip Navigation Links
All BooksExpand All Books
PythonExpand Python

Python

Introduction

Origin provides embedded python support, allowing users to execute python code within Origin. To include Python in Origin, the Embedded Python check box (checked by default) has to be checked during the installation of Origin. If you choose not to install Python the first time, you can always install it later by modifying the Origin installation. This embedded installation does not interfere with, or depend on, any installation of python you may have on your machine. For instance, you may even have a different version of python installed outside of Origin. The embedded python version is updated periodically in new Origin versions.

The current version, Origin 2017, uses version 3.5.2 for the embedded installation. If you failed to run Python 3.5.2 on your computer, please refer here.

The key benefits of the embedded installation are:

  1. As an Origin user, you do not need to spend time downloading and installing Python yourself. Python is readily available from within Origin right away.
  2. The Origin GUI provides several ways for you to execute existing Python code.
  3. A PyOrigin module is included in the embedded installation, which provides you with immediate access to Origin objects and properties. This allows you to take existing Python code and quickly expand it to interact with elements of the Origin interface, such as filling worksheet columns with results of a calculation, plotting the data, and publishing your results.

To get an overall picture of the benefits of using the embedded Python, we recommend that you examine the Origin sample projects located in the Samples\Python subfolder under the Origin installation folder.

Running Python in Origin

There are several ways to execute Python code from within Origin. Origin also provides its own PyOrigin module which enables users to interact with Origin objects such worksheets or graphs in their Python code. The available classes and functions in PyOrigin can be referred in the reference section of this document. This section describes the various methods of running embedded Python. Origin can also be accessed as automation (COM) server from Python installed outside of Origin. For available COM methods and properties, please refer to our Automation Server documentation.

Origin supports Python 3.5.2 and Python 2.7. By default, it is Python version 3.5.2 running in Origin. To switch to use Python version 2.7, open Script Window and select menu Edit: Script Execution: Python 2.7. It can be also switched by a system variable @PYV. When @PYV = 3 (Default), the embedded Python is 3.5.2, while if @PYV = 2, it is 2.7. The following document page is written based on the assumption that Python 3.5.2 is used in Origin. To set value for a system variable, click Tools:System Variables to open the Set System Variables dialog. Type PYV in the variable column. The current PYV value is shown and can be modified.

Run Python command in Console

To run Python in console, click Tools:Python Console.... You can execute Python command directly in Python console. Be aware that when you import the module, "PyOrigin" is case sensitive.

Python console.png

Run Python command or script in Script Window

Running Python commands in script window is basically the same as running commands in the console:

  1. Make sure Window:Script Window is checked.
  2. In the Script Window, make sure either Edit:Script Execution:Python 3.5 or Edit:Script Execution:Python 2.7 is selected.
  3. Type in single-line Python command. For example:
    print(2**5)
  4. Hit Enter to execute it.

You can also execute multiple-line Python script at once:

  1. Go to the system variable list dialog (Tools:System Variables).
  2. Change the value of @PYI to 0.(This will disable the interactive execution mode.)
  3. Go back to the script window.
  4. Type in or copy-paste your Python script. (make sure either Edit:Script Execution:Python 3.5 or Edit:Script Execution:Python 2.7 is selected. )
  5. Highlight all the codes and then hit Enter to execute the script line by line.

Try the following script:

import PyOrigin
wp = PyOrigin.CreatePage(PyOrigin.PGTYPE_WKS, "MyData", "Origin", 1)  # create a new worksheet
wks = wp.Layers(0)  # select the first worksheet in the workbook
pArr = [[x for x in range(5)] for x in range(2)]   # create a dataset
wks.SetData(pArr, 0,0)   # import the data into the worksheet

Run Python File within Labtalk

In order to run a Python file (with .py extension) in Origin's LabTalk script, use either of the following LabTalk command/object:

  • Use the run -pyf command option switch in LabTalk to run a local Python file:
  1. Cilck Window:Script Window to open the script window, make sure Edit:Script Execution:LabTalk is selected.
  2. Copy and paste the following script into the script window, highlight all and click Enter to execute.
// Define a string for the file path and name of the .py file
string str$ = system.PATH.PROGRAM$ + "\Samples\Python\ListMember.py";
// Run the .py file
run -pyf "%(str$)";  
 
// This .py file lists all provided functions in PyOrigin module
  • Use the run -pyp command option switch to execute a Python file attached to the Origin project file:
  1. Open the code builder by clicking View:Code Builder.
  2. Right click Project in Workspace and select Add Files.. to include Python files:\Samples\Python\ListMember.py.
  3. Go back to the script window by clicking Window:Script Window, make sure Edit:Script Execution:LabTalk is selected.
  4. Copy and paste the following script into the script window, highlight all and click Enter to execute.
run -pyp ListMember
  • Use the run.python() object method in LabTalk:
  1. Cilck Window:Script Window to open the script window, make sure Edit:Script Execution:LabTalk is selected.
  2. Copy and paste the following script into the script window, highlight all and click Enter to execute.
// Define a string for the file path
string file_path$ = system.PATH.PROGRAM$ + "\Samples\Python\";
// Set path of the python file
run.python("%(file_path$)", 32);
// Run the .py file named as ListMember.py under the selected path
run.python("ListMember.py", 2);
 
// This .py file lists all provided functions in PyOrigin module

Run Python code and evaluate Python Expression within Labtalk

It is also possible to run Python command lines or evaluate Python expression within Labtalk script which adds more flexibility for the LabTalk programming.

Again make sure Edit:Script Execution:LabTalk is selected. Convert Python command lines into a string variable in LabTalk, then use run -py command option switch or run.python() object method to execute it:

// Define the Python command line(s) as a LabTalk string variable
str$ = "a = 2 ** 8;print(a)";
// Execute Python command lines
run -py %(str$); 
// or
run.python(myname='Origin';print(myname));

You can also evaluate a Python expression using either of the following LabTalk command/object method: run -pye command option switch or run.python() object method in LabTalk:

// It evaluates the Python expression 10 % 4, % is the Modulus operator in Python
// It outputs the evaluated value 2, which is the remainder of 10 divided by 4
run -pye 10 % 4;
//or
run.python("10 % 4", 1);

Examples

Some Python examples are installed under \Samples\Python\ folder of Origin Program folder to demonstrate the practical applications of Python in Origin. For each example, open the .opj file and click the button to run the Python file attached to the project. To view the Python file behind, simply click the button Show Python code. Or open the Code Builder (View:Code Builder or ALT+4) and browse to the Project folder in Workspace panel to access the attached Python file. If you are also interested in linking a button to the Python file, please refer here.

Name Description
SendDataToWks.opj This example sets the XY designations for worksheet, then fills columns with data. A dialog is created when the script executes.
ImportWksPlotScatter.opj This example imports an ASCII file into an Origin worksheet, then makes a scatter plot.
ImportMatrixPlotContour.opj In this example, we scan the contents of an ASCII file, pick up the numeric values in sequence,and then save them in a 2D array in Python. The 2D array is then imported to an Origin Matrix sheet. A contour graph is eventually plotted from the matrix.
ExportAllGraphs.opj In this example, we call X-function expGraph in Python to export all graphs in the project to the User Files Folder. Note that XF method requires Origin 2016 or later.
PolyFit.opj This example imports an ASCII file into an Origin worksheet. Then X-function fitpoly is called to run polynomial fit on the third column of the data. The fitting results of different orders are stored in different worksheets.
PeakAnalysis.opj This example imports power spectra at various temperatures into an analysis template continuously. For each temperature, the spectrum is analyzed by Origin Peak Analyzer to identify the peak position. The calculated peak position is then saved into to a list in Python which is then plotted as a function of temperature in an separate Origin graph.
PeakMove.opj This example exports a video showing the change of spectrum shape with increasing excitation wavelength.

Click this link to download the latest Python examples.

Installing Python Extensions

The embedded Python in Origin includes all the basic modules and the modules which are installed to your Python application. If you want to use other Python extensions, e.g. PyQt4, numpy, scipy, follow the procedure below:

  1. Make sure the downloaded Python extension version is either version 3.5.2 or version 2.7 to be compatible with the embedded Python version in Origin.
  2. Make sure the downloaded Python extension corresponding to same CPU register size as the Origin being running - 32 bit or 64 bit. To know whether current Origin verison is 32 bit or 64 bit, you can directly read it from Origin program window title bar or type command " system.bits=;" in Command Window or Script Window.
  3. Make sure the installed Python extension path is properly appended so it can be directly used by Python import command.
  4. Make sure current Python running version is compatible with downloaded Python extension. By default, it is Python version 3.5.2 running in Origin. To switch to use Python version 2.7, open Script Window and select menu Edit: Script Execution: Python 2.7.


If a Python extension is installed in the computer under a specific folder, e.g. C:\Python35\Lib\site-packages, you need to add this installation folder to the system path list before you import it. To add the installation folder to system path list, the recommended approach is to run the following Python statements in the embedded Python environment of Origin (either directly run statements, or run from files):

import sys
 
# Replace the value of string variable py_ext_path 
# with the actual Python extension installation path in your computer
 
py_ext_path = "C:\Python35\Lib\site-packages"
 
if py_ext_path not in sys.path:
    sys.path.append(py_ext_path)

PyOrigin Reference

This section provides information on PyOrigin Classes and Global Functions.

 

© OriginLab Corporation. All rights reserved.