import scipy.stats as stats import numpy as np import pandas as pd import matplotlib.pyplot as plt # values for x (all between 0 and 25): x = np.linspace(0, 25, num=26) # PMF for all these values: fx_01 = stats.binom.pmf(x, 25, 0.1) fx_02 = stats.binom.pmf(x, 25, 0.2) fx_05 = stats.binom.pmf(x, 25, 0.5) fx_07 = stats.binom.pmf(x, 25, 0.7) # collect values in DataFrame: result_01 = pd.DataFrame({'x': x, 'fx_01': fx_01}) result_02 = pd.DataFrame({'x': x, 'fx_02': fx_02}) result_05 = pd.DataFrame({'x': x, 'fx_05': fx_05}) result_07 = pd.DataFrame({'x': x, 'fx_07': fx_07}) #print(f'result_01: \n{result_01}\n') #print(f'result_02: \n{result_02}\n') #print(f'result_05: \n{result_05}\n') #print(f'result_07: \n{result_07}\n') """ # plot: plt.bar(x, fx_01, color='0.6', width = 0.1) plt.xlabel('x') plt.ylabel('P(X=x)') plt.show() plt.bar(x, fx_02, color='0.6', width = 0.1) plt.xlabel('x') plt.ylabel('P(X=x)') plt.show() plt.bar(x, fx_05, color='0.6', width = 0.1) plt.xlabel('x') plt.ylabel('P(X=x)') plt.show() plt.bar(x, fx_07, color='0.6', width = 0.1) plt.xlabel('x') plt.ylabel('P(X=x)') plt.show() """ # create graph: fig = plt.figure(figsize=(14,7)) plt.subplot(221).set(title='Probability of Binomial with n=25 and p=0.1') _ = plt.bar(x, fx_01, color='0.6', width = 0.1) #_ = plt.stem(x, fx_01, use_line_collection = True) _ = plt.xlabel('x') _ = plt.ylabel('P(X=x)') plt.subplot(222).set(title='Probability of Binomial with n=25 and p=0.2') _ = plt.bar(x, fx_02, color='0.6', width = 0.1) #3_ = plt.stem(x, fx_02, use_line_collection = True) _ = plt.xlabel('x') _ = plt.ylabel('P(X=x)') plt.subplot(223).set(title='Probability of Binomial with n=25 and p=0.5') _ = plt.bar(x, fx_05, color='0.6', width = 0.1) #3_ = plt.stem(x, fx_05, use_line_collection = True) _ = plt.xlabel('x') _ = plt.ylabel('P(X=x)') plt.subplot(224).set(title='Probability of Binomial with n=25 and p=0.7') _ = plt.bar(x, fx_07, color='0.6', width = 0.1) #_ = plt.stem(x, fx_07, use_line_collection = True) _ = plt.xlabel('x') _ = plt.ylabel('P(X=x)') fig.subplots_adjust(hspace=0.4, wspace=0.2) # 하측그림의 상하 및 우측그림의 좌우 간격을 조정 #plt.savefig('C:/BOOK/PyBasics/PyStat/code/binom_pmf.png')