3

I've recently been using the PyNE library to model radioactive decay of complex materials. Amongst other things, this library allows you to define an arbitrary material as a dictionary, decay it by time t, then it will return a new material as a dictionary containing the species present at that time and their concentrations. For a once off operation this is simple. However, if you want to repeat the process (decay) for several times you'll end up with several dictionaries. The data in these dictionaries show how the radioactive species evolve (decay) with time.

This is great ,however, what I really want to do is plot each species as a function of time. However, matplotlib plot won't accept multiple dictionaries as input data.

My question is, how can I re-arrange the multiple dictionaries in a pythonic fashion to show each species evolution with time, in a format matplotlib plot will accept?

Let me show an example. Suppose my initial material is defined at T0 and consists of three species each with a quantity of 1.0. My initial dictionary would look like:

T0 = {A:1.0, B:1.0, C:1.0}

Now suppose I decay this material to T1, the new material might be.

T1 = {A:1.0, B:0.35, C:1.0, D:0.75}

We can see that the species B has decayed giving rise to D, but A and C haven't.

Now let's decay it again to T2

T2 = {A:0.9, C:0.8, D:1.0, E:0.1, F:0.2}

This time species B has entirely decayed to D while A and C have started to decay to E and F.

Now obviously I've only shown three simple cases. In reality there will be hundreds of dictionaries and possibly tens of species. In the example I'd like to plot A, B, C, D, E as a function of time. Since the data is in separate dictionaries defined at each time step, Matplotlib plot won't take that as an input argument. What I need is something that will group all the A data together and plot it against time, all the B data together and plot it against time and so on...

My first thought was to append to a 2D list where essentially the columns are times and the rows are species. When extracted in to vectors these could easily be plotted. However I think this may be difficult for species which appear half way through, for example D, E, F above. You'd need to set them to zero as below:

list = [['Time', T0, T1, T2],
['A', 1.0, 1.0, 0.9],
['B', 1.0, 0.35, 0.9],
['C', 1.0, 1.0, 0.8],
['D', 0.0, 0.0, 0.9],
['E, 0.0, 0.0, 0.9]]

Any thoughts on how to manipulate these dictionaries in a succinct pythonic way in to something easily plot-able would be most appreciated.

Mark

1 Answer 1

3

I think you want pandas DataFrame, it's designed to work with 2D arrays of labelled data, with automatic alignment and convenient handling of missing data and matplotlib integration.

http://pandas.pydata.org/

T0 = {'A':1.0, 'B':1.0, 'C':1.0}
T1 = {'A':1.0, 'B':0.35, 'C':1.0, 'D':0.75}
T2 = {'A':0.9, 'C':0.8, 'D':1.0, 'E':0.1, 'F':0.2}
T3 = {'C':0.6, 'D':0.8, 'E':0.1, 'F':0.2}

df = pd.DataFrame([T0, T1, T2, T3]).fillna(0)
print(df)

     A     B    C     D    E    F
0  1.0  1.00  1.0  0.00  0.0  0.0
1  1.0  0.35  1.0  0.75  0.0  0.0
2  0.9  0.00  0.8  1.00  0.1  0.2
3  0.0  0.00  0.6  0.80  0.1  0.2

df.plot(style='o-')

decay

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.