2.9.1 Creating GraphsCreatingGraphs
Plot GraphCreate Graph
Creating graphs is probably the most commonly performed operation in Origin. Origin provides a collection of XFunction and LabTalk functions for this purpose. You can find all XFunctions used for plotting under the Plotting category and can list them by typing the following command:
lx cat:="plotting";
Some XFunctions 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 categroy for details of each XFunction.
The following sections give examples of two XFunctions 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 format the graph.
Creating a Graph with the PLOTXY XFunction
plotxy is an XFunction 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 XFunctions:
plotxy option1:=optionValue option2:=optionValue ... optionN:=optionValue
All possible options and values are summarized in the XFunction help for plotxy. Since it is somewhat nonintuitive, 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 Xcolumn that is associated with that Ycolumn in the worksheet, or if there is no associated Xcolumn, 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 112 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 1020 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 layerCreate LayerGraph Layer
The following example adds a new BottomX LeftY 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 DoubleY GraphDoubleY Graph
// Import data file
string fpath$ = "Samples\Import and Export\S1512503.dat";
string fname$ = system.path.program$ + fpath$;
impASC;
// 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 XFunctionGraph Groups
According to the grouping variables (datasets), plotgroup XFunction 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";
newbook;
impASC fn$; // Import into new workbook
// Sort worksheetSorting 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";
newbook;
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 CommandGraph, 3D
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";
impasc;
// 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 MatrixVirtual Matrix
Origin can also create 3D graphs, such as 3D color map, contour, or 3D mesh, etc., from worksheet by the plotvm XFunction. This function creates a virtual matrix, and then plot from such matrix. For example:
// Create a new workbook and import sample data
newbook;
string fname$=system.path.program$ + "Samples\Graphing\VSurface 1.dat";
impasc;
// 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
LAYER.X.type=2;
Installing and Uninstalling a Graph Template Via LabTalkInstalling Graph Template
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");
