Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib-返回数组必须为ArrayType_Python_Numpy_Matplotlib_Bar Chart - Fatal编程技术网

Python Matplotlib-返回数组必须为ArrayType

Python Matplotlib-返回数组必须为ArrayType,python,numpy,matplotlib,bar-chart,Python,Numpy,Matplotlib,Bar Chart,我有一个语法 import matplotlib.pyplot as plt import numpy as np labels = ['2009-2015', '2016-2018', '2019-2021'] European = [92.55, 83.57, 82.52] Asian = [5.96, 8.55, 10.68] African = [1.49, 1.89, 6.37] Other = [0, 5.99, 0.43] b_African = list(np.add(Eu

我有一个语法

import matplotlib.pyplot as plt
import numpy as  np

labels = ['2009-2015', '2016-2018', '2019-2021']
European = [92.55, 83.57, 82.52]
Asian = [5.96, 8.55, 10.68]
African = [1.49, 1.89, 6.37]
Other = [0, 5.99, 0.43]

b_African = list(np.add(European,Asian))
b_Other = list(np.add(European,Asian,African))

width = 0.35

fig, ax = plt.subplots()

ax.bar(labels, European, width, label='European')
ax.bar(labels, Asian, width, bottom=European,label='Asian')
ax.bar(labels, African, width, bottom=b_African,label='African')
ax.bar(labels, Other, width, bottom=b_African,label='Other')
TypeError回溯(最近一次调用)
在里面
10
11 b_非洲人=名单(np.add(欧洲、亚洲))
--->12 b_其他=列表(np.add(欧洲、亚洲、非洲))
13
14宽度=0.35
TypeError:返回数组必须为ArrayType


有人知道如何修复它吗?

这个答案遵循评论中的建议。 您想添加相同类型的数组

TypeError                                 Traceback (most recent call last)
<ipython-input-6-5cfdb21f17d8> in <module>
     10 
     11 b_African = list(np.add(European,Asian))
---> 12 b_Other = list(np.add(European, Asian,African))
     13 
     14 width = 0.35

TypeError: return arrays must be of ArrayType

添加两个相同形式的数组是np.add(),因此我认为下面的方法是可行的。 
b_Other=list(np.add(np.add(欧洲、亚洲、非洲))
import matplotlib.pyplot as plt
import numpy as  np

labels = ['2009-2015', '2016-2018', '2019-2021']
European = [92.55, 83.57, 82.52]
Asian = [5.96, 8.55, 10.68]
African = [1.49, 1.89, 6.37]
Other = [0, 5.99, 0.43]

b_African = list(np.add(European,Asian))
# This is the trick:
b_Other = list(np.add(np.add(European,Asian),African))

width = 0.35

fig, ax = plt.subplots()

ax.bar(labels, European, width, label='European');
ax.bar(labels, Asian, width, bottom=European,label='Asian');
ax.bar(labels, African, width, bottom=b_African,label='African');
ax.bar(labels, Other, width, bottom=b_African,label='Other');

plt.show()