data type and logarithm

S

simona bellavista

Hi, I am quite new to python and I am trying to do some simple plots.
I am using python Python 2.6.4 and numpy/1.5.1
I have an ASCII data file that I am reading with the following lines
of code:

import pylab
import numpy as np

filename='something.dat'
file = open(filename)

rho = np.array([], dtype = 'float64')
entropy = np.array([], dtype = 'float64')
for line in file:
columns = line.split()
rho = np.append(rho,columns[0])
entropy = np.append(entropy,columns[1])

and rho and entropy are apparently read correctly, but when I look to
the data type

print rho.dtype
print entropy.dtype

I get |S22 , what's that?
Then I want to plot a logarithmic plot and I do

pylab.plot(np.log(rho), entropy)

and I get

NotImplementedError: Not implemented for this type

Does anybody has a clue? I do not know how to proceed

Many thanks
 
N

Nobody

print rho.dtype
print entropy.dtype

I get |S22 , what's that?

A string. You probably want to convert "columns" to floats before
appending its elements to the array.
 
A

afylot

I tried to cast it to float by

rho = float(np.append(rho,columns[0]))

but I get

TypeError: don't know how to convert scalar number to float

By the way, if I avoid to perform the logarithm and do a plot like

pylab.plot(rho, entropy)

it works!

Any idea?
 
P

Peter Otten

simona said:
Hi, I am quite new to python and I am trying to do some simple plots.
I am using python Python 2.6.4 and numpy/1.5.1
I have an ASCII data file that I am reading with the following lines
of code:

import pylab
import numpy as np

filename='something.dat'
file = open(filename)

rho = np.array([], dtype = 'float64')
entropy = np.array([], dtype = 'float64')
for line in file:
columns = line.split()
rho = np.append(rho,columns[0])

You have to convert the string to a float, e. g.

rho = np.append(rho, np.float64(columns[0]))
entropy = np.append(entropy,columns[1])

and rho and entropy are apparently read correctly, but when I look to
the data type

print rho.dtype
print entropy.dtype

I get |S22 , what's that?
Then I want to plot a logarithmic plot and I do

pylab.plot(np.log(rho), entropy)

and I get

NotImplementedError: Not implemented for this type

Does anybody has a clue? I do not know how to proceed

It should be easier to use numpy.loadtxt():

with open(filename) as f:
a = np.loadtxt(f)
rho = a[..., 0]
entropy = a[..., 1]
 
T

Terry Reedy

Hi, I am quite new to python and I am trying to do some simple plots.
I am using python Python 2.6.4 and numpy/1.5.1
I have an ASCII data file that I am reading with the following lines
of code:

import pylab
import numpy as np

filename='something.dat'
file = open(filename)

combine into one statement
file = open("sjlsjls.dat")
rho = np.array([], dtype = 'float64')
entropy = np.array([], dtype = 'float64')
for line in file:
columns = line.split()

r,e = line.split()
rho = np.append(rho,columns[0])
entropy = np.append(entropy,columns[1])

rho = mp.append(rho, float(r)) # same with entropy)

does numpy really not let you write Python stype

rho.append(float(r))
?

There are also numpy,scipy lists for numpy,scipy questions.
 
R

Robert Kern

rho = mp.append(rho, float(r)) # same with entropy)

does numpy really not let you write Python stype

rho.append(float(r))
?

No. numpy arrays are not extensible in-place in general because we use view
semantics for slices and similar operations like transpositions. We can't have
the underlying memory change out from underneath us. This is one of the worst
ways to accumulate values into a numpy array.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top