创建条形图但仅获取 TypeError size-1 数组可以转换为 Python 标量

Create barplot but getting TypeError only size-1 arrays can be converted to Python scalars

我正在尝试创建一个条形图,它显示不同反应的结合能力增加百分比。但是,我不断收到错误消息“TypeError only size-1 arrays can be converted to Python scalars”。这是我的代码:

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.3722 - (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 = [range(97,126)]
    
    plt.bar(reactions, increases)
    plt.xlabel("Reactions")
    plt.ylabel("Increase in binding capacity (%)")
    plt.title("Pure chitin")
    plt.show()

calculate_concentration(numbers_ci)

您的变量反应是一个范围列表而不是值列表

# this is how your reactions varibale looks like
[range(97, 126)]
# should look like this
[97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125]

尝试这样做 insetead

reactions = list(range(97,126))

未正确创建列表反应。目前它是一个包含单个范围的列表。我想你想要的是

reactions = [i for i in range(97,126)]