2016-09-07 99 views
0

我想用不同的nBirthday Problem可视化。我的目标是在同一幅图中绘制多个图形,但不起作用。它只绘制最后一张图并忽略其他图。我正在使用Jupyter Notebook。 这是我的代码:使用pylab绘制多个图形不起作用

from decimal import Decimal 

def calc_p_distinct(n): 
    p_distinct = numpy.arange(0, n.size, dtype=Decimal) 
    for i in n: 
     p_distinct[i] = Decimal(1.0) 

    for i in n: 
     for person in range(i): 
      p_distinct[i] = Decimal(p_distinct[i]) * Decimal(((Decimal(365-person))/Decimal(365))) 

    return p_distinct 


# n is the number of people 
n = numpy.arange(0, 20) 
n2 = numpy.arange(0, 50) 
n3 = numpy.arange(0, 100) 

# plot the probability distribution 
p_distinct = calc_p_distinct(n) 
pylab.plot(n, p_distinct, 'r') 

p_distinct2 = calc_p_distinct(n2) 
pylab.plot(n2, p_distinct2, 'g') 

p_distinct3 = calc_p_distinct(n3) 
pylab.plot(n3, p_distinct3, 'b') 

# set the labels of the axis and title 
pylab.xlabel("n", fontsize=18) 
pylab.ylabel("probability", fontsize=18) 
pylab.title("birthday problem", fontsize=20) 

# show grid 
pylab.grid(True) 

# show the plot 
pylab.show() 

当我更换calc_p_distinct()函数调用与其他内置功能(例如numpy.sin(N)),它会告诉我两个图形之一。所以,我得出结论,它必须与我的功能有关。我在这里做错了什么?

回答

0

这不是matplotlib;所有线条都在那里,就在彼此之上(这非常合理;对于100人而言,仅前20人的概率与仅20人的概率相同)。

如果我很快就绘制它们具有不同的线宽:

enter image description here

+0

啊!我的坏,你是绝对正确的,也许当我这样做时已经太迟了:D谢谢! –