1

I have to plot the values from a csv file into graph using python. I am using the follow code in python. CODE:

import numpy as np
import matplotlib.pyplot as plt

def graph():
    cv,dv = np.loadtxt('/home/test/test_mat.csv',delimiter=',',unpack=True)

    fig = plt.figure()
    ax1 = fig.add_subplot(1,1,1,axisbg='white')
    plt.plot(x=cv,y=dv)
    plt.title('Chemical Presence')
    plt.ylabel('dv')
    plt.xlabel('cv')
    plt.show()

graph()

In the code, I am trying to load the csv file from the location it is present. The data file looks like the screenshot attached here data file. Here col 1 = CV (X axis) and row1 = DV ( Y axis).

CV\Line        1            2           3           4           5
-7.49952    7.6904296   7.64923087  7.81402579  7.59429923  7.73162833
-7.48388    7.56683341  7.51190177  7.38830558  7.67669669  7.38830558
-7.46824    7.03124992  7.38830558  7.49816886  7.51190177  7.66296378
-7.4526     7.53936759  7.66296378  7.71789542  7.62176505  7.6904296
-7.43696    7.49816886  7.73162833  7.45697013  7.71789542  7.38830558
-7.42132    7.01751701  7.20977775  7.48443595  7.58056632  7.81402579
-7.40568    7.58056632  7.71789542  7.70416251  7.49816886  7.58056632

But I am getting an error while executing the code.The following is the error:

ERROR: File "/home/kishore/.local/lib/python2.7/site-packages/numpy/lib/npyio.py", line 928, in loadtxt items = [conv(val) for (conv, val) in zip(converters, vals)]

File "/home/kishore/.local/lib/python2.7/site-packages/numpy/lib/npyio.py", line 659, in floatconv
return float(x)
ValueError: could not convert string to float: CV\Line

I am new to Python programming. can anyone please help me in knowing how to solve this issue.

1
  • Thank you for your response. I made the necessary changes as you suggested. A new error has come up as mentioned below "File "test.py", line 5, in graph cv,dv = np.loadtxt(open('/home/kishore/test/test_mat.csv'),delimiter=',', skiprows=1,unpack=True) ValueError: too many values to unpack ". The reason for too many values is that the data file actually contains 54 columns and 961 rows of entries. how can I overcome this error message ? Commented Oct 15, 2015 at 18:24

1 Answer 1

1

Firstly, we should escape out the spaces in the path to make certain that it isn't choking on that:

cv,dv = np.loadtxt('/home/kishore/Documents/Owlstone\ developer\ task\ briefing/test_matrix.csv',delimiter=',',unpack=True)

or, change your directory name to have no spaces, e.g. Owlstone.

Alternatively, you could move the file and the script into the same directory and remove all the path information to prove that all is well with actually finding the file n the first place. It is clear from your error output that you are not getting past this line, so concentrate there first.

Now past the escaped space potential, let's concentrate on what is in the file. Your image and your posted data are not using , as a delimiter. Fixing that and using as data

-7.49952,7.6904296,7.64923087,7.81402579,7.59429923,7.73162833
-7.48388,7.56683341,7.51190177,7.38830558,7.67669669,7.38830558
-7.46824,7.03124992,7.38830558,7.49816886,7.51190177,7.66296378
-7.4526,7.53936759,7.66296378,7.71789542,7.62176505,7.6904296
-7.43696,7.49816886,7.73162833,7.45697013,7.71789542,7.38830558
-7.42132,7.01751701,7.20977775,7.48443595,7.58056632,7.81402579
-7.40568,7.58056632,7.71789542,7.70416251,7.49816886,7.58056632

where I have removed the first row (rather than skiprows=1 as suggested above, since that doesn't fix the Too many values to unpack problem) I can get an array via

dv = np.loadtxt('text.csv', delimiter=',')
print(dv)

yielding

[[-7.49952     7.6904296   7.64923087  7.81402579  7.59429923  7.73162833]
 [-7.48388     7.56683341  7.51190177  7.38830558  7.67669669  7.38830558]
 [-7.46824     7.03124992  7.38830558  7.49816886  7.51190177  7.66296378]
 [-7.4526      7.53936759  7.66296378  7.71789542  7.62176505  7.6904296 ]
 [-7.43696     7.49816886  7.73162833  7.45697013  7.71789542  7.38830558]
 [-7.42132     7.01751701  7.20977775  7.48443595  7.58056632  7.81402579]
 [-7.40568     7.58056632  7.71789542  7.70416251  7.49816886  7.58056632]]

N.B.: You can prove that things are working in general by including the following either in the console or in your code:

from StringIO import StringIO
c = StringIO("-7.49952,7.6904296,7.64923087,7.81402579,7.59429923,7.73162833\n-7.48388,7.56683341,7.51190177,7.38830558,7.67669669,7.38830558")
print(np.loadtxt(c, delimiter=","))

and you will see a nice array as well.

Now to plot we are going to work with the data presented, and we have (X,Y) pairs of the form (col2, col1) for the matrix, so the first two values to be plotted are (7.6904296, -7.49952) and (7.56683341, -7.48388) and we are disregarding the remainder of each row, columns 3-6, originally labelled as 2,3,4,5.

def graph():
    dv = np.loadtxt('text.csv', delimiter=',')
    print(dv)
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    x = dv[:,1]
    y = dv[:,0]
    ax.scatter(x,y)
    plt.title('Chemical Presence')
    plt.ylabel('CV')
    plt.xlabel('DV')
    plt.show()

The notation here of dv[:,0] is subsetting the array dv with all the rows and only the values of column 0, with the general format being array[rows,columns]. This code gives us

Chemical Presence Plot

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

18 Comments

Thank you for your response. I have simplified the path of the file and script into a single folder. Still I get the same error. I am wondering if I should try to add a converter to read the float values in the data file ?
I have mentioned a fraction of the data file content in the question
I don't want to have to type it in! Please paste some text so that we can recreate your problem. MCVE.
I have included the data in the code as you requested.
thank you for your response. what about selecting few values from the file and try to plot them. In this case how to represent the data in code ?
|

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.