numpy 创建一个代表我个人算法的图形,而不使用库计算函数

brtdzjyr  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(87)

我对Python中的图形不是很了解。有没有可能创建一个由我个人算法生成的图形?
我想创建图形,但不使用库的计算函数。例如,我看到要在Scipy中创建图形,必须首先使用它提供的计算函数,然后计算结果以图形表示。
我想创建一个我的不同My_Customs的绘图,使用Matplotlibplt.bar,其中一些酒吧是红色的,一些是绿色。与标签下的每个酒吧
我如何用图表来表示这一点?(我接受任何书店)

#Green Color
My_Custom_1 = ((1.54 ** 1) * 2.7182818284 ** (-1.54)) / 1 * 100
My_Custom_2 = ((1.54 ** 2) * 2.7182818284 ** (-1.54)) / 2 * 100
My_Custom_3 = ((1.54 ** 3) * 2.7182818284 ** (-1.54)) / 6 * 100

#Red color
My_Custom_0 = ((1.54 ** 0) * 2.7182818284 ** (-1.54)) / 1 * 100
My_Custom_4 = ((1.54 ** 4) * 2.7182818284 ** (-1.54)) / 24 * 100

字符串
我已经创建了一个小软件,它已经执行了计算并将结果返回给我。我不想使用函数来计算泊松。我想直接将各种My_Customs的结果用于图形中。
我画了我想找的东西:

kqlmhetl

kqlmhetl1#

“不使用库计算函数”是一个反目标。你至少应该使用Numpy,而不是重复计算。你使用matplotlib的事实意味着你已经安装了numpy。
除此之外,如果您希望条形图标签与绘图中所描绘的一样,

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(5)
d = np.array((1, 1, 2, 6, 24))
my_custom = 1.54**x * 2.7182818284**-1.54 / d * 100
colors = tuple('rgggr')

fig, ax = plt.subplots()
ax.bar(
    [f'My_Custom_{i}' for i in x],
    my_custom,
    color=colors,
)
plt.show()

字符串


的数据

hwamh0ep

hwamh0ep2#

也许你的意思是这样的:


的数据

import math
import matplotlib.pyplot as plt

def factorial( n ):
    f = 1
    for i in range( 1, n + 1 ):
        f *= i
    return f

def Poisson( mu, n ):
    return mu ** n * math.exp( -mu ) / factorial( n )

# The all-important parameter ...
p = 1.54

goals = [ 0, 1, 2, 3, 4 ]
prob_percent = []
for x in goals:
    prob_percent.append( 100 * Poisson( p, x ) )

plt.bar( goals, prob_percent, color=[ "red", "green", "green", "green", "red" ] )
plt.xlabel( "Goals" )
plt.ylabel( "Percent" )
plt.show()

字符串

相关问题