0

I'm trying to use python to make plots. Below is the simplified version of my code that cause error.

import numpy as np
import matplotlib   
import matplotlib.pyplot as plt
matplotlib.use("AGG")

distance = np.array('f')
depth = np.array('f')# make sure these two arrays store float type value

with open('Line671.txt','r') as datafile:
  for line in datafile:
    word_count = 0
    for word in line.split():
        word = float(word)#convert string type to float
        if word_count == 0:
            distance = np.append(distance, word)
        elif word_count == 1:
            depth = np.append(depth, word)
        else:
          print 'Error'
        word_count += 1

datafile.closed


print depth
print distance #outputs looks correct 
# original data is like this: -5.3458000e+00
# output of the array is :['f' '-5.3458' '-5.3463' ..., '-5.4902' '-5.4912' '-5.4926']

plt.plot(depth, distance)# here comes the problem  

The error message says that in line for plt.plot(depth, distance): ValueError: could not convert string to float: f
I don't understand this because it seems I converted all string values into float type. I tried to search this problem on stackoverflow but they all seem to solve the problem once they cast all string values into float or int. Can anyone give any suggestion on this problem? I would be really appreciate for any help.

Kathyz
  • 23
  • 1
  • 6

1 Answers1

-1

You confused the value with the type. If you're trying to declare the type, you need to use "dtype=". What you actually did was to stick a single character into the array.

To answer a later question, your line

word = float(word)

likely worked just fine. However, we can't tell because you didn't do anything with the resulting value. Are you expecting this to alter the original inside the variable "line"? Common variables don't work that way.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Thank you so much for your reply. Could you explain a little more about this dtype? Do I use it when I declare array or when I add values? – Kathyz Sep 23 '15 at 23:36
  • http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html This should also be in your course or training materials. – Prune Sep 23 '15 at 23:41
  • Thanks. I'm just learning it by myself and tried to start with small projects. I looked up into the document and changed the declaration into: "depth = np.array(dtype=float32)" I got another error message says "NameError: name 'float32' is not defined". Did I miss something before using that? – Kathyz Sep 23 '15 at 23:54
  • Also, I'm still confused about the value type I put into the array. Why "word = float(word)" didn't work, if the values that I put into the array were string? I thought I converted because I thought the change of my value from "-5.3458000e+00" into "-5.3458" would only happen for float value instead of string. – Kathyz Sep 24 '15 at 00:00
  • http://stackoverflow.com/questions/568962/how-do-i-create-an-empty-array-matrix-in-numpy ... I suspect that you need to specify numpy.float32 -- this depends on how you import numpy in the first place. Also see http://www.engr.ucsb.edu/~shell/che210d/numpy.pdf – Prune Sep 24 '15 at 00:05
  • I'm sorry to keep bothering but dtype simply doesn't work for me for some reason. I tried with all kinds of expression listed in manual including numpy.float32. When I just use np.array(float), and print out the array, I got this :"[ -5.3458 -5.3463 ..., -5.4902 -5.4912 -5.4926]". So I guess I had an array with float type values? I still got error message for plotting though: "TypeError: float() argument must be a string or a number". – Kathyz Sep 24 '15 at 00:15