How do you Plot data generated by a python script?

If I understand your question correctly, I may have a solution to your problem. First of all, the statement below, when converted to Python code, will generate an array of numbers the same length of your masses list.

'y runs fron 0 to n where n == len(masses) ’

However, this statement will give you a single number:

x = ‘mass_avg = sum(masses)/len(masses)’

You will not be able to plot these two objects because of the different sizes. If you are asking about a ‘running’ or cumulative mean, then you may want to use the cumulative sum function from Numpy (cumsum). To convert this into a cumulative average, you can do a simple division.

Below is a modification to your script that incorporates this averaging technique. (I don’t know why you want to print everything. Surely you can’t see all of the data as the file gets processed. It is also a very slow operation… I’ll just ignore those parts.)

import numpy as np

import matplotlib.pyplot as plt

f = open(‘myfile.txt’)

f.next() # You want to skip the first line, I guess.

mass = []

for line in f:

This will skip the lines that are spaces.

if line.isspace(): continue

The strip function is unnecessary. The defalut for the split function takes care of that.

columns = line.split()

Don’t call the float function every time. It’s a waste.

mass.append( columns[8] )

Here we can convert the list of strings into an array of floats with the dtype keyword.

mass = np.array( mass, dtype=‘float’)

Here’s the cumulative average steps.

mass_sum = np.cumsum(mass)

mass_average = mass_sum/ np.arange(1, len(mass_sum) + 1)

If you only plot one array or list of values, they are assumed to be the y values.

The x values in that case are the indices of the y value array.

plt.plot(mass_average)

plt.show()

Ryan

···

Message: 5
Date: Thu, 25 Aug 2011 11:15:57 -0700 (PDT)
From: surfcast23 <surfcast23@…287…>

Subject: Re: [Matplotlib-users] How do you Plot data generated by a
python script?
To: matplotlib-users@lists.sourceforge.net
Message-ID: <32336570.post@…986…>

Content-Type: text/plain; charset=us-ascii

Hi Martin,

Thank for the relpy.  What I have is a script that reads the data from

a large file then prints out the values listed in a particular column. What

I now need to do is have the information in that column plotted as the
number of rows vs. the mean value of all of the rows. What I have so far is

import matplotlib.pyplot as plt

masses = []

f = open( ‘myfile.txt’,‘r’)

f.readline()
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

mass = float(mass)
masses.append(mass)
print(mass)

plt.plot()
plt.show

I am thinking I can do something like

'y runs fron 0 to n where n == len(masses) ’
x = ‘mass_avg = sum(masses)/len(masses)’

Problem is I don’ tknow how to have matplotlib do it with out giving me an
error about dimentions. I would also like to do this with out having to
write and read from another file. I alos need to to be able to work on files

with ddifering numbers of rows.

Thanks

mdekauwe wrote:

I wasn’t quite able to follow exactly what you wanted to do but maybe this
will help. I am going to generate some “data” that I think sounds a bit

like yours, write it to a file, clearly you already have this. Then I am
going to read it back in and plot it, e.g.

import matplotlib.pyplot as plt
import numpy as np

Generate some data a little like yours, I think?

print it to a file, i.e. I am making your myfile.txt

numrows = 100
numcols = 8
mass = np.random.normal(0, 1, (numrows * numcols)).reshape(numrows,
numcols)
f = open(“myfile.txt”, “w”)

for i in xrange(numrows):
for j in xrange(numcols):
print >>f, mass[i,j],
print >> f
f.close()

read the file back in

mass = np.loadtxt(“myfile.txt”)

plot the 8th column

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(mass[:,7], ‘r-o’)
ax.set_xlabel(“Time”)
ax.set_ylabel(“Mass”)

plt.show()

I wasn’t clear on the mean bit, but that is easy to do with numpy, e.g.

mean_mass = np.mean(mass[:,8])

etc.

Numpy et al is great for stuff like this.

Hope that helps,

Martin


View this message in context: http://old.nabble.com/How-do-you-Plot-data-generated-by-a-python-script–tp32328822p32336570.html

Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi Ryan,

I think your solution will work thank you!! I did get an error though it is

" f.next() # You want to skip the first line, I guess.
    AttributeError: '_io.TextIOWrapper' object has no attribute 'next' "

thank you

Khary

rcnelson wrote:

···

If I understand your question correctly, I may have a solution to your
problem. First of all, the statement below, when converted to Python
code, will generate an array of numbers the same length of your masses
list.

