syntax
syntax

Reputation: 585

python pandas scatterplot error: is this a bug with pandas?

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

Upvotes: 3

Views: 1260

Answers (1)

bmu
bmu

Reputation: 36224

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)  

Upvotes: 1

Related Questions