【问题标题】:python pandas scatterplot error: is this a bug with pandas?python pandas scatterplot错误:这是熊猫的错误​​吗?
【发布时间】:2013-10-16 22:24:15
【问题描述】:

似乎无法让熊猫做散点图:

试图做一个散点图,不断收到这个错误:

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

基本示例:

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

我试图做一个气泡图(带有颜色和大小的散点图),需要 4 个额外的参数

气泡示例:

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

再一次,无论简单(分散)还是复杂(气泡):都无法超越这个错误

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

感谢任何帮助: 这是熊猫的错误​​吗? matplotlib 也会抛出错误? -最好的

【问题讨论】:

  • 你能发布一些示例数据以便可以复制吗?

标签: python matplotlib pandas


【解决方案1】:

对于 pandas 0.12matplotlib 1.3,当您将 DataFrame 的列作为参数传递给 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 0.13,有一个打开的拉取请求将添加“scatter”作为df.plot 的选项,因此以下行应该是等效的:

df.plot(kind='scatter', x='a', y='b', c='c', s=df.d * 100)  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-11
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    相关资源
    最近更新 更多