3

Cant seem to get pandas to do a scatterplot:

Trying to do a scatterplot, keep getting this error:

 TypeError: only length-1 arrays can be converted to Python scalars

basic example:

 x= self.df['he']
 y= self.df['xy']
 sct = plt.scatter(x,y)

I was trying to do a bubble plot (scatterplot with color and size), requires 4 additional args

bubble example:

color = self.df['clr'].values
size =  self.df['sze'].values
sct = plt.scatter(x, y, c=clr, s=size, linewidths=2, edgecolor='w')

again, regardless of simple (scatter) or complex (bubble): cannot get this beyond this error

TypeError: only length-1 arrays can be converted to Python scalars 

any help is appreciated: is this a bug in pandas? matplotlib throws error as well? -best

1
  • 1
    Can you post some sample data so that this can be reproduced? Commented Oct 17, 2013 at 4:06

1 Answer 1

1

With pandas 0.12 and matplotlib 1.3 this should work, when you pass the columns of the DataFrame as arguments to scatter:

In [18]: df = pd.DataFrame(np.random.rand(15, 4), columns=list('abcd'))

In [19]: plt.scatter(df.a, df.b, c=df.c, s=df.d*100)
Out[19]: <matplotlib.collections.PathCollection at 0x380e050>

pandas scatter example

(So maybe you are using older versions or you are using "special" data).

For pandas 0.13 there is an open pull request that will add "scatter" as an option to df.plot so that the following line should be equivalent:

df.plot(kind='scatter', x='a', y='b', c='c', s=df.d * 100)  
Sign up to request clarification or add additional context in comments.

Comments

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.