
Today I will talk a little about the QtiPlot program and the possibility of scripting its capabilities using the Python language.
The
QtiPlot program is developed as a free analogue of
Origin , that is, a universal means of presenting and analyzing data. It allows you to build all sorts of graphs, perform data operations, look for approximations of curves, and so on. I did not work with Origin from the first year of the institute, so I can’t compare the possibilities, it’s not important, QtiPlot has three major advantages, it’s free, cross-platform and Python scripting, which will be discussed further.
Currently, the “de facto standard” for plotting in the world of * nix systems is
gnuplot , in the Windows world this is Origin, apparently broken, if you look at its cost (just under $ 1000 for the coolest version). So QtiPlot in place with Python may well press these products.
')
First we need to install qtiplot. This is best done from the repositories of your distribution, because with a large guarantee it means that all libraries will link correctly (for example, for
opensuse 11.1 ). You can take a binary from the site, but it is linked against python 2.5, and now almost everyone already has 2.6, you can also compile it yourself, but this is not so easy. By the way, speaking, here we are faced with an example of paid free software. Windows users can download only demo version, full version costs money. Well, or they can compile themselves. Or find an
alternative build on the Internet. And you can buy, it costs only 20 euros, compared with the origin of just a penny.
NB * nix users! Before installation, you need to make sure that Python and the python-sip package are installed; without this, scripting will not work.
The second step is to enable script support, for this we need the qtiplotrc.py and qtiUtil.py implementation files. Unfortunately, they made a mistake, without which scripting should work, so it’s better to
download the files from me . Put them in some place, for example ~ / .qtiplot / and run qtiplot. Next View -> Preferences -> General -> File Locations select the directory where we put the files. Now it remains only to enable Python, Scripting -> Scripting Language -> Python and open the View -> Console console. The console should contain the inscription "python is ready" if you use my files. In fact, this is not a console at all, but the output of cout, which is somewhat disappointing.
If everything worked out, you can start playing! Let's, for example, build a Fibonacci sequence. By default, we already have one table, in column X we do Fill Column With -> Row Numbers, in column Y we choose Set Column Values ​​and enter a small program there, as shown in the figure. Do not forget to uncheck Use muParser. By the way, it is active by default, which infuriates wildly.
t = currentTable ()
t.setCell (2,1,1)
t.setCell (2,2,1)
for in range (3,10):
t.setCell (2, i, t.cell (2, i-1) + t.cell (2, i-2)

Hooray, in column Y we got 10 numbers of Fibonnachi. But it was, so to speak, a first attempt at writing and checking that scripting works. It is clear that every time to write a function is not very convenient, and the number of numbers is hard-coded. Therefore, we will create our own function
with blackjack and .... Go to ~ / .qtiplot and create a file there, for example, qtimyfunc.py with the following content (yes, I know that recursion is lame, but this is just an illustration):
#!/usr/bin/python <br>
<br>
import qti, math<br>
<br>
#global fib <br>
def fib (n):<br>
" Calculates fibonacci numbers "<br>
if n == 0:<br>
return 0<br>
elif n == 1:<br>
return 1<br>
else :<br>
return fib(n-1) + fib(n-2)<br>
<br>
And we add the import_to_global line (“qtimyfunc”, None, True) to the qtiplotrc.py file. Restart qtiplot and now our function will appear in the drop-down list of functions. Since this is a full-fledged Python program, you can cram everything in there, from the simplest recursion to the calculation of the trajectory of the spacecraft.

But laziness does not stand still. Each time to fill the column, enter the function, press the button sadly a little less than completely. Therefore, we will write a script that will create a table itself, fill it in and build a graph. Let's take a look at Fibonacci, let's say we have some experimental data that are noisy and we need to build a proximization and we know that the data should have a quadratic law.
We write the following script:
#!/usr/bin/python <br>
import sys<br>
<br>
import qtimyfunc<br>
import random<br>
random.seed()<br>
<br>
t = newTable(" Data ", 20, 2)<br>
t.setColName(1, " x ")<br>
t.setColumnRole(1, Table.X)<br>
t.setColName(2, " y ")<br>
t.setColumnRole(2, Table.Y)<br>
for i in range(1, 20):<br>
t.setCell(1,i,i)<br>
t.setCell(2,i,(i+(random.randint(0,100)-50)/20)**2)<br>
g = plot(t, t.colName(2), 1).activeLayer()<br>
<br>
f = PolynomialFit(g, " Data " + " _ " + t.colName(2), 2, True)<br>
f.fit()<br>
<br>
The script uses the
Qtiplot Python API , it is very simple and well documented. To run it, execute the command from the console. Naturally, myscript.py should be in scope.
$ qtiplot -x myscript.py
The result of the script execution will be a pleasing to the eye picture:

In addition to the ability to call scripts from the console, Qtiplot can add a script launch to any menu item or hang it on a toolbar button. Thus, the customization options of the program are practically unlimited; you can create your own actions and trigger them with one mouse click. This is done in the Scripting menu. And it will turn out like this:

It should be noted that the program is constantly evolving, adding new features, extending the API, improving the interface. Unfortunately, few people know about the program now, I hope this article will help to spread it. Also, the program has a
forum where all sorts of useful additions are laid out on python.