【问题标题】:Matplotlib- return arrays must be of ArrayTypeMatplotlib- 返回数组必须是 ArrayType
【发布时间】:2021-07-21 11:20:14
【问题描述】:
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                                 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(European,Asian),African))

标签: python numpy matplotlib bar-chart


【解决方案1】:

此答案遵循 cmets 的建议。 你想添加相同类型的数组!

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()

【讨论】:

    猜你喜欢
    • 2017-02-25
    • 2019-12-15
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    相关资源
    最近更新 更多