Python is a dynamic programming language that allows fast, platform independent development. It is a high level programming language that uses a clear and consistent syntax, aiming for both concise and readable code. In contrast to MATLAB, it is a general purpose language, not directly aimed at numerical calculations. Additional functionality is provided by packages. For scientific functionality, the
SciPy library (a package set) is key. This library adds functionality for fast numerical calculations, simulation and data visualization (and more). In this example, we will specifically make use of the
Scripting with Python
We will reuse the example from the MATLAB and LaTeX scripting page. The thought process is exactly the same so we will not repeat all steps and only detail the code.
Below is the first fragment of the Python-script displayed. We notice several steps: first, the necessary packages are loaded. Second, the data is loaded from file and the transfer function is computed. Finally, the data is saved to file.
If we want to plot some data, we can make use of the (already loaded)
matplotlib.pyplot class. The plotting commands from
plotpy are very similar (sometimes identical) to their MATLAB counterparts. Important to note are the
set_tight_layout command and explicit
show method call. The first method is needed to ensure everything looks good. By default, it might be possible a title or label is (partially) out of the frame. This is fixed by this call. The second command displays the actual plot. Unlike the MATLAB counterpart,
figure does not open a figure window. The
show method opens a window for a specific figure (
Finally, we need to save the plot. This is much easier with Python and
pyplot than MATLAB. A single-line command suffices:
All source code used to create this page, including measurement data, is contained in the Python-script (
.py) and CSV-file (