TypeError: unsupported operand type(s) for +: 'int' and 'str' when making bar graph

TypeError: unsupported operand type(s) for +: 'int' and 'str' when making bar graph

我正在尝试制作一个包含多个堆叠条形图的图表,这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

NEAlosses1 = open("NEA losses with altitude - 2015-2020.txt", "r")
NEAlosses2 = open("NEA losses with altitude - 2015-2025.txt", "r")
NEAlosses3 = open("NEA losses with altitude - 2015-2035.txt", "r")
altitudes = ['0.0', '30.0', '40.0', '50.0', '60.0']

data1 = {} # 2015-2020
for line in NEAlosses1:
    col = line.strip().split("|")
    location = col[0]
    altitude = col[1]
    eighteen = col[2] # V < 18
    twenty = col[3] # V < 20
    twentytwo = col[4] # V < 22
    try:
        data1[location][str(altitude)] = [eighteen, twenty, twentytwo]
    except KeyError:
        data1[location] = {}
        data1[location][str(altitude)] = [eighteen, twenty, twentytwo]
data2 = {} # 2015-2025
for line in NEAlosses2:
    col = line.strip().split("|")
    location = col[0]
    altitude = col[1]
    eighteen = col[2] # V < 18
    twenty = col[3] # V < 20
    twentytwo = col[4] # V < 22
    try:
        data2[location][str(altitude)] = [eighteen, twenty, twentytwo]
    except KeyError:
        data2[location] = {}
        data2[location][str(altitude)] = [eighteen, twenty, twentytwo]
data3 = {} # 2015-2035
for line in NEAlosses3:
    col = line.strip().split("|")
    location = col[0]
    altitude = col[1]
    eighteen = col[2] # V < 18
    twenty = col[3] # V < 20
    twentytwo = col[4] # V < 22
    try:
        data3[location][str(altitude)] = [eighteen, twenty, twentytwo]
    except KeyError:
        data3[location] = {}
        data3[location][str(altitude)] = [eighteen, twenty, twentytwo]
NEAlosses1.close(); NEAlosses2.close(); NEAlosses3.close()

for i in data1:
    eighteen1 = []; twenty1 = []; twentytwo1 = []
    eighteen2 = []; twenty2 = []; twentytwo2 = []
    eighteen3 = []; twenty3 = []; twentytwo3 = []
    for j in altitudes:
        eighteen1.append(data1[i][j][0])
        twenty1.append(data1[i][j][1])
        twentytwo1.append(data1[i][j][2])

        eighteen2.append(data2[i][j][0])
        twenty2.append(data2[i][j][1])
        twentytwo2.append(data2[i][j][2])

        eighteen3.append(data3[i][j][0])
        twenty3.append(data3[i][j][1])
        twentytwo3.append(data3[i][j][2])

    # plot details
    bar_width = 0.25
    epsilon = .015
    line_width = 1
    opacity = 0.7
    centre_bar_positions = np.arange(1, len(eighteen1)+1)
    left_bar_positions = centre_bar_positions - bar_width
    right_bar_positions = centre_bar_positions + bar_width

    # Make bar plots
    bar_2020_18 = plt.bar(left_bar_positions, eighteen1, bar_width, color='red')
    bar_2020_20 = plt.bar(left_bar_positions, twenty1, bar_width-epsilon, alpha=opacity, color='white', edgecolor='red', linewidth=line_width, hatch='//')
    bar_2020_22 = plt.bar(left_bar_positions, twentytwo1, bar_width-epsilon, alpha=opacity, color='white', edgecolor='red', linewidth=line_width, hatch='0')

    bar_2025_18 = plt.bar(centre_bar_positions, eighteen2, bar_width, color='blue')
    bar_2025_20 = plt.bar(centre_bar_positions, twenty2, bar_width-epsilon, alpha=opacity, color='white', edgecolor='blue', linewidth=line_width, hatch='//')
    bar_2025_22 = plt.bar(centre_bar_positions, twentytwo2, bar_width-epsilon, alpha=opacity, color='white', edgecolor='blue', linewidth=line_width, hatch='0')

    bar_2035_18 = plt.bar(right_bar_positions, eighteen3, bar_width, color='green')
    bar_2035_20 = plt.bar(right_bar_positions, twenty3, bar_width-epsilon, alpha=opacity, color='white', edgecolor='green', linewidth=line_width, hatch='//')
    bar_2035_22 = plt.bar(right_bar_positions, twentytwo3, bar_width-epsilon, alpha=opacity, color='white', edgecolor='green', linewidth=line_width, hatch='0')

    plt.xticks(centre_bar_positions, altitudes)
    plt.ylabel("Number of NEAs")
    plt.xlabel("Altitude")
    plt.show()

但是当我 运行 它时,我不断收到错误:类型错误:+ 不支持的操作数类型:'int' 和 'str'。回溯指向行

bar_2020_18 = plt.bar(left_bar_positions, eighteen1, bar_width, color='red')

但我看不到我在该行的哪个位置添加了 int 和 str...

在此先感谢您的帮助!!非常感谢:)

eighteen1 是一个包含 字符串 数字的列表。因此,您不能将此列表作为整数隐式传递给 plt.bar。记得从字符串转换为浮点数!