views:

68

answers:

2

Hi,

I'm very new to python. two days. trying to get a plot working with matplotlib. I'm getting this error:

cannot perform reduce with flexible type

the line with the error is:

ax.scatter(x,y,z,marker='o')

Variables:

ax is defined as: ax = Axes3D(fig)

fig is defined as: fig = plt.figure()

x, y, z are lists

Any python experts tell me why? I got the plot to work but the values are integers. Decimal values are preferred.

I read that lists in python are not very good with numbers... can someone clarify? I got it to work with lists and ndarrays but all the values have to be integers.

Thanks!

+1  A: 

Compare your code with this basic scatter plot code. Notice that there xs,ys,zs are ndarrays of floats. Does that code work for you? Maybe you can incrementally morph that code into your own to get code that works (or learn where yours breaks).

If that doesn't help, perhaps post enough code to allow us to reproduce the problem. At the very least, post the output of

print(repr(x))
print(repr(y))
print(repr(z))
unutbu
+1  A: 

Thanks for the responses.

I got it working with floats but maybe you all can tell me why. I just created an account here and can't edit my guest question...

I'm getting my data from a sqlite database. I can round the values in the sql or cast to floats and it works. I'm guessing that the data was a numerical object even though it was numerical. Is this correct?

Here's an excerpt of the code that doesn't work. I got it to work by casting the values as floats.


import sqlite3
import re
import numpy as np
from numpy import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

conn = sqlite3.connect('./RES/resDB.dbl')

fig = plt.figure()
ax = Axes3D(fig)

sam = array([])
x = array([])
y = array([])
z = array([])

for sv in conn.execute('select sam,easting,northing,altitude from trx_all'):
    sam = np.append(sam, [sv[0]]) #this works: sam = np.append(sam, [float(sv[0])])
    x = np.append(x, [sv[1]]) #this works: x = np.append(x, [float(sv[1])])
    y = np.append(y, [sv[2]]) #this works: y = np.append(y, [float(sv[2])])
    z = np.append(z, [sv[3]]) #this works: z = np.append(z, [float(sv[3])])
if sam.__len__() > 0:
    print(repr(x))
    print(repr(y))
    print(repr(z))
    ax.scatter(x, y, z, marker='o')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title("3D plot")
plt.show()

unutbu requested the output of print(repr(x)):


x: array([u'227004.744896075', u'227029.694213685', u'227051.351657645', ...,
       u'226416.484484696', u'226436.436477995', u'226401.253669657'],
      dtype='U16')
y: array([u'3648736.75423911', u'3648737.3357031', u'3648746.34280013', ...,
       u'3645870.67565062', u'3645893.61216942', u'3645866.95833722'],
      dtype='U16')
z: array([u'1600.2589433603', u'1535.77224937826', u'1429.01016684435', ...,
       u'341.064685015939', u'343.452275622636', u'312.491347198375'],
      dtype='U16')

and finally here's the error that it outputs on the line ax.scatter(x,y,z,marker='o') :


Traceback (most recent call last):
  File "C:\stripped_3dplot.py", line 27, in 
    ax.scatter(x, y, z, marker='o')
  File "C:\Python27\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1019, in scatter
    patches = Axes.scatter(self, xs, ys, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 5813, in scatter
    minx = np.amin(temp_x)
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 1846, in amin
    return amin(axis, out)
TypeError: cannot perform reduce with flexible type

Like i said i think it was just the wrong type, but if you all can tell me what type causes the error i could learn something.

Thanks.

max
You're not using arrays for floats, you're using arrays of strings, which is why you're getting an error. You have to convert the strings to a floating point value.Numpy has string arrays, so if you try to convert a bunch of strings directly to an array without setting the dtype of the array, it will assume you want a string array. (Also, np.append is slow, as numpy arrays aren't built to have fast appends. It's better to accumulate things in a list and then convert the list to an array.)
Joe Kington