python语言绘图:绘制一组二项分布图

代码源自:

https://github.com/PacktPublishing/Bayesian-Analysis-with-Python

 

 

 

===========================================================

 

 

 

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import seaborn as sns
palette = 'muted'
sns.set_palette(palette); sns.set_color_codes(palette)


n_params = [1, 2, 4]
p_params = [0.25, 0.5, 0.75]
x = np.arange(0, max(n_params)+1)
f, ax = plt.subplots(len(n_params), len(p_params), sharex=True,
  sharey=True)
for i in range(3):
    for j in range(3):
        n = n_params[i]
        p = p_params[j]
        y = stats.binom(n=n, p=p).pmf(x)
        ax[i,j].vlines(x, 0, y, colors='b', lw=5)
        ax[i,j].set_ylim(0, 1)
        ax[i,j].plot(0, 0, label="n = {:3.2f}\np = {:3.2f}".format(n, p), alpha=0)
        ax[i,j].legend(fontsize=12)
ax[2,1].set_xlabel('$\\theta$', fontsize=14)
ax[1,0].set_ylabel('$p(y|\\theta)$', fontsize=14)
ax[0,0].set_xticks(x)
plt.savefig('B04958_01_03.png', dpi=300, figsize=(5.5, 5.5))


plt.show()

 

 

 

 

 

绘图:

 

 

 

=============================================================

 

posted on 2022-06-08 22:54  Angry_Panda  阅读(396)  评论(0编辑  收藏  举报

导航