from pylab import * import numpy as np def makePlot( generateEnglishPlot = True, outputFilename = r'Birthday_paradox.svg', useYLogScale = False): N=91 n = np.arange(float(N)) pbar=np.exp(-n* (n-1) / (2.0*365.0)) p=1.0-pbar n05 = 0.5*(1.0+np.sqrt(1-8.0*365.0*np.log(1.0-0.5))) plot([n05,n05],[0.0,0.5],c='k', linestyle='--') plot([0.0,n05],[0.5,0.5],c='k', linestyle='--') text(23.5,0.02,' ~23') if generateEnglishPlot: plot(n,p ,c='r',label = unicode('Probability of a pair', 'utf8')) plot(n,pbar,c='b',label = unicode('Probability of no matching pair', 'utf8')) else: plot(n,p ,c='r',label = unicode('Probabilité de coïncidence', 'utf8')) plot(n,pbar,c='b',label = unicode('Probabilité de non-coïncidence', 'utf8')) legend(loc='right') xlim(0, N) if useYLogScale: ylim(1e-6, 1) ax = gca() ax.set_yscale('log') else: ylim(0, 1) yticks([0.0,0.2,0.4,0.5,0.6,0.8,1.0]) xticks(range(0, N, 10)) grid(True, ls='-', c='#a0a0a0') if generateEnglishPlot: xlabel('Number of people') ylabel('Probability') else: xlabel('Nombre de personnes') ylabel(unicode('Probabilité', 'utf8')) savefig(outputFilename) show() makePlot(generateEnglishPlot = True, outputFilename = r'Birthday_paradox.svg') makePlot(generateEnglishPlot = False, outputFilename = r'Paradoxe_anniversaire.svg')请为代码标上注释
时间: 2023-05-29 14:06:50 浏览: 148
这段代码的作用是生成一个生日悖论的概率图。其中,np是numpy库,makePlot是绘图函数,N是设定的人数,p和pbar是概率值,n05是求得的横坐标值。函数中的参数generateEnglishPlot可以选择生成英文或法文的图,outputFilename是输出文件名,useYLogScale可以选择是否使用对数坐标轴。最后,代码绘制了两条曲线,分别代表有配对和无配对的概率,并添加了图例和网格线。
阅读全文