displaying graph after importing txt file matplotlib
Question: I am writing a simple program to output a basic graph after importing a text
file. i get the following error:

    
    
    Traceback (most recent call last):
      File "C:\Users\Chris1\Desktop\attempt2\ex1.py", line 13, in <module>
        x.append(int(xAndY[0]))
    ValueError: invalid literal for int() with base 10: '270.286'
    

my python code looks like this:

    
    
        import matplotlib.pyplot as plt
    
    x = []
    y = []
    
    readFile = open ('temp.txt', 'r')
    
    sepFile = readFile.read().split('\n')
    readFile.close()
    
    for plotPair in sepFile:
        xAndY = plotPair.split(',')
        x.append(int(xAndY[0]))
        y.append(int(xAndY[1]))
    
    print x
    print y
    
    
    
    plt.plot(x, y)
    
    plt.title('example 1')
    plt.xlabel('D')
    plt.ylabel('Frequency')
    
    plt.show()    
    

a snippet of my text file looks like this:

    
    
    270.286,4.353,16968.982,1903.115
    38.934,68.608,16909.727,1930.394    
    190.989,1.148,16785.367,1969.925         
    

the issue seems minor but cannot seem to resolve it myself thanks


Answer: It is quite simple, just replace the `int` conversion with `float`:

    
    
    for plotPair in sepFile:
        xAndY = plotPair.split(',')
        x.append(float(xAndY[0]))
        y.append(float(xAndY[1]))
    

That will fix the error.

