Origin Graph Templates for Python Developers

はじめに

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.

Getting Started

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:

or

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

Cloneable Template

Embedded Graph Template

Template Project

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.

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.

Addendum: Saving Graph Templates Video

About Chris Drozdowski

Chris Drozdowski is a Product Support Engineer at OriginLab. He loves to talk to customers and educate them. He particularly relishes diagnosing and solving difficult, edge-case issues. As well, he contributes code to help solve problems or enhance user experience. In his down time at work, he likes to research and write about esoteric product features. Outside of work, he enjoys spending time with his family, having fun with C++, working on his aquarium, and exploring craft beers.

View all posts by Chris Drozdowski →

5 Comments on “Origin Graph Templates for Python Developers”

  1. Thank you Chris for the tutorial on the convenience of utilizing graph templates programmatically via Python. On running the gt_examples.py script, I get the following error

    ——————————————————
    Traceback (most recent call last):
    File “”, line 6, in
    File “C:\Users\fubar\OneDrive\Desktop\OneDriveBackUp\OneDrive – Indian Institute of Science\Documents\OriginLab\User Files\PythonInOrigin\py_graph_templates\gt_examples.py”, line 77, in
    wks.plot_cloneable(tmpl)
    AttributeError: ‘WSheet’ object has no attribute ‘plot_cloneable’
    ——————————————————

    How to resolve the error? I am using 9.8.0.200 (Learning Edition) version of OriginPro 2021 (64-bit)

    1. Hi,

      You need to update the originpro package as I mentioned in the blog post.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です