0

I've been trying to plot a database that I parsed from a text file into a numpy array. The array has 857 rows.

This error keeps popping up, I dont understand what it means.

import matplotlib
import matplotlib.pyplot as plt
import numpy

def parseFile(file):
    F = open(file)
    n = len(F.readlines())  #no. of lines in file
    numpyMat = numpy.zeros((n,3)) # create numpy matrix
    classLabelVector = []
    F = open(file)          # from the beginning again
    i = 0
    for line in F.readlines():
        line = line.strip()
        listFromLine = line.split()
        numpyMat[i,:] = listFromLine[0:3]   # 3 is the no. of variables/columns
        classLabelVector.append(int(listFromLine[-1]))
    i+=1
    return numpyMat, classLabelVector

dataMatrix = parseFile('kiwibubbles_tran.txt')

fig = plt.figure()

ax = fig.add_subplot(111)
ax.scatter(dataMatrix[:1], dataMatrix[:2])   # Error

plt.show()

Error: ValueError: could not broadcast input array from shape (857,3) into shape (857)

Eiffel
  • 153
  • 4

1 Answers1

0

Your dataMatrix is a tuple, so you have two options:

Take the results of the fuction in two different variables:

numpyMat, classLabels = parseFile('kiwibubbles_tran.txt')
fig = plt.figure()

ax = fig.add_subplot(111)
ax.scatter(numpyMat[:,1], numpyMat[:,2])

plt.show()

Plot only the numpyMat from the tuple:

dataMatrix = parseFile('kiwibubbles_tran.txt')
fig = plt.figure()

ax = fig.add_subplot(111)
ax.scatter(dataMatrix[0][:,1], dataMatrix[0][:,2])

plt.show()
Alberto Castaño
  • 186
  • 5
  • 16