【发布时间】:2021-09-25 22:02:21
【问题描述】:
我正在尝试将列表“增加”中的不同数字用于不同的 y 轴数据,但是我收到错误消息“列表索引必须是整数或切片,而不是元组”。我看到有人说要使用 np.asarray(),但是,这给出了错误“数组的索引过多:数组是一维的,但有 7 个被索引”。
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)
如您所见,我将# 放在我想要的方式前面。如果我手动将数字写入列表,我会得到正确的数字,但这需要很多时间,而且我需要做更多的数据。问题是,如何正确使用列表索引在不同的条形图中获取正确的数字?
【问题讨论】:
标签: python list bar-chart typeerror