2016-09-07 104 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謝謝! –