小编Tup*_*gwe的帖子

如何生成随机数的直方图?

我用代码生成了 1 到 100 之间的 100 个随机数:

def histogram():
    for x in range(100):
        x = random.randint(1, 100)
        print(x)
Run Code Online (Sandbox Code Playgroud)

现在我试图用直方图表示这些信息,我将 matplotlib.pyplot 导入为 plt 并尝试构建它,但我似乎遇到了问题。

我试过:

def histogram():
    for x in range(100):
        x = random.randint(1, 100)
        return x       
    histogram_plot = histogram()
    plt.hist(histogram_plot)
    plt.show()
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

def histogram():
    for x in range(100):
        x = random.randint(1, 100)
        print(x)
        plt.hist(x)
        plt.show()
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

python matplotlib

3
推荐指数
1
解决办法
1万
查看次数

如何在另一个公式中使用一个公式的结果?

如何将一个函数的结果用于另一个函数.

list = [1, 1, 1, 1]
def margin():
    a = 0
    b = 1.2
    c = 1
    for i in list:
        if i == 1:
            x = a + b - c
            return x    # x = 0.2


def calc():
    for i in list:
        formula = 2 + margin()
        print(formula)

calc()
# 2.2 2.2 2.2 2.2 
Run Code Online (Sandbox Code Playgroud)

我希望程序计算:

'''
          2  + 0.2 = 2.2
         2.2 + 0.2 = 2.4
         2.4 + 0.2 = 2.6
         2.6 + 0.2 = 2.8
giving …
Run Code Online (Sandbox Code Playgroud)

python math return function formula

0
推荐指数
1
解决办法
121
查看次数

标签 统计

python ×2

formula ×1

function ×1

math ×1

matplotlib ×1

return ×1