from __future__ import division, print_function
from numpy.random import randint
import random
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
def bday(c):
trials = 5000
count = 0
for trial in range(trials):
year = [0]*365
l = False
for i in range(c):
bdayp = randint(1,365)
year[bdayp] = year[bdayp] + 1
if year[bdayp] > 1:
l = True
if l == True:
count = count + 1
prob = count / trials
return prob
for i in range(2,41):
a = bday(i)
print(i,a)
如您所见,我会生成班级人数以及他们共同生日的概率。如何使用matplotlib.pyplot绘制该图,以便在x轴上具有n(人数),在y轴上具有概率?
谢谢。
答案 0 :(得分:0)
我已在注释中链接了与您的问题有关的正确文档。为了您找到自己的解决方案,也许看看以下内容可能会更有效地解决您的问题:
def func(x):
return x * 10
x = []
y = []
for i in range(10):
x.append(i)
y.append(func(i))
plt.plot(x, y)
上述操作也可以通过以下操作来实现:
def func(x):
return x * 10
x = np.arange(10)
plt.plot(x, func(x))
这是np.arange的文档;两者都会绘制以下内容: