2.9.1 Creating Graphs

Creating graphs is probably the most commonly performed operation in Origin. Origin provides a collection of X-Function and LabTalk functions for this purpose. You can find all X-Functions used for plotting under the Plotting category and can list them by typing the following command:

lx cat:="plotting";

Some X-Functions are general tools to plot graph from a specific kinds of data, for example plotxy to plot graphs from XY range data, and plotm to plot graph from matrix data. Some are used to plot a special plot type, for example plotgboxraw to plot a grouped box plot from raw data, and plotpiper to create a piper plot. Please refer to plotting category for details of each X-Function.

The following sections give examples of two X-Functions that allow you to create graphs directly from LabTalk scripts: plotxy and plotgroup. Once a plot is created, you can use object properties, like page, layer, axis objects, and set command to format the graph.

Creating a Graph with the PLOTXY X-Function

plotxy is an X-Function used for general purpose plotting. It is used to create a new graph window, plot into a graph template, or plot into a new graph layer. It has a syntax common to all X-Functions:

plotxy option1:=optionValue option2:=optionValue ... optionN:=optionValue

All possible options and values are summarized in the X-Function help for plotxy. Since it is somewhat non-intuitive, the plot option and its most common values are summarized here:

plot:= Plot Type
200 Line
201 Scatter
202 Line+symbol
203 column

All of the possible values for the plot option can be found in the Plot Type IDs.

Plotting X Y data

Input XYRange referencing the X and Y

The following example plots the first two columns of data in the active worksheet, where the first column will be plotted as X and the second column as Y, as a line plot.

plotxy iy:=(1,2) plot:=200;

Input XYRange referencing just the Y

The following example plots the second column of data in the active worksheet, as Y against its associated X, as a line plot. When you do not explicitly specify the X, Origin will use the the X-column that is associated with that Y-column in the worksheet, or if there is no associated X-column, then an <auto> X will be used. By default, <auto> X is row number.

plotxy iy:=2 plot:=200;

Plotting X YY data

The following example plots the first three columns of data from Book1, Sheet1, where the first column will be plotted as X and the second and third columns as Y, as a grouped scatter plot.

plotxy iy:=[Book1]Sheet1!(1,2:3) plot:=201;

Plotting XY XY data

The following example plots the first four columns of data in the active worksheet, where the first column will be plotted as X against the second column as Y and the third column as X against the fourth column as Y, as a grouped line+symbol plot.

plotxy iy:=((1,2),(3,4)) plot:=202;

Plotting using worksheet column designations

The following example plots all columns in the active worksheet, using the worksheet column plotting designations, as a column plot. '?' indicates to use the worksheet designations; '1:end' indicates to plot all the columns.

plotxy iy:=(?,1:end) plot:=203;

Plotting a subset of a column

The following example plots rows 1-12 of all columns in the active worksheet, as a grouped line plot.

plotxy iy:=(1,2:end)[1:12] plot:=200;

Note: Please refer to Specifying Subrange Using X Values for more details on subset.

Plotting into a graph template

The following example plots the first column as theta(X) and the second column as r(Y) in the active worksheet, into the polar plot graph template, and the graph window is named MyPolarGraph.

plotxy (1,2) plot:=192 ogl:=[<new template:=polar name:=MyPolarGraph>];

Plotting into an existing graph layer

The following example plots columns 10-20 in the active worksheet, using column plotting designations, into the second layer of Graph1. These columns can all be Y columns and they will still plot against the associated X column in the worksheet.

plotxy iy:=(?,10:20) ogl:=[Graph1]2!;

Creating a new graph layer

The following example adds a new Bottom-X Left-Y layer to the active graph window, plotting the first column as X and the third column as Y from Book1, Sheet2, as a line plot. When a graph window is active and the output graph layer is not specified, a new layer is created.

plotxy iy:=[Book1]Sheet2!(1,3) plot:=200;

Creating a Double-Y Graph

// Import data file
string fpath$ = "Samples\Import and Export\S15-125-03.dat";
string fname$ = system.path.program$ + fpath$;

// Remember Book and Sheet names
string bkname$ = page.name$;
string shname$ = layer.name$;

// Plot the first and second columns as X and Y 
// The worksheet is active, so can just specify column range
plotxy iy:=(1,2) plot:=202 ogl:=[<new template:=doubleY>];

// Plot the first and third columns as X and Y into the second layer
// Now that the graph window is the active window, need to specify Book 
//and Sheet
plotxy iy:=[bkname$]shname$!(1,3) plot:=202 ogl:=2;

Create Graph Groups with the PLOTGROUP X-Function

According to the grouping variables (datasets), plotgroup X-Function creates grouped plots for page, layer or dataplot. To work properly, the worksheet should be sorted by the graph group data first, then the layer group data and finally the dataplot group data.

This example shows how to plot by group.

// Establish a path to the sample data
fn$ = system.path.program$ + "Samples\Statistics\body.dat";
impASC fn$;      // Import into new workbook

// Sort worksheet--Sorting is very important!
wsort bycol:=3;

// Plot by group
plotgroup iy:=(4,5) pgrp:=Col(3);

This next example creates graph windows based on one group and graph layers based on a second group:

// Bring in Sample data
fn$ = system.path.program$ + "Samples\Graphing\Categorical Data.dat";
impASC fn$;
// Sort
dataset sortcol = {4,3}; // sort by drug, then gender
dataset sortord = {1,1}; // both ascending sort
wsort nest:=sortcol ord:=sortord;
// Plot each drug in a separate graph with gender separated by layer
plotgroup iy:=(2,1) pgrp:=col(drug) lgrp:=col(gender);

Note : Each group variable is optional. For example, you could use one group variable to organize data into layers by omitting Page Group and Data Group. The same sort order is important for whichever options you do use.

Create 3D Graphs with Worksheet -p Command

To create 3D Graphs, use the Worksheet (command) (-p switch).

First, create a simple 3D scatter plot:

// Create a new book
newbook r:=bkn$;
// Run script on bkn$
win -o bkn$ {
	// Import sample data
	string fname$ = system.path.program$ +
		"\samples\Matrix Conversion and Gridding" +
		"\XYZ Random Gaussian.dat";
	// Save new book name
	bkn$ = %H;
	// Change column type to Z
	wks.col3.type = 6;
	// Select column 3
	worksheet -s 3;
	// Plot a 3D scatter graph by template named "3d"
	worksheet -p 240 3d;

You can also create 3D color map or 3D mesh graph. 3D graphs can be plotted either from worksheet or matrix. And you may need to do gridding before plotting.

We can run the following script after above example and create a 3D wire frame plot from matrix:

win -o bkn$ {
	// Gridding by Shepard method
	xyz_shep 3;
	// Plot 3D wire frame graph;
	worksheet -p 242 wirefrm;

Create 3D Graph and Contour Graphs from Virtual Matrix

Origin can also create 3D graphs, such as 3D color map, contour, or 3D mesh, etc., from worksheet by the plotvm X-Function. This function creates a virtual matrix, and then plot from such matrix. For example:

// Create a new workbook and import sample data
string fname$=system.path.program$ + "Samples\Graphing\VSurface 1.dat";
// Treat entire sheet as a Virtual Matrix and create a colormap surface plot
plotvm irng:=1! format:=xacross rowpos:=selrow1 colpos:=selcol1
 ztitle:="VSurface 1" type:=242 ogl:=<new template:=cmap>;
// Change X axis scale to log
// Nonlinear axis type supported for 3D graphs created from virtual matrix

Create Circular Dendrogram

Creating Circular Dendrogram menu is not support the Origin GUI. If you want to plot this graph type, you can do Hierarchical Cluster Analysis to create the graph, or use the following script:

run.section(plot, CircularPhyTree); 

run.section(plot, CircularBinPhyTree);

This sample shows how to plot this kind of graph:

// Import data file
string fpath$ = "Samples\Graphing\US Mean Temperature.dat";
string fname$ = system.path.program$ + fpath$;
// Hierarchical Cluster Analysis
hcluster -r 2 irng:=[USMeanTempera]"US Mean Temperature"!D"January"[1]:O"December"[100] link:=ward number:=4 center:=1;
// Highlight datasets
page.active$ = "Cluster Plot Data1" ;
worksheet -s 1 0 3 0;
// Create graph
run.section(plot, CircularPhyTree);

Installing and Uninstalling a Graph Template Via LabTalk

You can use LabTalk to install or uninstall a graph template using the following commands ( the graph template file must exist in your User Files Folder). When uninstalling, the file is not moved from your User Files Folder.

// To install
run.section(dofile.ogs, OnInstallTemplate, "%YMyTemplate.otpu");

// To uninstall
run.section(dofile.ogs, OnUnInstallTemplate, "%YMyTemplate.otpu");