1

I am fairly new to programing and have a question regarding matplotlib. I wrote a python script that reads in data from the outfile of another program then prints out the data from one column.

f = open( '/home/student/AstroSimulation/out.0001.z0.753.AHF_halos','r')
for line in f:
    if line != ' ':
        line = line.strip()    # Strips end of line character 
        columns = line.split() # Splits into coloumn 
        mass = columns[8]      # Column which contains mass values 
        print(mass)

What I now need to do is have matplotlib take the values printed in 'mass' and plot number versus mean mass. I have read the documents on the matplotlib website, but they are don't really address how to get data from a script(or I just did not see it). If anyone can point me to some documentation that explains how I do this it would be really appreciated. Thank you

1
  • @eryksun -yes the data is all in column 8. Each row of column 8 has a value and what I have to do is takes the average of all of the values in column eight( sum up the values in each row of column eight and divide that by the total number of rows in column eight) and plot that vs the number of rows. I apologize if I have been un clear in trying to explain what I have to do, and thank you again for your help. Commented Aug 25, 2011 at 6:39

2 Answers 2

2

You will call matplotlib from within your script, so matplotlib will not "get data from a script" as such. You send it into matplotlib.

You will need to save the masses outside the loop however, but then, it's just a call to the plot() and show() functions in it's most basic form:

import matplotlib.pyplot as plt

masses = []

f = open( '/home/student/AstroSimulation/out.0001.z0.753.AHF_halos','r')
f.readline() # Remove header line
for line in f:
    if line != ' ':
        line = line.strip()    # Strips end of line character 
        columns = line.split() # Splits into coloumn 
        mass = columns[8]      # Column which contains mass values 
        masses.append(mass)
        print(mass)

# If neccessary, process masses in some way

plt.plot(masses)
plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to do this using both your script as well as eryksun's. (I think it is instructive to be able to do it more than one way) I am trying to get rid of the first line in the column which is a string using the way eryksun did below, but it does not work on the above script. Can you explain why? Thanks
eryksun uses a different method, with an explicit iterator. If you want to remove the first line in this method, simply call f.readline() before the for loop. Also, I notice I have an error in my indentation, which I'll fix now...
1

I was with you right up to "plot the sum over the average". Perhaps you could link to an image that's like the plot you want to make.

In your current script where you print 'mass', you want to append to a list as floating point value:

from matplotlib import pyplot

DATAFILE = '/home/student/AstroSimulation/out.0001.z0.753.AHF_halos'
MASS_COL = 8

masses = []
with open(DATAFILE) as f:
    f_it = iter(f)                   #get an iterator for f
    next(f_it)                       #skip the first line
    for n, line in enumerate(f_it):  #now the for loop uses f_it 
        row = line.strip().split()
        if len(row) > MASS_COL:
            mass = row[MASS_COL]
            try:
                mass = float(mass)
                masses.append(mass)
                print "%0.3f" % mass
            except ValueError:
                print "Error (line %d): %s" % (n, mass)

#crunch mass data 
plot_data = ...

#make a plot
pyplot.plot(plot_data)
pyplot.show()

7 Comments

I had another question. How do you delete the first line of a column? The first line in my mass column is a string that I need to get rid of because it is causing errors.
sorry if this is a boneheaded question, but what do the three dots in this bit of code mean? " plot_data = ..."
@ eryksun what I ment was I need to plot number versus mean mass
xs = sum(masses) ys = [mass_avg] pyplot.plot(xs,ys) pyplot.show()
@ eryksun Hi tried runing the code a bunch of times it opens a plot window then just hangs.
|

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.