Plotting data from file
How can we use what we have learnt in the past few workshops to read data from a file? And how can we plot this?
One way to then plot this data would be to extract the index and/or multiple columns we are interested in plotting and use matplotlib as we have done before.
As an example, we’ll look at a new data file. This contains a simulation of the (fractional) concentration change for two species in a reaction, “A” and “B” where \(A \rightarrow B\).
If we open this file more directly in Python when we looked at a text file) we can take quick look at the contents to see what this looks like:
From printing out the first 200 characters, we can see this looks like a table of data with each column separated by spaces. This looks nicely formatted with a title row including the column names and no extra header rows.
We will see in the next year that there are dedicate packages to manipulate large tabulated files. However, for now we can exploit numpy as a quick and effective way to read such input.
This is a special kind of array called a structured array, because it has named fields. You can check this by querying the subproperties of the array:
These various names are the fields of the array, corresponding to the columns in the file. This syntax is reminiscent of what we have seen in the case of dictionaries, where keys are used to specific other objects stored in the dictionary.
This data also has a column for “Time” as the number of seconds elapsed.
To create a plot for time vs concentration of A, we can extract this data from our array
We could then use matplotlib to plot this:
This simulation shows a clean relationship as concentration decreases over time (in an exponential way).
Suppose we want to filter our data and plot only concentrations less than 0.5. We can simply create a boolean array that masks the invalid data.
Here we have filtered our data to include all fractional concentrations less than (or equal to) 0.5. Notice we have also matched our time values to plot to these concentrations by extracting the index from our new array concentration_A_low.
You can also see the slope is similiar (in fact it is the same) between our unfiltered and filtered data (this is due to the exponential relationship).
Exercise
- Re-plot the un-filtered graph created above and add another line to show concentration of B vs time from the
data_kinetic_k0005array on the same plot.
- Can you include a label for A and B?
- Calculate the the sum of the concentrations for these two columns (A and B).
- Re-plot the graph created in the previous question and add this sum as a third line.
- Consider: What does this sum tell us about “A” is decaying into “B”?
We add the sum of concentrations (A+B) as a third line to the plot. This sum remains very close to 1 throughout the reaction, indicating that as “A” decays, it is converted into “B” without significant loss or gain of total concentration. This demonstrates conservation of mass in the reaction: all of “A” is accounted for as it becomes “B”.