Origin 2021 (and later, of course) is an excellent graphing companion for Python for these reasons:
- Designing complex, compelling graphs is easy via Origin’s GUI.
- Origin allows you to focus on the data and analysis part of your Python code and not the intricacies of graph generation.
- Your graphs are modifiable and flexibly exportable long after your script has finished running.
In this blog post, I’ll show you how to use Origin’s graph template features from within your Python code. I’ll present four types of templates:
- Basic graph template: This is the fundamental type of graph template in Origin. You load it and add plots to it. It knows about the type of plot being added.
- Cloneable template: This is a “smart” template. It automatically knows which data to plot from a given worksheet. Cloneable templates can be very complex.
- Embedded graph template: This is a template where the graph is embedded into a workbook and is automatically updated when data is added to the workbook. This type allows for a broad range of sophisticated graphs.
- Template project: Some graphs are too complex to turn into graph templates. For example, when a graph is comprised of data from multiple workbooks. In this case, an Origin project (OPJU file) can serve as a template.
I’ll assume that you already know the basics of creating an Origin graph. But if you are unfamiliar with saving user-defined graph templates, watch the video presented at the end of this post.
Update the originpro Python package
We’ll use the originpro Python package, so the first step is to install or update the package to the latest version.
If “external Python” (not the version embedded in Origin) is being used, run pip to install or update the package:
pip install originpro
pip install --upgrade originpro
If “embedded” Python is being used, update the originpro package using the Python Package Manager accessible from the Connectivity menu in Origin. See below, but note that the latest version may be higher than the one illustrated:
Download the Companion Files for This Post
Click here to download a zip archive of the companion files for this blog post. Decompress it and save the folder to the location of your choosing. The files can be anywhere- not necessarily in your Origin User Files folder. Note that the files should all be kept together- no need to move them around individually.
The folder contains three Python scripts:
- gt_examples.py: contains example code for using a variety of pre-made graph templates included in the companion folder.
- tp_example.py: contains example code for using a template project. The OPJU file is included in the companion folder.
- realtime_monitor.py: This bonus script shows you how you could implement an “in real time” updating graph (from template) to monitor some type of long-running process. Optionally install the pygetwindow package for an enhanced experience.
If using external Python, the scripts can be loaded and run using the Python editor of you choice. If using embedded Python, the scripts can be loaded and run from Origin Code Builder.
Illustrations of Using Each Type of Template
The following are examples of how to use each type of graph template. These examples are for illustrative purposes and are not runnable. Runnable examples are provided in gt_examples.py in the companion files folder.
First, there are rules for the location from which to load graph templates:
- File name only: Origin looks first in the User Files folder, then the folder containing the Origin EXE.
- Full path and file name: The specified file is loaded from that location.
Basic Graph Template
# Double-Y plot wks = op.new_book('w', hidden = True) wks.from_file(working_folder + 'data\\xyyy.csv', False) # Load mock data just for example tmpl = working_folder + 'templates\\doubley_template.otpu' gr = op.new_graph(template = tmpl) lay = gr lay.add_plot(wks, 1, 0, type = 230) # type is template-defined, color is template-defined lay.rescale('x') # Don't rescale X axis, we'll manually rescale it below lay = gr lay.add_plot(wks, 2, 0, type = 230) # type is template-defined, color is template-defined lay.rescale('x') lay.xlim = (0, 1.01, 0.10) # X axis from, to, major ticks increment
wks = op.new_book('w', hidden = True) wks.from_file(working_folder + 'data\\cloneable.csv', False) # Load mock data just for example tmpl = working_folder + 'templates\\cloneable_template.otpu' wks.plot_cloneable(tmpl)
Embedded Graph Template
tmpl = working_folder + 'templates\\embedded_template.ogwu' bk = op.load_book(tmpl) wks = bk for i in range(9): list = [random.gauss(50, 5) for j in range(16)] wks.from_list(int(i), list) bk.unembed_sheet(1) # Separates graph from workbook, activates graph op.find_graph().rescale() # Rescales active graph bk.show = False
This one is somewhat different than the other types of graph templates previously discussed. In this case, a complete Origin project (OPJU file) is loaded. It contains both a graph and the associated workbook(s). The key is to populate the workbook(s) which then automatically populates the graph.
This example is for illustrative purposes and is not runnable. A runnable example is provided in tp_example.py in the companion files folder.
# Load the template project. op.open(working_folder + 'templates\\template.opju') # Generate some mock data to insert into Origin # These functions are defined in tp_example.py x = np.arange(start = 0, stop = 61, step = 1) y_1 = logistic(x, 10, 100, 20, 1) y_2 = logistic(x, 0,90, 40, 1) y_3 = exp_decay(x, 0, 100, 1, 5) y_4 = exp_decay(x, 0, 130, 2, 7) # Get 1st sheets from Book1 and Book2 wks_1 = op.find_book(type = 'w', name = 'Book1') wks_2 = op.find_book(type = 'w', name = 'Book2') # Insert generated data into worksheets wks_1.from_list(col = 0, data = x.tolist()) wks_1.from_list(1, y_1.tolist(), comments = 'Experiment 1') wks_1.from_list(2, y_2.tolist(), comments = 'Experiment 2') wks_2.from_list(col = 0, data = x.tolist()) wks_2.from_list(1, y_3.tolist(), comments = 'Experiment 1') wks_2.from_list(2, y_4.tolist(), comments = 'Experiment 2') # Now, graph automatically gets updated along with axes
That’s how simple it is to use graph templates!
Thanks for taking the time to read this post. I believe that it demonstrates how useful Origin graph templates are for your Python projects.