2017-03-04 64 views
-3

我正在研究癌症细胞,我试图做一个饼图来显示有丝分裂X的死亡百分比。因此,我有一个数组命名mitosis_events和另一个名为Death_events以下是我的代码:python在创建饼图和数组时出错

import matplotlib.pyplot as plt 

mitotic_events[get_generation_number(cell)] += 1 
death_events[len(cell)-1] += 1 

#Simple Pie chart 

# The slices will be ordered and plotted counter-clockwise. 
labels = 'Mitosis', 'Deaths' 
sizes = [mitotic_events, death_events] 
colors = ['Green', 'Red' ] 
explode = (0, 0.1) # only "explode" the 2nd slice (i.e. 'Hogs') 

plt.pie(sizes, explode=explode, labels=labels, colors=colors, 
    autopct='%1.1f%%', shadow=True, startangle=90) 
# Set aspect ratio to be equal so that pie is drawn as a circle. 
plt.axis('equal') 

plt.show() 

,我在控制台收到此错误:

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

我不知道该如何解决?我知道这个问题是在这一行:

sizes = [mitotic_events, death_events] 

谢谢你在先进

+1

尝试用打印至少调试您的代码。此外,你可以谷歌你的错误,并找到答案。 –

+0

我明显使用了搜索引擎,但没有涉及我的请求+我知道错误出现在这一行“”sizes = [mitotic_events,death_events]“” –

+0

您的代码片段并不完整,因为您没有显示mitotic_events的定义, 'death_events'和'plt'(尽管最后一个很明显)。请参见[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –

回答

0

您的代码不会完全展现的mitotic_eventsdeath_events的定义,因为这些使用get_generation_number()cell这是不是在你的代码中定义片段。你也不会显示如何前两个变量是创建 - 你只是显示他们如何更新。然而,我们从报表

mitotic_events[get_generation_number(cell)] += 1 
death_events[len(cell)-1] += 1 

两个mitotic_eventsdeath_events是名单见。您然后定义

sizes = [mitotic_events, death_events] 

因此sizes是列表的列表。然后尝试将sizes作为第一个参数传递给pyplot的pie()函数。

但是,pie()的第一个参数必须是数组或数字列表的第一个参数。 documentation意味着但没有说清楚,但examplestutorial表明必须如此。当pyplot读取sizes时,它会查看sizes列表中的第一项,并且它看起来不是数字(标量),而是一个列表。 Pyplot显然试图将内部列表mitotic_events更改为标量,但它是长度大于1的列表,因此转换失败。 Pyplot然后给你那不是非常有用的错误信息。

解决方案是为您构建sizes,因此它是一个数字列表。也许你应该

sizes = mitotic_events + death_events 

构建它适当地组合这两个名单,但你可能需要改变你的其他参数pie()。你没有提供足够的信息让我说更多。