'y runs fron 0 to n where n == len(masses) '

However, this statement will give you a single number:

x = 'mass_avg = sum(masses)/len(masses)'

You will not be able to plot these two objects because of the different
sizes. If you are asking about a 'running' or cumulative mean, then you
may
want to use the cumulative sum function from Numpy (cumsum). To convert
this
into a cumulative average, you can do a simple division.

Below is a modification to your script that incorporates this averaging
technique. (I don't know why you want to print everything. Surely you
can't
see all of the data as the file gets processed. It is also a very slow
operation... I'll just ignore those parts.)

import numpy as np
import matplotlib.pyplot as plt
f = open('myfile.txt')
f.next() # You want to skip the first line, I guess.
mass = []
for line in f:
    # This will skip the lines that are spaces.
    if line.isspace(): continue
    # The strip function is unnecessary. The defalut for the split
function
takes care of that.
    columns = line.split()
    # Don't call the float function every time. It's a waste.
    mass.append( columns[8] )
# Here we can convert the list of strings into an array of floats with the
dtype keyword.
mass = np.array( mass, dtype='float')
# Here's the cumulative average steps.
mass_sum = np.cumsum(mass)
mass_average = mass_sum/ np.arange(1, len(mass_sum) + 1)
# If you only plot one array or list of values, they are assumed to be the
y
values.
# The x values in that case are the indices of the y value array.
plt.plot(mass_average)
plt.show()

Ryan

Message: 5
Date: Thu, 25 Aug 2011 11:15:57 -0700 (PDT)
From: surfcast23 <surfcast23@...287...>
Subject: Re: [Matplotlib-users] How do you Plot data generated by a
       python script?
To: matplotlib-users@lists.sourceforge.net
Message-ID: <32336570.post@...986...>
Content-Type: text/plain; charset=us-ascii

Hi Martin,

    Thank for the relpy. What I have is a script that reads the data
from
a large file then prints out the values listed in a particular column.
What
I now need to do is have the information in that column plotted as the
number of rows vs. the mean value of all of the rows. What I have so far
is

import matplotlib.pyplot as plt

masses = []

f = open( 'myfile.txt','r')
f.readline()
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
   mass = float(mass)
   masses.append(mass)
   print(mass)

plt.plot()
plt.show

I am thinking I can do something like

'y runs fron 0 to n where n == len(masses) '
x = 'mass_avg = sum(masses)/len(masses)'

Problem is I don' tknow how to have matplotlib do it with out giving me
an
error about dimentions. I would also like to do this with out having to
write and read from another file. I alos need to to be able to work on
files
with ddifering numbers of rows.

Thanks

mdekauwe wrote:
>
> I wasn't quite able to follow exactly what you wanted to do but maybe
this
> will help. I am going to generate some "data" that I think sounds a bit
> like yours, write it to a file, clearly you already have this. Then I
am
> going to read it back in and plot it, e.g.
>
> import matplotlib.pyplot as plt
> import numpy as np
>
> # Generate some data a little like yours, I think?
> # print it to a file, i.e. I am making your myfile.txt
> numrows = 100
> numcols = 8
> mass = np.random.normal(0, 1, (numrows * numcols)).reshape(numrows,
> numcols)
> f = open("myfile.txt", "w")
> for i in xrange(numrows):
> for j in xrange(numcols):
> print >>f, mass[i,j],
> print >> f
> f.close()
>
> # read the file back in
> mass = np.loadtxt("myfile.txt")
>
> # plot the 8th column
> fig = plt.figure()
> ax = fig.add_subplot(111)
> ax.plot(mass[:,7], 'r-o')
> ax.set_xlabel("Time")
> ax.set_ylabel("Mass")
> plt.show()
>
>
> I wasn't clear on the mean bit, but that is easy to do with numpy, e.g.
>
> mean_mass = np.mean(mass[:,8])
>
> etc.
>
> Numpy et al is great for stuff like this.
>
> Hope that helps,
>
> Martin
>
>


View this message in context:
http://old.nabble.com/How-do-you-Plot-data-generated-by-a-python-script--tp32328822p32336570.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
EMC VNX: the world's simplest storage, starting under $10K
The only unified storage solution that offers unified management
Up to 160% more powerful than alternatives and 25% more efficient.
Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users


View this message in context: http://old.nabble.com/How-do-you-Plot-data-generated-by-a-python-script--tp32328822p32338782.html
Sent from the matplotlib - users mailing list archive at Nabble.com.