1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| import numpy as np import proplot as pplt import toml
from proplot import rc
rc['font.name'] = "Arial" rc['title.size'] = 14 rc['label.size'] = 12 rc['font.size'] = 10.5 rc['tick.width'] = 1.3 rc['meta.width'] = 1.3 rc['label.weight'] = 'bold' rc['tick.labelweight'] = 'bold' rc['ytick.major.size'] = 4.6 rc['ytick.minor.size'] = 2.5 rc['xtick.major.size'] = 4.6 rc['xtick.minor.size'] = 2.5
def calculate_deltas(pH, n, K): """ 计算 n 元弱酸的分布分数 delta :param pH: 体系的 pH 值 :param n: n 元弱酸 :param K: 解离平衡常数 K :return: 返回分布分数 delta """ C = 10 ** (-pH)
S = np.sum([(C ** ((n + 1) - i)) * np.prod(K[:i - 1]) for i in range(1, n + 1)])
F = np.zeros(n + 1) for i in range(n + 1): F[i] = (C ** (n - i)) * np.prod(K[:i])
distributions = F / S
return distributions
def plot_distribution_curve(n, K): """ 绘制分布分数-pH曲线图 :param n: n 元弱酸 :param K: 解离平衡常数 K """ pH_range = np.linspace(0, 14, 500) distributions = np.zeros((n + 1, len(pH_range)))
for i, pH in enumerate(pH_range): distributions[:, i] = calculate_deltas(pH, n, K)
sum_distribution = np.sum(distributions, axis=0) normalized_distributions = distributions / sum_distribution
fig, ax = pplt.subplots(figsize=(5.4 * 0.9, 4 * 0.9), dpi=300)
for i in range(n + 1): ax.plot(pH_range, normalized_distributions[i, :], label=f'δ{n}({i + 1})', linewidth=1.3)
ax.legend(loc='ur', ncols=1, fontweight='bold', fontsize='12.5', frame=True)
fig.format( grid=False, ylabel='Fraction', xlabel='pH Values', xlim=(0, 14), xminorlocator=1, xlocator=2, ylim=(0, 1), yminorlocator=0.1, ylocator=0.2 )
fig.show()
return fig, ax
equilibrium = toml.load("equilibrium.toml")
K_values = [item['value'] for item in equilibrium.get("K")]
acid_num = len(K_values)
fig, ax = plot_distribution_curve(acid_num, K_values)
fig.savefig("delta.png", dpi=400, bbox_inches="tight")
|