Python

This module provides bindings to the Python programming language. Basic usage in the context of QtiPlot will be discussed below, but for more in-depth information on the language itself, please refer to its excellent documentation.

The Initialization File

This file allows you to customize the Python environment, import modules and define functions and classes that will be available in all of your projects. The default initialization file shipped with QtiPlot imports Python's standard math functions as well as special functions from SciPy (if available). Also, it creates some handy shortcuts, like table("table1") for qti.app.table("table1").

When activating Python support, QtiPlot searches the initialization file in a default folder, which can be customized via the File Locations tab of the Preferences dialog. If the initialization file is not found, QtiPlot will issue a warning and muParser will be kept as the default interpreter.

Files ending in .pyc are compiled versions of the .py source files and therefore load a bit faster. The compiled version will be used if the source file is older or nonexistent. Otherwise, QtiPlot will try to compile the source file (if you've got write permissions for the output file).

Python Basics

Mathematical expressions work largely as expected. However, there's one caveat, especially when switching from muParser (which has been used exclusively in previous versions of QtiPlot): a^b does not mean "raise a to the power of b" but rather "bitwise exclusive or of a and b"; Python's power operator is **. Thus:


2^3 # read: 10 xor 11 = 01
#> 1
2**3
#> 8

One thing you have to know when working with Python is that indentation is very important. It is used for grouping (most other languages use either braces or keywords like do...end for this). For example,


x=23
for i in (1,4,5):
	x=i**2
	print(x)
	
will do what you would expect: it prints out the numbers 1, 16 and 25; each on a line of its own. Deleting just a bit of space will change the functionality of your program:

x=23
for i in (1,4,5):
	x=i**2
print(x)
	
will print out only one number - no, not 23, but rather 25. This example was designed to also teach you something about variable scoping: There are no block-local variables in Python.

There are two different variable scopes to be aware of: local and global variables. Unless specified otherwise, variables are local to the context in which they were defined. Thus, the variable x can have three different values in, say, two different Note windows and a column formula. Global variables on the other hand can be accessed from everywhere within your project. A variable x is declared global by executing the statement global x. You have to do this before assigning a value to x, but you have to do it only once within the project (no need to "import" the variable before using it). Note that there is a slight twist to these rules when you define your own functions.

Defining Functions and Control Flow

The basic syntax for defining a function (for use within one particular note, for example) is


def answer():
	return 42
	
If you want your function to be accessible from the rest of your project, you have to declare it global before the definition:

global answer
def answer():
	return 42
	
You can add your own function to QtiPlot's function list. We'll also provide a documentation string that will show up, for example, in the "set column values" dialog:

global answer
def answer():
	"Return the answer to the ultimate question about life, the universe and everything."
	return 42
qti.mathFunctions["answer"] = answer
	
If you want to remove a function from the list, do:

del qti.mathFunctions["answer"]
	

Note that functions have their own local scope. That means that if you enter a function definition in a Note, you will not be able to access (neither reading nor writing) Note-local variables from within the function. However, you can access global variables as usual.

If-then-else decisions are entered as follows:


if x>23:
	print(x)
else:
	print("The value is too small.")
	

You can do loops, too:


for i in range(1, 11):
	print(i)
	
This will print out the numbers between 1 and 10 inclusively (the upper limit does not belong to the range, while the lower limit does).

Mathematical Functions

Python comes with some basic mathematical functions that are automatically imported (if you use the initialization file shipped with QtiPlot). Along with them, the constants e (Euler's number) and pi (the one and only) are defined.

Table 7-4. Supported Mathematical Functions

NameDescription
acos(x)inverse cosinus
asin(x)inverse sinus
atan(x)inverse tangent
atan2(y,x)equivalent to atan(y/x), but more efficient
ceil(x)ceiling; smallest integer greater or equal to x
cos(x)cosinus of x
cosh(x)hyperbolic cosinus of x
degrees(x)convert angle from radians to degrees
exp(x)Exponential function: e raised to the power of x.
fabs(x)absolute value of x
floor(x)largest integer smaller or equal to x
fmod(x,y)remainder of integer division x/y
frexp(x)Returns the tuple (mantissa,exponent) such that x=mantissa*(2**exponent) where exponent is an integer and 0.5 <=abs(m)<1.0
hypot(x,y)equivalent to sqrt(x*x+y*y)
ldexp(x,y)equivalent to x*(2**y)
log(x)natural (base e) logarythm of x
log10(x)decimal (base 10) logarythm of x
modf(x)return fractional and integer part of x as a tuple
pow(x,y)x to the power of y; equivalent to x**y
radians(x)convert angle from degrees to radians
sin(x)sinus of x
sinh(x)hyperblic sinus of x
sqrt(x)square root of x
tan(x)tangent of x
tanh(x)hyperbolic tangent of x

Accessing QtiPlot's objects from Python

We will assume that you are using the initialization file shipped with QtiPlot. Accessing the objects in your project is straight-forward,


t = table("Table1")
m = matrix("Matrix1")
g = graph("Graph1")
p = plot3D("Graph2")
n = note("Notes1")
# get a pointer to the QTextEdit object used to display information in the results log window:
log = resultsLog()
	  
as is creating new objects:

# create an empty table named "tony" with 5 rows and 2 columns:
t = newTable("tony", 5, 2)
# use defaults
t = newTable()
# create an empty matrix named "gina" with 42 rows and 23 columns:
m = newMatrix("gina", 42, 23)
# use defaults
m = newMatrix()
# create an empty graph window
g = newGraph()
# create a graph window named "test" with two layers disposed on a 2 rows x 1 column grid
g = newGraph("test", 2, 2, 1)
# create an empty 3D plot window with default title
p = newPlot3D()
# create an empty note named "momo"
n = newNote("momo")
# use defaults
n = newNote()
The currently selected Table/Matrix etc. can be accessed with the follwing commands:

t = currentTable()
m = currentMatrix()
g = currentGraph()
n = currentNote()
The functions will only return a valid object if a window of the wanted type is actually selected. You can check if the object is valid with a simple if clause:
if isinstance(t,qti.Table): print "t is a table"

Every piece of code is executed in the context of an object which you can access via the self variable. For example, entering self.cell("t",i) as a column formula is equivalent to the convenience function col("t").

Once you have established contact with a MDI window, you can modify some of its properties, like the name, the window label, the geometry, etc.. For example, here's how to rename a window, change its label and the way they are displayed in the window title bar, the so called caption policy:

t = table("Table1")
setWindowName(t, "toto")
t.setWindowLabel("tutu")
t.setCaptionPolicy(MDIWindow.Both)
The caption policy can have one of the following values:

  1. Name

    the window caption is determined by the window name

  2. Label

    the caption is detemined by the window label

  3. Both

    caption = "name - label"

For a fast editing process, you can create template files from existing tables, matrices or plots. The templates can be used later on in order to create customized windows very easily:

saveAsTemplate(graph("Graph1"), "my_plot.qpt")
g = openTemplate("my_plot.qpt")
Also, you can easily clone a MDI window:

g1 = clone(graph("Graph1"))
If you want to delete a project window, you can use the close()method. You might want to deactivate the confirmation message, first:

w.confirmClose(False)
w.close()
All QtiPlot subwindows are displayed in a QMdiArea. You can get a pointer to this object via the workspace()method. This can be particularly usefull if you need to customize the behavior of the workspace via your scripts. Here follows a small example script that pops-up a message displaying the name of the active MDI subwindow each time a new window is activated:

def showMessage():
	QtGui.QMessageBox.about(qti.app, "", workspace().activeSubWindow().objectName())

QtCore.QObject.connect(workspace(), QtCore.SIGNAL("subWindowActivated(QMdiSubWindow *)"), showMessage)

Project Folders

Storing your data tables/matrices and your plots in folders can be very convenient and helpful when you're analysing loads of data files in the same project. New objects will always be added to the active folder. You can get a pointer to it via:

f = activeFolder()
The functions table, matrix, graph and note will start searching in the active folder and, failing this, will continue with a depth-first recursive search of the project's root folder, given by:

f = rootFolder()
In order to access subfolders and windows, there are the following functions:

f2 = f.folders()[number]
f2 = f.folder(name, caseSensitive=True, partialMatch=False)
t = f.table(name, recursive=False)
m = f.matrix(name, recursive=False)
g = f.graph(name, recursive=False)
n = f.note(name, recursive=False)
If you supply True for the recursive argument, a depth-first recursive search of all subfolders will be performed and the first match returned.

New folders can be created using:


newFolder = addFolder("New Folder", parentFolder = 0)
If the parentFolderis not specified, the new folder will be added as a subfolder of the project's root folder. When you create a new folder via a Python script, it doesn't automatically become the active folder of the project. You have to set this programatically, using:

changeFolder(newFolder, bool force=False)
Folders can be deleted using:

deleteFolder(folder)
You can save a folder as a project file, and of course, you can also save the whole project:

saveFolder(folder, "new_file.qti", compress=False)
saveProjectAs("new_file_2.qti", compress=False)
If compressis set to True, the project file will be archived to the .gz format, using zlib.

Also, you can load a QtiPlot or an Origin project file into a new folder. The new folder will have the base name of the project file and will be added as a subfolder to the parentFolder or to the current folder if no parent folder is specified.


newFolder = appendProject("projectName", parentFolder = 0)
If you don't want to be asked for confirmation when a table/matrix is renamed during this operation, or when deleting a folder via a Python script, you must change your preferences concerning prompting of warning messages, using the Preferences dialog ("Confirmations" tab).

Folders store their own log information containing the results of the analysis operations performed on the child windows. This information is updated in the result log window each time you change the active folder in the project. You can access and manipulate these log strings via the following functions:


text = folder.logInfo()
folder.appendLogInfo("Hello!")
folder.clearLogInfo()

Working with Tables

We'll assume that you have assigned some table to the variable t. You can access its numeric cell values with

t.cell(col, row)
# and
t.setCell(col, row, value)
Whenever you have to specify a column, you can use either the column name (as a string) or the consecutive column number (starting with 1). Row numbers also start with 1, just as they are displayed. If you want to work with arbitrary texts or the textual representations of numeric values, you can use:

t.text(col, row)
# and
t.setText(col, row, string)
The number of columns and rows is accessed via:

t.numRows()
t.numCols()
t.setNumRows(number)
t.setNumCols(number)
You can add a new column at the end of the table or you can insert new columns before a startColumnusing the functions bellow:

t.addColumn()
t.insertColumns(startColumn, count)
It is also possible to swap two columns using:

t.swapColumns(column1, column2)
You can delete a colum or a range of rows using the functions bellow:

t.removeCol(number)
t.deleteRows(startRowNumber, endRowNumber)
Column names can be read and written with:

t.colName(number)
t.setColName(col, newName, enumerateRight=False)
If enumerateRightis set to True, all the table columns starting from index colwill have their names modified to a combination of the newNameand a numerical increasing index. If this parameter is not specified, by default it is set to False.

You can change the plot role of a table column (abscissae, ordinates, error bars, etc...) using:


t.setColumnRole(col, role)
where rolespecifies the desired column role:

0.

Table.None

1.

Table.X

2.

Table.Y

3.

Table.Z

4.

Table.xErr

5.

Table.yErr

6.

Table.Label

You can normalize a single column or all columns in a table:


t.normalize(col)
t.normalize()
Sort a single or all columns:

t.sortColumn(col, order = 0)
t.sort(type = 0, order = 0, leadingColumnName)
Import values from file, using sepas separator char, ignoring ignoreLineslines at the head of the file and all lines starting with a commentstring.

t.importASCII(file, sep="\t",ignoreLines=0,renameCols=False,stripSpaces=True,simplifySpace=False,
importComments=False,comment="#",readOnly=False,importAs=Table.Overwrite,endLine=0,maxRows=-1)
As you see from the above list of import options, you have the possibility to set the new columns as read-only. This will prevent the imported data from beeing modified. You have the possibility to remove this protection at any time, by using:

t.setReadOnlyColumn(col, False)

The importAs flag can have the following values:

0.

Table.NewColumns: data values are added as new columns.

1.

Table.NewRows: data values are added as new rows.

2.

Table.Overwrite: all existing values are overwritten (default value).

The endLine flag specifies the end line character convention used in the ascii file. Possible values are: 0 for line feed (LF), which is the default value, 1 for carriage return + line feed (CRLF) and 2 for carriage return only (usually on Mac computers).

The last parameter maxRows allows you to specify a maximum number of imported lines. Negative values mean that all data lines must be imported.

If the decimal separator of the imported file does not match the currently used conventions, you have to adjust them before using the table:


t.setDecimalSeparators(country,ignoreGroupSeparator=True)

Where country can have one of the following values:

0.

Use the system value

1.

Use the following format: 1,000.00

2.

Use the following format: 1.000,00

3.

Use the following format: 1 000,00

You can export values from a table to an ASCII file, using sep as separator chararacter. The ColumnLabels option allows you to export or ignore the column labels, ColumnComments does the same for the comments displayed in the table header and the SelectionOnly option makes possible to export only the selected cells of the table.


t.exportASCII(file,sep="\t",ignore=0,ColumnLabels=False,ColumnComments=False,SelectionOnly=False)
Other settings that you can modify are the text displayed as a comment in the header of a column or the expression used to calculate the column values. Please beware that changing the command doesn't automatically update the values of the column!

t.setComment(col, newComment)
t.setCommand(col, newExpression)
You can access the column comments and enable/disable their display via the following functions:

t.comment(col)
t.showComments(on = True)
You can also modify the width of a column (in pixels) or hide/show table columns:

t.setColumnWidth(col, width)
t.hideColumn(col, True)
If one or several tabel columns are hidden you can make them visible again using:

t.showAllColumns()
After having changed some table values from a script, you will likely want to update dependent Graphs:

t.notifyChanges()
As a simple example, let's set some column values without using the dialog.

t = table("table1")
for i in range(1, t.numRows()+1):
	t.setCell(1, i, i**2)
t.notifyChanges()
You can check if a column or row of a table is selected by using the following functions:

t.isColSelected(col)
t.isRowSelected(row)

Working with Matrices

Matrix objects have a dual view mode: either as images or as data tables. Assuming that you have assigned some matrix to the variable m, you can change its display mode via the following function:

m.setViewType(Matrix.TableView)
m.setViewType(Matrix.ImageView)
If a matrix is viewed as an image, you have the choice to display it either as gray scale or using a predefined color map:

m.setGrayScale()
m.setRainbowColorMap()
m.setDefaultColorMap() # default color map defined via the 3D plots tab of the preferences dialog
You can also define custom color maps:

map = LinearColorMap(QtCore.Qt.yellow, QtCore.Qt.blue)
map.setMode(LinearColorMap.FixedColors) # default mode is LinearColorMap.ScaledColors
map.addColorStop(0.2, QtCore.Qt.magenta)
map.addColorStop(0.7, QtCore.Qt.cyan)
m.setColorMap(map)
You have direct access to the color map used for a matrix via the following functions:

map = m.colorMap()
from = map.color1()
to = map.color2()
Accessing cell values is very similar to Table, but since Matrix doesn't use column logic, row arguments are specified before columns and obviously you can't use column name.

m.cell(row, col)
m.setCell(row, col, value)
m.text(row, col)
m.setText(row, col, string)
An alternative solution to assign values to a Matrix, would be to define a formula and to calculate the values using this formula, like in the following example:

m.setFormula("x*y*sin(x*y)")
m.calculate()
You can also specify a column/row range in the calculate() function, like this:

m.calculate(startRow, endRow, startColumn, endColumn)
Before setting the values in a matrix you might want to define the numeric precision, that is the number of significant digits used for the computations:

m.setNumericPrecision(prec)
Also, like with tables, you can access the number of rows/columns in a matrix:

rows = m.numRows()
columns = m.numCols()
Matrix objects allow you to define a system of x/y coordinates that will be used when plotting color/contour maps or 3D height maps. You can manipulate these coordinates using the following functions:

xs = m.xStart()
xe = m.xEnd()
ys = m.yStart()
ye = m.yEnd()
m.setCoordinates(xs + 2.5, xe, ys - 1, ye + 1)
The horizontal and vertical headers of a matrix can display either the x/y coordinates or the column/row indexes:

m.setHeaderViewType(Matrix.ColumnRow)
m.setHeaderViewType(Matrix.XY)
There are several built-in transformations that you can apply to a matrix object. You can transpose or invert a matrix and calculate its determinant, provided, of course, that the conditions on the matrix dimensions, required by these operations, are matched:

m.transpose()
m.invert()
d = m.determinant()
Some other operations, very useful when working with images, like 90 degrees rotations and mirroring, can also be performed. By default rotations are performed clockwise. For a counterclockwise rotation you must set the clockwiseparameter to False.

m.flipVertically()
m.flipHorizontally()
m.rotate90(clockwise = True)
Please note that sometimes, after a change in the matrix settings, you need to use the following function in order to update the display:

m.resetView()
If you need to get data from a table, in order to use it in a matrix (or vice-versa), you can avoid time consuming copy/paste operations and speed up the whole proces by simply converting the table into a matrix:

m = tableToMatrix(table("Table1"))
t = matrixToTable(m)
Also, it's worth knowing that you can easily import image files to matrices, that can be used afterwards for plotting (see the next section for more details about 2D plots):

m1 = importImage("C:/poze/adi/PIC00074.jpg")
m2 = newMatrix()
m2.importImage("C:/poze/adi/PIC00075.jpg")
The algorithm used to import the image returns a gray value between 0 and 255 from the (r, g, b) triplet corresponding to each pixel. The gray value is calculated using the formula: (r * 11 + g * 16 + b * 5)/32

For custom image analysis operations, you can get a copy of the matrix image view, as a QImage object, via:


image = m.image()
You can export matrices to all raster image formats supported by Qt or to any of the following vectorial image format: EPS, PS, PDF or SVG using:

m.export(fileName)
This is a shortcut function which uses some default parameters in order to generate the output image. If you need more control over the export parameters you must use one of the following functions:

m1.exportRasterImage(fileName, quality = 100, dpi = 0)
m2.exportVector(fileName, resolution, color = True)
, where the qualityparameter influences the size of the output file. The higher this value (maximum is 100), the higher the qualitity of the image, but the larger the size of the resulting files. The dpiparameter represents the export resolution in pixels per inch (the default is screen resolution).

You can also import an ASCII data file, using sep as separator characters, ignoring ignore lines at the head of the file and all lines starting with a comment string:


m.importASCII(file, sep="\t", ignore=0, stripSpaces=True, simplifySpace=False, comment="#",
				importAs=Matrix.Overwrite, locale=QLocale(), endLine=0, maxRows=-1)

The importAs flag can have the following values:

0.

Matrix.NewColumns: data values are added as new columns.

1.

Matrix.NewRows: data values are added as new rows.

2.

Matrix.Overwrite: all existing values are overwritten (default value).

The locale parameter can be used to specify the convention for decimal separators used in your ASCII file.

The endLine flag specifies the end line character convention used in the ascii file. Possible values are: 0 for line feed (LF), which is the default value, 1 for carriage return + line feed (CRLF) and 2 for carriage return only (usually on Mac computers).

The last parameter maxRows allows you to specify a maximum number of imported lines. Negative values mean that all data lines must be imported.

Also, you can export values from a matrix to an ASCII file, using sep as separator chararacters. The SelectionOnly option makes possible to export only the selected cells of the matrix.


m.exportASCII(file, sep="\t", SelectionOnly=False)

Stem Plots

A stemplot (or stem-and-leaf plot), in statistics, is a device for presenting quantitative data in a graphical format, similar to a histogram, to assist in visualizing the shape of a distribution. A basic stemplot contains two columns separated by a vertical line. The left column contains the stems and the right column contains the leaves. See Wikipedia for more details.

QtiPlot provides a text representation of a stemplot. The following function returns a string of characters reprezenting the statistical analysis of the data:


text = stemPlot(Table *t, columnName, power = 1001, startRow = 0, endRow = -1)
where the power variable is used to specify the stem unit as a power of 10. If this parameter is greater than 1000 (the default behavior), than QtiPlot will try to guess the stem unit from the input data and will pop-up a dialog asking you to confirm the automatically detected stem unit.

Once you have created the string representation of the stemplot, you can display it in any text editor you like: in a note within the project or even in the results log:


resultsLog().append(stemPlot(table("Table1"), "Table1_2", 1, 2, 15))

2D Plots

As you have seen in the previous section, it is possible create 2D plots from matrices. Here's how you can do it in practice:

m = importImage("C:/poze/adi/PIC00074.jpg")
g1 = plot(m, Layer.ColorMap)
g2 = plot(m, Layer.Contour)
g3 = plot(m, Layer.GrayScale)
If you want to create a new Graph window for some data in table Table1, you can use the plot command:

t = table("Table1")
g = plot(t, column, type)
typespecifies the desired plot type and can be one of the following numbers or the equivalent reserved word:

0

Layer.Line

1

Layer.Scatter

2

Layer.LineSymbols

3

Layer.VerticalBars

4

Layer.Area

5

Layer.Pie

6

Layer.VerticalDropLines

7

Layer.Spline

8

Layer.HorizontalSteps

9

Layer.Histogram

10

Layer.HorizontalBars

13

Layer.Box

15

Layer.VerticalSteps

You can plot more than one column at once by giving a Python tuple (see the Python Tutorial) as an argument:

g1 = plot(table("Table1"), (2,4,7), 2)
g2 = plot(table("Table1"), ("Table1_2","Table1_3"), Layer.LineSymbols)
All the curves in a plot layer can be customized in terms of color, line width and line style. Here's a short script showing the corresponding functions at work:

t = newTable("test", 30, 4)
for i in range(1, t.numRows()+1):
	t.setCell(1, i, i)
	t.setCell(2, i, i)
	t.setCell(3, i, i+2)
	t.setCell(4, i, i+4)

l = plot(t, (2,3,4), Layer.Line).activeLayer() # plot columns 2, 3 and 4
for i in range(0, l.numCurves()):
	l.setCurveLineColor(i, 1 + i) #curve color is defined as an integer value
	l.setCurveLineWidth(i, 0.5 + 2*i)

l.setCurveLineStyle(1, QtCore.Qt.DotLine)
l.setCurveLineStyle(2, QtCore.Qt.DashLine)
You can also create a vector plot by giving four columns in a Python tuple as an argument and the plot type as Layer.VectXYXY (11) or Layer.VectXYAM (14), depending on how you want to define the end point of your vectors: using (X, Y) coordinates or (Angle, Magnitude) coordinates.

g = plot(table("Table1"), (2,3,4,5), Layer.VectXYXY)
If you want to add a curve to an existing Graph window, you have to choose the destination layer. Usually,

l = g.activeLayer()
will do the trick, but you can also select a layer by its number:

l = g.layer(num)

Working with curves

You can then add or remove curves to or from this layer:

l.insertCurve(table, Xcolumn, Ycolumn, type=Layer.Scatter)
l.addCurve(table, column, type=Layer.Line, lineWidth = 1, symbolSize = 3, startRow = 0, endRow = -1)
l.addCurves(table, (2,4), type=Layer.Line, lineWidth = 1, symbolSize = 3, startRow = 0, endRow = -1)
l.removeCurve(curveName)
l.removeCurve(curveNumber)
l.deleteFitCurves()
Use the following function to change the axis attachment of a curve:

l.setCurveAxes(number, x-axis, y-axis)

where number is the curve's number, x-axis is either 0 or 1 (bottom or top) and y-axis is either 0 or 1 (left or right).

You can also add analytical function curves to a plot layer:

l.addFunction("x*sin(x)", 0, 3*pi, points = 100)
l.addParametricFunction("cos(m)", "sin(m)", 0, 2*pi, points = 100, variableName = "m")
l.addPolarFunction("t", "t", 0, 2*pi, points = 100, variableName = "t")
In case you need the number of curves on a layer, you can get it with

l.numCurves()

The plot title


l.setTitle("My beautiful plot")
l.setTitleFont(QtGui.QFont("Arial", 12))
l.setTitleColor(QtGui.QColor("red"))
l.setTitleAlignment(QtCore.Qt.AlignLeft)
The alignment parameter can be any combination of the Qt alignment flags (see the PyQt documentationfor more details).

If you want you can remove the plot title using:


l.removeTitle()
Here's how you can add greek symbols in the plot title or in any other text in the plot layer: axis labels, legends:

l.setTitle("normal text <font face=\"Symbol\">greek text</font>")

Using the font specifications, you can also change the color of some parts of the title only:

l=newGraph().activeLayer()
l.setTitle("<font color = red>red</font> <font color = yellow>yellow</font> <font color = blue>blue</font>")

Customizing the axes

Layer axes can be shown/hidden using the following function:

l.enableAxis(int axis, on = True)
where axiscan be any integer value between 0 and 3 or the equivalent reserved word:

0.

Layer.Left

1.

Layer.Right

2.

Layer.Bottom

3.

Layer.Top

If an axis is enabled, you can fully customize it via a Python script. For example you can set its title:

l.setAxisTitle(axis, "My axis title")
l.setAxisTitleFont(axis, QtGui.QFont("Arial", 11))
l.setAxisTitleColor(axis, QtGui.QColor("blue"))
l.setAxisTitleAlignment(axis, alignFlags)
its color and the font used for the tick labels:

l.setAxisColor(axis, QtGui.QColor("green"))
l.setAxisFont(axis, QtGui.QFont("Arial", 10))
The tick labels of an axis can be enabled or disabled, you can set their color and their rotation angle:

l.enableAxisLabels(axis, on = True)
l.setAxisLabelsColor(axis, QtGui.QColor("black"))
l.setAxisLabelRotation(axis, angle)
anglecan be any integer value between -90 and 90 degrees. A rotation angle can be set only for horizontal axes (Bottom and Top).

The numerical format of the labels can be set using:


l.setAxisNumericFormat(axis, format, precision = 6, formula)
where formatcan have the following values:

0.

Automatic: the most compact numeric representation is chosen

1.

Decimal: numbers are displayed in floating point form

2.

Scientific: numbers are displayed using the exponential notation

3.

Superscripts: like Scientific, but the exponential part is displayed as a power of 10

precisionis the number of significant digits and formulais a mathematical expression that can be used to link oposite scales. It's argument must be xfor horizontal axes and yfor vertical axes. For example, assuming that the bottom axis displays a range of wavelengths in nanometers and that the top axis represents the equivalent energies in eV, with the help of the code bellow all the wavelengths will be automatically converted to electron-volts and the result will be displayed in floating point form with two significant digits after the decimal dot sign:

l.setAxisNumericFormat(Layer.Top, 1, 2, "1239.8419/x")
The axis ticks can be customized via the following functions:

l.setTicksLength(minLength, majLength)
l.setAxisTicksLength(axis, majTicksType, minTicksType, minLength, majLength)
where the majTicksTypeand minTicksTypeparameters specify the desired orientation for the major and minor ticks, respectively:

0.

Layer.NoTicks

1.

Layer.Out: outward orientation for ticks, with respect to the plot canvas

2.

Layer.InOut: both inward and outward ticks

3.

Layer.In: inward ticks

minLengthspecifies the length of the minor ticks, in pixels and majLengththe length of the major ticks.

You can also customize the scales of the different axes using:


l.setScale(int axis, double start, double end, double step=0.0, int majorTicks=5, int minorTicks=5, int type=0, bool inverted=False)
where type specifies the desired scale type:

0.

Layer.Linear

1.

Layer.Log10

and step defines the size of the interval between the major scale ticks. If not specified (default value is 0.0), the step size is calculated automatically. The other flags should be self-explanatory.

Defining a scale range for an axis doesn't automatically disable autoscaling. This means that if a curve is added or removed from the layer, the axes will still automatically adapt to the new data interval. This can be avoided by disabling the autoscaling mode, thus making sure that your scale settings will always be taken into account:

l.enableAutoscaling(False)
If you want to rescale the plot layer so that all the data points are visible, you can use the following utility function:

l.setAutoScale()
The same setScalefunction above, with a longer list of arguments, can be used to define an axis break region:

l.setScale(axis, start, end, step=0.0, majorTicks=5, minorTicks=5, type=0, inverted=False,
	left=-DBL_MAX, right=DBL_MAX, breakPosition=50, stepBeforeBreak=0.0, stepAfterBreak=0.0,
	minTicksBeforeBreak=4, minTicksAfterBreak=4, log10AfterBreak=False, breakWidth=4, breakDecoration=True)
where leftspecifies the left limit of the break region, rightthe right limit, breakPositionis the position of the break expressed as a percentage of the axis length and breakWidthis the width of the break region in pixels. The names of the other parameters should be self-explanatory.

Finally, you can specify the width of all axes and enable/disable the drawing of their backbone line, using:


l.setAxesLinewidth(2)
l.drawAxesBackbones(True)

The canvas

You can display a rectangular frame around the drawing area of the plot (the canvas) and fill it with a background color, using:

l.setCanvasFrame(2, QtGui.QColor("red"))
l.setCanvasColor(QtGui.QColor("lightGrey"))
Drawing the canvas frame and disabling the axes backbone lines is the only possible solution for the issue of axes not touching themselves at their ends.

The layer frame

You can display a rectangular frame around the whole layer and fill it with a background color, using:

l.setFrame(2, QtGui.QColor("blue"))
l.setBackgroundColor(QtGui.QColor("grey"))
The default spacing between the layer frame and the other layer elements (axes, title) can be changed via:

l.setMargin(10)

Customizing the grid

You can display the grid associated to a layer axis or the whole grid using:

l.showGrid(axis)
l.showGrid()
This will display the grid with the default color, width and pen style settings. If you need to change these settings, as well as to enable/disable certain grid lines, you can use the following functions:

grid = l.grid()
grid.setMajPenX(QtGui.QPen(QtCore.Qt.red, 1))
grid.setMinPenX(QtGui.QPen(QtCore.Qt.yellow, 1, QtCore.Qt.DotLine))
grid.setMajPenY(QtGui.QPen(QtCore.Qt.green, 1))
grid.setMinPenY(QtGui.QPen(QtCore.Qt.blue, 1, QtCore.Qt.DashDotLine))
grid.enableXMax(True)
grid.enableXMin()
grid.enableYMax()
grid.enableYMin(False)
grid.enableZeroLineX(True)
grid.enableZeroLineY(False)
l.replot()
All the grid functions containing an Xrefer to the vertical grid lineas, whereas the Yleter indicates the horizontal ones. Also, the Majword refers to the main grid lines and Minto the secondary grid.

The plot legend

You can add a new legend to a plot using:

legend = l.newLegend()
#or
legend = l.newLegend("enter your text here")
Plot legends are special text objects which are updated each time you add or remove a curve from the layer. They have a special auto-updateflag which is enabled by default. The following function returns Truefor a legend object:

legend.isAutoUpdateEnabled()
You can disable/enable the auto-update behavior of a legend/text object using:

legend.setAutoUpdate(False/True)
You can add common texts like this:

text = l.addText(legend)
text.setOrigin(legend.x(), legend.y()+50)
Please notice that the addTextfunction returns a different reference to the new text object. You can use this new reference later on in order to remove the text:

l.remove(text)
Once you have created a legend/text, it's very easy to customize it. If you want to modify the text you can use:

legend.setText("Enter your text here")
All other properties of the legend: rotation angle, text color, background color, frame style, font and position of the top-left corner can be modified via the following functions:

legend.setAngle(90)
legend.setTextColor(QtGui.QColor("red"))
legend.setBackgroundColor(QtGui.QColor("yellow"))
legend.setFrameStyle(Frame.Shadow)
legend.setFrameColor(QtCore.Qt.red)
legend.setFrameWidth(3)
legend.setFrameLineStyle(QtCore.Qt.DotLine)
legend.setFont(QtGui.QFont("Arial", 14, QtGui.QFont.Bold, True))
# set top-left position using scale coordinates:
legend.setOriginCoord(200.5, 600.32)
# or set top-left position using pixel coordinates:
legend.setOrigin(5, 10)
legend.repaint()
Other frame styles available for legends are: Legend.Line, which draws a rectangle around the text and Legend.None(no frame at all). There is also a function allowing you to add an automatically built time stamp:

timeStamp = l.addTimeStamp()

Adding arrows/lines to a plot layer


arrow = ArrowMarker()
arrow.setStart(10.5, 12.5)
arrow.setEnd(200, 400.51)
arrow.setStyle(QtCore.Qt.DashLine)
arrow.setColor(QtGui.QColor("red"))
arrow.setWidth(1)
arrow.drawStartArrow(False)
arrow.drawEndArrow(True)
arrow.setHeadLength(7)
arrow.setHeadAngle(35)
arrow.fillArrowHead(True)

l = newGraph().activeLayer()
arrow1 = l.addArrow(arrow)

arrow.setStart(120.5, 320.5)
arrow.setColor(QtGui.QColor("blue"))
arrow2 = l.addArrow(arrow)

l.remove(arrow1)
As you might notice from the sample code above, the addArrowfunction returns a reference to a new arrow object that can be used later on to modify this new arrow or to delete it with the removefunction.

Adding images to a layer


l = newGraph().activeLayer()
image = l.addImage("C:/poze/adi/PIC00074.jpg")
image.setCoordinates(200, 800, 800, 200)
image.setFrameStyle(Frame.Shadow)
image.setFrameColor(QtCore.Qt.green)
image.setFrameWidth(3)
l.replot()
The setCoordinatesfunction above can be used to set the geometry of the image using scale coordinates. If you need to specify the image geometry in pixel coordinates, independently of the plot axes values, you may use the following functions:

image.setOrigin(x, y)
image.setSize(width, height)
image.setRect(x, y, width, height)
l.replot()
You can remove an image using its reference:

l.remove(image)

Rectangles


l = newGraph().activeLayer()

r = Rectangle(l)
r.setSize(100, 100)
r.setOrigin(100, 200)
r.setBackgroundColor(QtCore.Qt.yellow)
r.setFrameColor(QtCore.Qt.red)
r.setFrameWidth(3)
r.setFrameLineStyle(QtCore.Qt.DotLine)
r.setBrush(QtGui.QBrush(QtCore.Qt.green, QtCore.Qt.FDiagPattern))

r1 = l.add(r)
You can remove a rectangle using its reference:

r2 = l.add(r)
r2.setOrigin(200, 200)
l.remove(r1)

Circles/Ellipses


l = newGraph().activeLayer()

e = Ellipse(l)
e.setSize(100, 100)
e.setOrigin(100, 200)
e.setBackgroundColor(QtCore.Qt.yellow)
e.setFrameColor(QtCore.Qt.red)
e.setFrameWidth(0.8)
e.setFrameLineStyle(QtCore.Qt.DotLine)
e.setBrush(QtGui.QBrush(QtCore.Qt.green, QtCore.Qt.FDiagPattern))

l.add(e)

Antialiasing

Antialiasing can be enabled/disabled for the drawing of the curves and other layer objects, but it is a very resources consuming feature:

l.setAntialiasing(True, bool update = True)

Resize events

The default behaviour of 2D plot layers, with respect to the resizing of the graph window is to adapt the sizes of the fonts used for the various texts, to the new size of the plot window. You can override this behaviour and keep the size of the fonts unchanged:

l.setAutoscaleFonts(False)

Exporting plots/layers to different image formats

Layers and whole Graphs can be printed and exported from within Python. The fastest way to export a plot/layer is the following:

l.export(fileName)
This function uses some default parameters for the properties of the image. If you need more control over the exported images you can use one of the following specialized functions:

l.exportVector(fileName, dpi = 96, color = True, size = QSizeF(), unit = Frame.Pixel, fontsFactor = 1.0)
l.exportImage(filename, quality = 100, transparent = False, dpi = 0, size = QSizeF(), unit = Frame.Pixel, fontsFactor = 1.0)

The function exportVector can export the plot/layer to the following vector formats: .eps, .ps, .pdf.

The function exportImage can be used if you need to export to one of the Qt supported bitmap image formats (.bmp, .png, .jpg, etc...). The transparent option can only be used in conjunction with the file formats supporting transprency: .png and .tif (.tiff). The quality parameter influences the size of the output file. The higher this value (maximum is 100), the higher the qualitity of the image, but the larger the size of the resulting files. The dpi parameter represents the export resolution in pixels per inch (the default is screen resolution), size is the printed size of the image (the default is the size on screen) and unit is the length unit used to express the custom size and can take one of the following values:

0.

Inch

1.

Millimeter

2.

Centimeter

3.

Point: 1/72th of an inch

4.

Pixel

The fontsFactor parameter represents a scaling factor for the font sizes of all texts in the plot (the default is 1.0, meaning no scaling). If you set this parameter to 0, the program will automatically try to calculate a scale factor.

All the export functions rely on the file name suffix in order to choose the image format.

Arranging Layers

When you are working with many layers in a 2D plot window, setting the layout of these layers manually can be a very tedious task. With the help of a simple Python script you can make this task very easy and automatically manage the layout of the plot window. For example, here's how you can create a two rows by two columns matrix of layers, each plot layer having a canvas size (the drawing area) of 400 pixels wide and 300 pixels in height:

g = newGraph("Test", 4, 2, 2)
g.setLayerCanvasSize(400, 300)
g.arrangeLayers(False, True)
The arrangeLayers()function takes two parameters. The first one specifies if the layers should be arranged automatically, using a best-layout algorithm, or if the numbers of rows and columns is fixed by the user. If the value of the second parameter is True, the size of the canvas is fixed by the user and the plot window will be enlarged or shrinked, according to the user settings. Otherwise the size of the plot window will be kept and the canvas area of each layer will be automatically adapted to fit this size. Here's how you can modify the graph created in the previous example, in order to display a row of three layers, while keeping the size of the plot window unchanged:

g.setNumLayers(3)
g.setRows(1)
g.setCols(3)
g.arrangeLayers(False, False)
By default, the space betwee two neighbouring layers as well as the distance between the layers and the borders of the plot window is set to five pixels. You can change the spacing between the layers and the margins using the following functions:

g.setSpacing (x, y)
g.setMargins (left, right, top, bottom)
Another aspect of the layout management is the alignement of the layers. There are three alignement flags that you can use for the horizontal alignement (HCenter, Left, Right) and another three for the vertical alignement (VCenter, Top, Bottom) of the layers. The following code line aligns the layers with the right edge of the window and centers them vertically in the available space:

g.setAlignement(Graph.Right, Graph.VCenter)
All the examples above suppose that the layers are aranged on a grid, but of course you can add layers at any position in the plot window. In the examples bellow the x, y coordinates, in pixels, refer to the position of the top-left corner of the layer. The origin of the coordinates system coincides with the top-left corner of the plot window, the y coordinate increasing towards the bottom of the window. If the width and height of the layer are not specified they will be set to the default values:

g = newGraph()
l1 = g.addLayer()
l2 = g.addLayer(10, 20, 200, 200)
l3 = g.addLayer(215, 20)
You can remove a plot layer using:

l = g.layer(num)
g.removeLayer(l)
g.removeActiveLayer()
As you have already seen, in a plot window the active layer is, by default, the last layer added to the plot, but you can change it programatically:

l = g.layer(num)
g.setActiveLayer(l)
In case you need to perform a repetitive task on all the layers in a plot window, you need to use a for loop and of course you need to know the number of layers existant on the plot. Here's a small example showing how to custom the titles of all the layers in the plot window:

g = graph("Graph1")
layers = g.numLayers()
for i in range (1, layers+1):
	l = g.layer(i)
	l.setTitle("Layer"+QtCore.QString.number(i))
	l.setTitleColor(QtGui.QColor("red"))
	l.setTitleFont(QtGui.QFont("Arial", 14, QtGui.QFont.Bold, True))
	l.setTitleAlignment(QtCore.Qt.AlignLeft)
Finally, sometimes it might be useful to be able to swap two layers. This can be done with the help of the following function:

g.swapLayers(layerNum1, layerNum2)

3D Plots

Creating a 3D plot

You can plot 3D analytical functions or parametric surfaces. For the 3D functions, the only parameters allowed are xfor the the abscissae values and yfor the ordinates:

g = plot3D("sin(x*y)", -10.0, 10.0, -10.0, 10.0, -2.0, 2.0)
For the parametric surfaces the only parameters allowed are the latitude and the longitude: uand v. Here's, for example, how you can plot a sphere:

g = plot3D("cos(u)*cos(v)", "sin(u)*cos(v)", "sin(v)", -3.14, 3.14, -2, 2)
You can also create 3D height maps using data from matrices and, of course, you can plot table columns:

g = plot3D(matrix("Matrix1"), style = 5)
g = plot3D(table("Table1"), "3", style)
In the case of 3D plots created from matrix data sources the styleparameter can take any integer value from 1 to 5, with the following signification:

1.

Wireframe style

2.

Hidden Line style

3.

Color filled polygons without edges

4.

Color filled polygons with separately colored edges

5.

Scattered points (the default style)

For 3D plots created from tables the styleparameter can take any integer value from 0 to 3 or the equivalent style values from the follwing list:

0.

Graph3D.Scatter

1.

Graph3D.Trajectory

2.

Graph3D.Bars

3.

Graph3D.Ribbon

An alternative method to create a 3D plot is to create an empty plot window and to assign a data source to it. As you have already seen a data source can be an analytical function, a matrix or a table. For large data sets you can increase the drawing speed by reducing the number of points taken into account. The lower the resolution parameter, the higher the number of points used: for an integer value of 1, all the data points are drawn.

g = newPlot3D("test3D")
g.setTitle("My 3D Plot", QtGui.QColor("blue"), QtGui.QFont("Arial",14))
g.setResolution(2)
g.setFunction("sin(x*y)", -10.0, 10.0, -10.0, 10.0, -2.0, 2.0)
#or
g.setData(table("Table1"), "3")
#or
g.setMatrix(matrix("Matrix1"))
Once a plot is created, you can modify the scales and set the data range to display, using, for example:

g.setScales(-1.0, 1.0, -10.0, 11.0, -2.0, 3.0)

Customizing the view

When a new 3D plot is created, the scene view parameters are set to default values. Of course, QtiPlot provides functions to customize each aspect of the view. For example, you can set rotation angles, in degrees, around the X, Y and Z axes, respectively, using:

g.setRotation(45, 15, 35)
The following function allows you to shift the plot along the world X, Y and Z axes, respectively:

g.setShift(3.0, 7.0, -4.0)
You can also zoom in/out the entire plot as a whole, or you can zoom along a particular axis:

g.setZoom(10)
g.setScale(0.1, 0.05, 0.3)
Also, you can automatically detect the zoom values that fit best with the size of the plot window:

g.findBestLayout()
You can enable/disable the perspective view mode or animate the view using:

g.setOrthogonal(False)
g.animate(True)

Plot Styles

The style of the 3D plot can be set using the following functions:

g.setPolygonStyle()
g.setFilledMeshStyle()
g.showLegend(True)
g.setHiddenLineStyle()
g.setWireframeStyle()
g.setAntialiasing(True)
g.setMeshLineWidth(0.7)
For scatter plots using points you can specify the radius of the points and their shape: circles if smoothis True, rectangles otherwise.

g.setDotOptions(10, smooth = True)
g.setDotStyle()
Other symbols available for scatter plots are: bars

g.setBarRadius(0.01)
g.setBarLines(False)
g.setFilledBars(True)
g.setBarStyle()
cones

g.setConeOptions(radius, quality)
g.setConeStyle()
and crosses (surrounded by a box frame, if boxedis set to True):

g.setCrossOptions(radius, width, smooth, boxed)
g.setCrossStyle()

The 2D Projection

By default the floor projection of the 3D surface plot is disabled. You can enable a full 2D projection or only display the isolines using the following functions:

g.showFloorProjection()
g.showFloorIsolines()
g.setEmptyFloor()

Customizing the Coordinates System

The coordinates system around the surface plot can be customized to display all the twelve axes, only three of them or none, respectively, with the help of the following funcions:

g.setBoxed()
g.setFramed()
g.setNoAxes()
If the axes are enabled, you can set their legends and the distance between the legend and the axes via:

g.setXAxisLabel("X axis legend")
g.setYAxisLabel("Y axis legend")
g.setZAxisLabel("Z axis legend")
g.setLabelsDistance(30)
Also, you can fix the length of the major and minor ticks of an axis:

g.setXAxisTickLength(2.5, 1.5)
g.setYAxisTickLength(2.5, 1.5)
g.setZAxisTickLength(2.5, 1.5)

Grid

If the coordinate system is displayed, you can also display a grid around the surface plot. Each side of the grid can be shown/hidden:

g.setLeftGrid(True)
g.setRightGrid()
g.setCeilGrid()
g.setFloorGrid()
g.setFrontGrid()
g.setBackGrid(False)

Customizing the Plot Colors

The default color map of the plot is defined using two colors: red for the maximum data values and blue for the minimum data values. You can change these default colors:

g.setDataColors(QtCore.Qt.black, QtCore.Qt.green)
g.update()
Of course, you can define more complex color maps, using LinearColorMapobjects:

map = LinearColorMap(QtCore.Qt.yellow, QtCore.Qt.blue)
map.setMode(LinearColorMap.FixedColors) # default mode is LinearColorMap.ScaledColors
map.addColorStop(0.2, QtCore.Qt.magenta)
map.addColorStop(0.7, QtCore.Qt.cyan)
g.setDataColorMap(map)
g.update()
Also, you can use predifined color maps stored in .map files. A .map file consists of a of 255 lines, each line defining a color coded as RGB values. A set of predefined color map files can be downloaded from QtiPlot web site, in the "Miscelanous" section.

g.setDataColorMap(fileName)
g.update()
The colors of all the other plot elements can be customized as shown bellow. Don't forget to update the plot in order to display the new colors:

g.setMeshColor(QtGui.QColor("blue"))
g.setAxesColor(QtGui.QColor("green"))
g.setNumbersColor(QtGui.QColor("black"))
g.setLabelsColor(QtGui.QColor("darkRed"))
g.setBackgroundColor(QtGui.QColor("lightYellow"))
g.setGridColor(QtGui.QColor("grey"))
g.setDataColors(QtGui.QColor("red"), QtGui.QColor("orange"))
g.setOpacity(0.75)
g.update()

Exporting

In order to export a 3D plot you need to specify a file name containing a valid file format extention:

g.export(fileName)
This function uses some default export options. If you want to customize the exported image, you should use the following function in order to export to raster image formats:

g.exportImage(fileName, int quality = 100, bool transparent = False, dpi = 0, size = QSizeF(), unit = Frame.Pixel, fontsFactor = 1.0)
where qualityis a compression factor: the larger its value, the better the quality of the exported image, but also the larger the file size. The dpiparameter represents the export resolution in pixels per inch (the default is screen resolution), sizeis the printed size of the image (the default is the size on screen) and unitis the length unit used to express the custom size and can take one of the following values:

0.

Inch

1.

Millimeter

2.

Centimeter

3.

Point: 1/72th of an inch

4.

Pixel

The fontsFactorparameter represents a scaling factor for the font sizes of all texts in the plot (the default is 1.0, meaning no scaling). If you set this parameter to 0, the program will automatically try to calculate a scale factor.

3D plots can be exported to any of the following vector formats: .eps, .ps, .pdf, .pgf and .svg, using the function bellow:


g.exportVector(fileName, textMode = 0, sortMode = 1)
where textMode is an integer value, specifing how texts are handled. It can take one of the following values:

0.

All text will be converted to bitmap images (default).

1.

Text output in the native output format.

2.

Text output in additional LaTeX file as an overlay.

The last option sortMode is also an integer value and can take one of the following values:

0.

No sorting at all.

1.

A simple, quick sort algorithm (default).

2.

BSP sort: best algorithm, but slow.

Data Analysis

General Functions

As you will see in the following subsections, the data analysis operations available in QtiPlot are: convolution/deconvolution, correlation, differentiation, FFT, filtering, smoothing, fitting and numerical integration of data sets. Generally, you can declare/initialize an analysis operation using one of the following methods, depending on the data source, which can be a 2D plot curve or a table:

op = FFTFilter(graph("Graph1").activeLayer(), "Table1_2", 1.5, 3.9)
op = LinearFit(table("Table1"), "colX", "colY", 10, 100)
In the first example the data source is a curve Table1_2, plotted in the active layer of the graph Graph1and the abscissae range is chosen between 1.5 and 3.9. In the second example the data source is a table Table1. The abscissae of the data set are stored in the column called colXand the ordinates in the column colY. The data range is chosen between the 10th row and the row with the index 100. If you don't specify the row range, by default the whole table will be used. Not all operations support curves as data sources, like for example: convolution/deconvolution and correlation. For these operations only table columns can be used as data sources for the moment.

Once you have initialized an operation, you can still chage its input data via the following functions:


op.setDataFromCurve("Table1_energy", 10.5, 20.1, graph("Graph2").activeLayer())
op.setDataFromTable(table("Table1"), "colX", "colY", 10, 100)
You don't have to specify a plot layer in the setDataFromCurve() function, if the analysis operation has already been initialized by specifying a curve on an existing graph and you just want to treat another curve from the same plot layer.

Also, when performing analysis tasks via Python scripts, there are several utility functions that can be called for all operations. For example you can disable any graphical output from an operation or you can redirect the output to the plot layer of your choice:


op.enableGraphicsDisplay(False)
op.enableGraphicsDisplay(True, graph("Graph2").activeLayer())
Let's assume that you need to perform a specific operation op, which analyses your data and at the end, displays a result curve. For this kind of operations, you can customize the number of points in the resulting curve and its color:

op.setOutputPoints(int)
op.setColor(int)
op.setColor("green")
Colors can be specified by their names or as integer values, from 0 to 23, each integer corresponding to a predefined color: 0 - "black", 1 - "red", 2 - "green", 3 - "blue", 4 - "cyan", 5 - "magenta", 6 - "yellow", 7 - "darkYellow", 8 - "navy", 9 - "purple", etc ...

Most of the time, a new table is also created as a result of a data analysis operation. This table stores the data displayed by the result curve and is hidden by default, but you can interact with it via the following function:


t = op.resultTable()

After the initialization of an analysis operation, which consists of setting the data source, the data range and some other properties, like color, number of points, etc..., you can execute it via a call to its run() function:

op.run()
For data fitting operations, there's an alias for the run() function which is: fit().

Correlation, Convolution/Deconvolution

Assuming you have a table named "Table1", here's how you can calculate the convolution of two of its columns, "Table1_B" and "Table1_C":

conv = Convolution(table("Table1"),  "B", "C")
conv.setColor("green")
conv.run()
The deconvolution and the correlation of two data sets can be done using a similar synthax:

dec = Deconvolution(table("Table1"),  "B", "C")
dec.run()

cor = Correlation(table("Table1"),  "B", "C", 10, 200)
cor.setColor("green")
cor.run()

Differentiation

Assuming you have a Graph named "Graph1" with a curve entitled "Table1_2" (on its active layer), here's how you can differentiate this curve within a defined x interval, [2,10] in this case:

diff = Differentiation(graph("Graph1").activeLayer(), "Table1_2", 2, 10)
diff.run()
The result of these code sequence would be a new plot window displaying the derivative of the initial curve. The numerical derivative is calculated using a five terms formula.

FFT

Assuming you have a graph named "Graph1" with a curve entitled "Table1_2" (on its active layer), having a periodicity of 0.1 in the time domain, a FFT will allow you to extract its characteristic frequencies. The results will be stored in a hidden table named "FFT1".

fft = FFT(graph("Graph1").activeLayer(), "Table1_2")
fft.normalizeAmplitudes(False)
fft.shiftFrequencies(False)
fft.setSampling(0.1)
fft.run()
By default the calculated amplitudes are normalized and the corresponding frequencies are shifted in order to obtain a centered x-scale. If we want to recover the initial curve with the help of the inverse transformation, we mustn't modify the amplitudes and the frequencies. Also the sampling parameter must be set to the inverse of the time period, that is 10. Here's how we can perform the inverse FFT, using the "FFT1" table, in order to recover the original curve:

ifft = FFT(table("FFT1"), "Real", "Imaginary")
ifft.setInverseFFT()
ifft.normalizeAmplitudes(False)
ifft.shiftFrequencies(False)
ifft.setSampling(10)
ifft.run()

FFT Filters

In this section, it will be assumed that you have a signal (data set "Table1_2") displayed in a graph ("Graph1", on its active layer). This signal has a power spectrum with high and low frequencies. You can filter some of these frequencies according to your needs, using a FFTFilter. Here's how you can cut all the frequencies lower than 1 Hz:

filter = FFTFilter(graph("Graph1").activeLayer(), "Table1_2", FFTFilter.HighPass)
filter.setCutoff(1)
filter.run()
Here's how you can cut all the frequencies lower than 1.5 Hz and higher than 3.5 Hz. In the following example the continuous component of the signal is also removed:

filter.setFilterType(FFTFilter.BandPass)
filter.enableOffset(False)
filter.setBand(1.5, 3.5)
filter.run()
Other types of FFT filters available in QtiPlot are: low pass (FFTFilter.LowPass) and band block (FFTFilter.BandBlock).

Fitting

Assuming you have a graph named "Graph1" with a curve entitled "Table1_2" (on its active layer), a minimal Fit example would be:

f = GaussFit(graph("Graph1").activeLayer(), "Table1_2")
f.guessInitialValues()
f.fit()
	  
This creates a new GaussFit object on the curve, lets it guess the start parameters and does the fit. The following fit types are supported:

  • LinearFit(layer, curve)

  • PolynomialFit(layer, curve, degree=2, legend=False)

  • ExponentialFit(layer, curve, growth=False)

  • TwoExpFit(layer, curve)

  • ThreeExpFit(layer, curve)

  • GaussFit(layer, curve)

  • GaussAmpFit(layer, curve)

  • LorentzFit(layer, curve)

  • LogisticFit(layer, curve)

  • SigmoidalFit(layer, curve)

  • NonLinearFit(layer, curve)

    
f = NonLinearFit(layer, curve)
    f.setFormula(formula_string)
    f.save(fileName)
    		    
  • PluginFit(layer, curve)

    
f = PluginFit(layer, curve)
    f.load(pluginName)
    		    
For each of these, you can optionally restrict the X range that will be used for the fit, like in

f = LinearFit(graph("Graph1").activeLayer(), "Table1_2", 2, 7)
f.fit()
	  
You can also restrict the search range for any of the fit parameters:

f = NonLinearFit(graph("Graph1").activeLayer(), "Table1_2")
f.setFormula("a0+a1*x+a2*x*x")
f.setParameterRange(parameterIndex, start, end)
All the settings of a non-linear fit can be saved to an XML file and restored later one, using this file, for a faster editing process. Here's for example how you can save the above fit function:

f.save("/fit_models/poly_fit.txt")
and how you can use this file during another fitting session, later on:

f = NonLinearFit(graph("Graph1").activeLayer(), "Table1_2")
f.load("/fit_models/poly_fit.txt")
f.fit()
If your script relies on a specific numbering of the fit parameters use setParameters() before setting the formula and switch of automatic detection of the fit parameters when the fit formula is set:

f.setParameters("a2","a0","a1")
f.setFormula("a0+a1*x+a2*x*x",0)

After creating the Fit object and before calling its fit() method, you can set a number of parameters that influence the fit:


f.setDataFromTable(table("Table4"), "w", "energy", 10, 200) change data source
f.setDataFromCurve(curve)			change data source
f.setDataFromCurve(curve, graph)		change data source
f.setDataFromCurve(curve, from, to)		change data source
f.setDataFromCurve(curve, from, to, graph)	change data source
f.setInterval(from, to)				change data range
f.setInitialValue(number, value)
f.setInitialValues(value1, ...)
f.guessInitialValues()
f.setAlgorithm(algo) # algo = Fit.ScaledLevenbergMarquardt, Fit.UnscaledLevenbergMarquardt, Fit.NelderMeadSimplex
f.setWeightingData(method, colname) # method = Fit.NoWeighting, Fit.Instrumental, Fit.Statistical, Fit.Dataset, Fit.Direct
f.setTolerance(tolerance)
f.setOutputPrecision(precision)
f.setMaximumIterations(number)
f.scaleErrors(yes = True)
f.setColor("green")			change the color of the result fit curve to green (default color is red)
	  

After you've called fit(), you have a number of possibilities for extracting the results:

f.results()
f.errors()
f.residuals()
f.dataSize()
f.numParameters()
f.parametersTable("params")
f.covarianceMatrix("cov")
There are a number of statistical functions allowing you to test the goodness of the fit:

f.chiSquare()
f.rSquare()
f.adjustedRSquare()
f.rmse() # Root Mean Squared Error
f.rss()  # Residual Sum of Squares
Also you can display the confidence and the prediction limits for the fit, using a custom confidence level:

f.showPredictionLimits(0.95)
f.showConfidenceLimits(0.95)
Confidence limits for individual fit parameters can be calculated using:

f.lcl(parameterIndex, confidenceLevel)
f.ucl(parameterIndex, confidenceLevel)
where parameterIndexis a value between zero and f.numParameters() - 1.

It is important to know that QtiPlot can generate an analytical formula for the resulting fit curve or a normal plot curve with data stored in a hidden table. You can choose either of these two output options, before calling the fit() instruction, using:


f.generateFunction(True, points=100)

If the first parameter of the above function is set to True, QtiPlot will generate an analytical function curve. If the points parameter is not specified, by default the function will be estimated over 100 points. You can get the analytical formula of the fit curve via a call to resultFormula():

formula = f.resultFormula()
print(formula)
If the first parameter of generateFunction() is set to False, QtiPlot will create a hidden data table contining the same number of points as the data set/curve to be fitted (same abscissae). You can interact with this table and extract the data points of the result fit curve using:

t = f.resultTable()

Integration

With the same assumptions as above, here's how you can integrate a curve within a given interval:

integral = Integration(graph("Graph1").activeLayer(), "Table1_2", 2, 10)
integral.setMethodOrder(4)
integral.setTolerance(1e-4)
integral.setMaximumIterations(100)
integral.run()
result = integral.area()
The method order parameter can be any integer value between 1 (Trapezoidal rule, the default value) and 5. The code integrates the curve using an iterative algorithm. The tolerance determines the termination criteria for the solver. Because, sometimes we ask for too much accuracy, setting a maximum number of iterations makes sure that the solver will not enter an infinite loop, which could freeze the application.

As you can see from the above example, the numerical value of the integral can be obtained via the area() function.

Interpolation

The interpolation is used to generate a new data curve with a high number of points from an existing data set. Here's an example:

interpolation = Interpolation(graph("Graph1").activeLayer(), "Table1_2", 2, 10, Interpolation.Linear)
interpolation.setOutputPoints(10000)
interpolation.setColor("green")
interpolation.run()
The simplest interpolation method is the linear method. There are two other methods available: Interpolation.Akimaand Interpolation.Cubic. You can choose the interpolation method using:

interpolation.setMethod(Interpolation.Akima)

Smoothing

Assuming you have a graph named "Graph1" with an irregular curve entitled "Table1_2" (on its active layer). You can smooth this curve using a SmoothFilter:

smooth = SmoothFilter(graph("Graph1").activeLayer(), "Table1_2", SmoothFilter.Average)
smooth.setSmoothPoints(10)
smooth.run()
The default smoothing method is the mowing window average. Other smoothing methods are the SmoothFilter.FFTand SmoothFilter.SavitzkyGolay. Here's an example of how to use this last method:

smooth.setSmoothPoints(5,5)
smooth.setMethod(SmoothFilter.SavitzkyGolay)
smooth.setPolynomOrder(9)
smooth.run()

Using Qt's dialogs and classes

Let's assume that you have a lot of ASCII data files to analyze. Furthermore, let's suppose that these files were created during several series of measurements, each measurement generating a set of files identified by a certain string in the file name, like for example: "disper1". In order to analyze these files, you need first of all to import them into tables. The following code snippet shows how to automize this task using Qt dialogs and convenience classes:

# Pop-up a file dialog allowing to chose the working folder:
dirPath = QtGui.QFileDialog.getExistingDirectory(qti.app, "Choose Working Folder", "/test/")

# Create a folder object using Qt's QDir class:
folder = QtCore.QDir(dirPath)

# Pop-up a text input dialog allowing to chose the file naming pattern:
namePattern = QtGui.QInputDialog.getText(qti.app, "Enter Pattern", "Text: ", QtGui.QLineEdit.Normal, "disper1")

# Get the list of file names in the working directory containing the above pattern:
fileNames = folder.entryList (QtCore.QStringList ("*_" + namePattern[0] + "*.dat"))

# Import each file into a new project table:
for i in range (0, lst.count()):
    t = newTable()
    t.importASCII(dirPath + fileNames[i], " ", 0, False, True, True)
For a detailed description of all the dialogs and utility classes provided by Qt/PyQt please take a look at the PyQt documentation.

Task automatization example

Bellow you can find a detailed example showing how to completely automatize tasks in QtiPlot. It can be used in order to verify the accuracy of the curve fitting algorithms in QtiPlot. The data used in this example is retrieved from the Statistical Reference Datasets Project of the National Institute of Standards and Technology (NIST). In order to run this example, you need an internet connection, since the script will try to download all the nonlinear regression test files from the Statistical Reference Datasets Project.

import urllib, re, sys

# Pop-up a file dialog allowing to chose a destination folder:
dirPath = QtGui.QFileDialog.getExistingDirectory(qti.app, "Choose Destination Folder")

saveout = sys.stdout
# create a log file in the destination folder
fsock = open(dirPath + "/" + "results.txt", "w")
sys.stdout = fsock

# on Unix systems you can redirect the output directly to a console by uncommenting the line bellow:
#sys.stdout = sys.__stdout__

# make sure that the decimal separator is the dot character
qti.app.setLocale(QtCore.QLocale.c())

host = "http://www.itl.nist.gov/div898/strd/nls/data/LINKS/DATA/"
url = urllib.urlopen(host)
url_string = url.read()
p = re.compile( '\w{,}.dat">' )
iterator = p.finditer( url_string )
for m in iterator:
	name = (m.group()).replace("\">", "")
	if (name == "Nelson.dat"):
		continue

	url = host + name
	print  "\nRetrieving file: " + url
	path = dirPath + "/" + name
	urllib.urlretrieve( url, path ) # retrieve .dat file to specified location

	file = QtCore.QFile(path)
	if file.open(QtCore.QIODevice.ReadOnly):
		ts = QtCore.QTextStream(file)
		name = name.replace(".dat", "")
		changeFolder(addFolder(name)) #create a new folder and move to it
		formula = ""
		parameters = 0
		initValues = list()
		certifiedValues = list()
		standardDevValues = list()
		xLabel = "X"
		yLabel = "Y"

		while (ts.atEnd() == False):
			s = ts.readLine().simplified()

			if (s.contains("(y = ")):
				lst = s.split("=")
				yLabel = lst[1].remove(")")

			if (s.contains("(x = ")):
				lst = s.split("=")
				xLabel = lst[1].remove(")")

			if (s.contains("Model:")):
				s = ts.readLine().simplified()
				lst = s.split(QtCore.QRegExp("\\s"))
				s = lst[0]
				parameters = s.toInt()[0]
				ts.readLine()
				if (name == "Roszman1"):
					ts.readLine()
					formula = ts.readLine().simplified()
				else:
					formula = (ts.readLine() + ts.readLine() + ts.readLine()).simplified()
				formula.remove("+ e").remove("y =").replace("[", "(").replace("]", ")")
				formula.replace("**", "^").replace("arctan", "atan")

			if (s.contains("Starting")):
				ts.readLine()
				ts.readLine()
				for i in range (1, parameters + 1):
					s = ts.readLine().simplified()
					lst = s.split(" = ")
					s = lst[1].simplified()
					lst = s.split(QtCore.QRegExp("\\s"))
					initValues.append(lst[1])
					certifiedValues.append(lst[2])
					standardDevValues.append(lst[3])

			if (s.contains("Data: y")):
				row = 0
				t = newTable(name, 300, 2)
				t.setColName(1, "y")
				t.setColumnRole(1, Table.Y)
				t.setColName(2, "x")
				t.setColumnRole(2, Table.X)
				while (ts.atEnd() == False):
					row = row + 1
					s = ts.readLine().simplified()
					lst = s.split(QtCore.QRegExp("\\s"))
					t.setText(1, row, lst[0])
					t.setText(2, row, lst[1])

				g = plot(t, t.colName(1), Layer.Scatter).activeLayer()
				g.setTitle("Data set: " + name + ".dat")
				g.setAxisTitle(Layer.Bottom, xLabel)
				g.setAxisTitle(Layer.Left, yLabel)

				f = NonLinearFit(g, name + "_" + t.colName(1))
				if (f.setFormula(formula) == False) :
					file.close()
					changeFolder(rootFolder())
					continue

				f.scaleErrors()
				for i in range (0, parameters):
					f.setInitialValue(i, initValues[i].toDouble()[0])
				f.fit()
				g.removeLegend()
				f.showLegend()
				print  "QtiPlot Results:\n" + f.legendInfo()

				print  "\nCertified Values:"
				paramNames = f.parameterNames()
				for i in range (0, parameters):
					print  '%s = %s +/- %s' % (paramNames[i], certifiedValues[i], standardDevValues[i])

				print  "\nDifference with QtiPlot results:"
				results = f.results()
				for i in range (0, parameters):
					diff = fabs(results[i] - certifiedValues[i].toDouble()[0])
					print  'db%d = %6g' % (i+1, diff)

				file.close()
				changeFolder(rootFolder())

newNote("ResultsLog").importASCII(dirPath + "/" + "results.txt")
saveProjectAs(dirPath + "/" + "StRD_NIST.qti")
sys.stdout = saveout
fsock.close()