2

I am trying to follow a tutorial on youtube, now in the tutorial they plot some standard text files using matplotlib.pyplot, I can achieve this easy enough, however I am now trying to perform the same thing using some csvs I have of real data.

The code I am using is import matplotlib.pyplot as plt import csv #import numpy as np

with open(r"Example RFI regression axis\Delta RFI.csv") as x, open(r"Example RFI regression axis\strikerate.csv") as y:
    readx = csv.reader(x)
    ready = csv.reader(y)

    plt.plot(readx,ready)

    plt.title ('Test graph')
    plt.xlabel('x axis')
    plt.ylabel('y axis')

    plt.show()

The traceback I receive is long

Traceback (most recent call last):
  File "C:\V4 code snippets\matplotlib_test.py", line 11, in <module>
    plt.plot(readx,ready)
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2832, in plot
    ret = ax.plot(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 3997, in plot
    self.add_line(line)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 1507, in add_line
    self._update_line_limits(line)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 1516, in _update_line_limits
    path = line.get_path()
  File "C:\Python27\lib\site-packages\matplotlib\lines.py", line 677, in get_path
    self.recache()
  File "C:\Python27\lib\site-packages\matplotlib\lines.py", line 401, in recache
    x = np.asarray(xconv, np.float_)
  File "C:\Python27\lib\site-packages\numpy\core\numeric.py", line 320, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number

Please advise what I need to do, I realise this is probably very easy to most seasoned coders. Kind regards SMNALLY

1 Answer 1

9

csv.reader() returns strings (technically, .next()method of reader object returns lists of strings). Without converting them to float or int, you won't be able to plt.plot() them.

To save the trouble of converting, I suggest using genfromtxt() from numpy. (http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html)

For example, there are two files: data1.csv:

data1
2
3
4
3
6
6
4

and data2.csv:

data2
92
73
64
53
16
26
74

Both of them have one line of header. We can do:

import numpy as np
data1=np.genfromtxt('data1.csv', skip_header=1) #suppose it is in the current working directory
data2=np.genfromtxt('data2.csv', skip_header=1)
plt.plot(data1, data2,'o-')

and the result:

enter image description here

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

7 Comments

Hi CT Zhu, thanks for the reply, I feel this could well be the answer I am looking for. Is there any chance of providing more detail of how to do this for the example provided? Many thanks SMNALLY
You are welcome. And an example was just added. Let me know if the example fits the structure of your csv files.
Not normal Stackoverflow etiquette but.. I think i love you... many thanks :)
Testing... testing...testing...perfect :) :) :) Will be all over this now. Thanks a bunch!
It is shorthand for marker='o', linestyle='-': use filled circles for data points and solid line to connect them.
|

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.