Authors Note: This post has been superseded by a new post with more information and plotting options
Origin 2021 came with much improved embedded Python support. But it also brought support for accessing Origin from Python installations external to Origin. And similarly to how you can use the originpro Python package in embedded Python, you can use it with external Python.
In this blog post, I’ll show you the easiest way to generate great Origin-based graphs from external Python. For example:
The plan is to create an Origin project file ahead of time to serve as a template project for the graph you want to create via Python. You will create a new workbook (or matrixbook) with an example of your data and then create the graph of your choice. Now is your chance to do whatever you want with the graph- customize it exactly the way you want.
The only big consideration is whether you foresee that the real data will vary from the example to the point where the axes ranges may change. If you see that as a possibility, account for that in the Axis Dialog (e.g. set the Axsis Scale Rescale to Auto).
You will want to make a note of the short name of the workbook because you are likely to need it in your script.
Finally, you can optionally delete your example data from the worksheet and save the Origin project file to make the file size smaller.
Please download the zip file for this blog. There is an Origin project file template.opju and python file script.py.
Rather than trying to explain what needs to be coded, I simply commented the script in zip file below really well so you can learn from it. It is meant to operate with the template project included in the zip file. As long as the script and template OPJU file are in the same folder, all should work properly. By the way, you’ll need to install numpy. You can check and install it in Python Packages dialog opened by Connectivity: Python packages menu.
# Must install numpy.
import originpro as op
import numpy as np
# Very useful, especially during development, when you are
# liable to have a few uncaught exceptions.
# Ensures that the Origin instance gets shut down properly.
# Note: only applicable to originpro run with external Python.
def origin_shutdown_exception_hook(exctype, value, traceback):
'''Ensures Origin gets shut down if an uncaught exception'''
sys.__excepthook__(exctype, value, traceback)
if op and op.oext:
sys.excepthook = origin_shutdown_exception_hook
# Non-Origin code.
# Define vectorized exponential decay data generator function.
def exp_decay_(x, y0, A, x0, t):
noise = np.random.normal(0,1)
return y0 + A * math.exp( -(x - x0) / t) + noise
exp_decay = np.vectorize(exp_decay_)
# Define vectorized logistic data generator function.
def logistic_(x, A1, A2, x0, p):
noise = np.random.normal(0,1)
return A2 + (A1 - A2) / (1 + (x / x0) ** p) + noise
logistic = np.vectorize(logistic_)
# Generate some mock data to insert into Origin.
x = np.arange(start = 0, stop = 61, step = 1)
y1 = exp_decay(x, 0, 100, 1, 5)
y2 = logistic(x, 10, 100, 20, 1)
y3 = exp_decay(x, 0, 130, 2, 7)
y4 = logistic(x, 0,90, 40, 1)
# Start Origin-related code.
# Set Origin instance visibility.
# Important for external Python.
# Should not be used with embedded Python.
# Open the Origin template project from same folder as this script.
op.open(os.path.dirname(os.path.realpath(__file__)) + r'\template.opju')
# Get book named Book1.
book = op.find_book(type = 'w', name = 'Book1')
# Get 1st worksheet.
wks = book
# Insert generated data into columns.
wks.from_list(col = 0, data = x.tolist())
wks.from_list(1, y1.tolist(), comments = 'Experiment 1')
wks.from_list(2, y2.tolist(), comments = 'Experiment 1')
wks.from_list(3, y3.tolist(), comments = 'Experiment 2')
wks.from_list(4, y4.tolist(), comments = 'Experiment 2')
# Now, graph automatically gets updated along with axes.
# Save the project to same folder as this script.
op.save(os.path.dirname(os.path.realpath(__file__)) + r'\results.opju')
# Exit running instance of Origin.
# Required for external Python but don't use with embedded Python.
# Well, technically it isn't required if you actually want Origin to stay open!
I hope this post was enlightening and helps you learn how to access Origin from external Python.