尝试 select y 轴数据的多个列表项;获取类型错误 "list indices must be integers or slices, not tuple"
Trying to select multiple list items for y-axis data; getting TypeError "list indices must be integers or slices, not tuple"
我试图对不同的 y 轴数据使用列表 'increases' 中的不同数字,但是我收到错误 'list indices must be integers or slices, not tuple'。我看到有人说要用np.asarray(),然而,这给出了错误'too many indices for array: array is 1-dimensional, but 7 were indexed'。
import numpy as np
import matplotlib.pyplot as plt
numbers_ci = [1.113, 1.068, 0.999, 1.021, 1.078, 1.086, 1.024, 1.025, 1.082, 1.215, 1.069, 1.09, 1.11, 1.106, 1.02, 1.087, 1.124, 1.069, 1.004, 1.002, 1.058, 0.993, 1.024, 0.926, 1.099, 1.083, 0.995, 1.023, 1.422]
def calculate_concentration(numbers):
concentrations = [0.2957 - (number/2.185*0.3722) for number in numbers]
increases = [(concentration - concentrations[-1])/concentrations[-1]*100 for concentration in concentrations]
print(f"The average absorbance numbers are:\n{numbers}")
print(f"The concentrations of bound copper are:\n{concentrations}")
print(f"The increases in copper binding are:\n{increases}")
reactions = ["KNO3", "NH4NO3", "(NH4)2S2O8", "(NH4)2Cr2O7", "H2O2", "H2SO4", "NaIO4"]
x = np.arange(len(reactions))
# y1 = increases[1,5,9,13,17,21,25]
# y2 = increases[2,6,10,14,18,22,26]
# y3 = increases[3,7,11,15,19,23,27]
# y4 = increases[4,8,12,16,20,25,28]
y1 = [98.4, 109.6, 108.3, 99.4, 94.9, 116.0, 102.9]
y2 = [112.8, 107, 65.9, 100.7, 112.5, 136.7, 108]
y3 = [134.8, 126.8, 112.5, 128.1, 133.2, 126.8, 136]
y4 = [127.7, 126.5, 105.8, 106.7, 133.8, 158, 127.1]
width = 0.2
plt.bar(x-0.3, y1, width, color="blue", label="oxidator")
plt.bar(x-0.1, y2, width, color="cyan", label="KCN")
plt.bar(x+0.1, y3, width, color="orange", label="KFe")
plt.bar(x+0.3, y4, width, color="red", label="KHex")
plt.xticks(x, reactions, fontsize=7, rotation=45)
plt.xlabel("Reactions")
plt.ylabel("Increase in binding capacity (%)")
plt.title("Pure chitin")
plt.legend(fontsize=7)
plt.show()
calculate_concentration(numbers_ci)
如你所见,我把#放在了我想做的方式前面。如果我手动将数字写入列表,我会得到正确的数字,但这需要很多时间,而且我有更多数据需要这样做。问题是,我怎样才能正确使用列表索引来获得不同柱中的正确数字?
我假设您要访问列表 increases
中索引 1,5,9,13...
处的值,并将这些值放在单独的列表中。 increases[1,5,9,13,17,21,25]
不支持这样做,您一次只能访问一个索引。
y1 = [increases[i] for i in [1,5,9,13,17,21,25]]
我试图对不同的 y 轴数据使用列表 'increases' 中的不同数字,但是我收到错误 'list indices must be integers or slices, not tuple'。我看到有人说要用np.asarray(),然而,这给出了错误'too many indices for array: array is 1-dimensional, but 7 were indexed'。
import numpy as np
import matplotlib.pyplot as plt
numbers_ci = [1.113, 1.068, 0.999, 1.021, 1.078, 1.086, 1.024, 1.025, 1.082, 1.215, 1.069, 1.09, 1.11, 1.106, 1.02, 1.087, 1.124, 1.069, 1.004, 1.002, 1.058, 0.993, 1.024, 0.926, 1.099, 1.083, 0.995, 1.023, 1.422]
def calculate_concentration(numbers):
concentrations = [0.2957 - (number/2.185*0.3722) for number in numbers]
increases = [(concentration - concentrations[-1])/concentrations[-1]*100 for concentration in concentrations]
print(f"The average absorbance numbers are:\n{numbers}")
print(f"The concentrations of bound copper are:\n{concentrations}")
print(f"The increases in copper binding are:\n{increases}")
reactions = ["KNO3", "NH4NO3", "(NH4)2S2O8", "(NH4)2Cr2O7", "H2O2", "H2SO4", "NaIO4"]
x = np.arange(len(reactions))
# y1 = increases[1,5,9,13,17,21,25]
# y2 = increases[2,6,10,14,18,22,26]
# y3 = increases[3,7,11,15,19,23,27]
# y4 = increases[4,8,12,16,20,25,28]
y1 = [98.4, 109.6, 108.3, 99.4, 94.9, 116.0, 102.9]
y2 = [112.8, 107, 65.9, 100.7, 112.5, 136.7, 108]
y3 = [134.8, 126.8, 112.5, 128.1, 133.2, 126.8, 136]
y4 = [127.7, 126.5, 105.8, 106.7, 133.8, 158, 127.1]
width = 0.2
plt.bar(x-0.3, y1, width, color="blue", label="oxidator")
plt.bar(x-0.1, y2, width, color="cyan", label="KCN")
plt.bar(x+0.1, y3, width, color="orange", label="KFe")
plt.bar(x+0.3, y4, width, color="red", label="KHex")
plt.xticks(x, reactions, fontsize=7, rotation=45)
plt.xlabel("Reactions")
plt.ylabel("Increase in binding capacity (%)")
plt.title("Pure chitin")
plt.legend(fontsize=7)
plt.show()
calculate_concentration(numbers_ci)
如你所见,我把#放在了我想做的方式前面。如果我手动将数字写入列表,我会得到正确的数字,但这需要很多时间,而且我有更多数据需要这样做。问题是,我怎样才能正确使用列表索引来获得不同柱中的正确数字?
我假设您要访问列表 increases
中索引 1,5,9,13...
处的值,并将这些值放在单独的列表中。 increases[1,5,9,13,17,21,25]
不支持这样做,您一次只能访问一个索引。
y1 = [increases[i] for i in [1,5,9,13,17,21,25]]