Friday, June 30, 2017

Python Plotting Tutorial -- Part 7

Importing data from a text file

Text files ('.txt') are a simple and common way to store data from an experiment. These files can be generated from an Excel spreadsheet, generated by a Python script, or just typed in manually. Typically one lists the data in several columns, similar to a spreadsheet or table format. The characters separating each column are called the "delimiter". Common delimiters include commas, spaces, and tabs.

Here (data.txt) is an example of a text file containing a table of data. You can open it in any text editor (e.g. notepad, textedit, sublime text, etc). It should look like this:

When importing files into python, by default the computer expects the file to be located in the same folder as the script. Therefore, to use data.txt, it should be uploaded into a folder in your Jupyter account along with the script you want to use, like so:

Now you can reference the file within you python script. Numpy has a built in function specifically for this purpose, called np.genfromtxt (genfromtxt is a slightly more advanced version of np.loadtxt).

The function np.genfromtxt has one mandatory argument: the name of the file to be opened. However, several keyword arguments are important. You must specify the column delimiter (the default delimiter is whitespace). If you have a header in the text file, you should specify skip_header=1 to skip the first row of the file when importing. By default, the data will unpack into one large array, with each entry being a row of the table. However, we usually want to extract each column seperately. By specifying unpack=True, the output will be an array of the columns. By inputting this array into a list of names, each name will be equal to one of the columns. Try running the following code to see how this works.

In order to plot data from a text file, you simply need to use the generated arrays:



Exporting data to a text file

 Numpy also makes it simple to generate a text file from an array. The function for this is np.savetxt. There are two mandatory arguments: the file name, and the array to save. Note that this function saves one array as a text file. It expects to be given an array of rows, so that each entry in the saved array is to be one row of the output file.

In order to take several array (each of equal length), and properly combine them for saving, you can use the function np.dstack ("depth stack"). This example shows how this works:
As you can see there is an unwanted extra layer to this array (the extra pair of square brackets). This can be gotten rid of using d[0] to get the first entry which is

Once you have set up the output array, it can be exported to a text file. The simplest case, with no options, looks like this:
The output file then looks like this:

To control the delimiters and the formatting of the numbers, you can include keyword arguments:
The output file then looks like:


For a more complicated example, here is the script which generated the file data.txt that we used in the previous part:





No comments:

Post a Comment