ChemPy plotting example
Posted On March 7, 2023
Here’s an example of how you can use the chempy library to plot the results of a reaction kinetics calculation:
import chempy
import matplotlib.pyplot as plt
# Define the reaction
reaction = chempy.Reaction(
reactants={'Benzene': 1},
products={'Phenol': 1},
k_forward=0.1,
k_reverse=0.01
)
# Define the initial conditions
initial_conditions = {'Benzene': 1.0, 'Phenol': 0.0}
# Define the reaction time
time = chempy.Quantity(100, 's')
# Calculate the reaction kinetics
result = reaction.kinetics(initial_conditions, time)
# Plot the results
plt.plot(result.time, result.concentrations['Benzene'], label='Benzene')
plt.plot(result.time, result.concentrations['Phenol'], label='Phenol')
plt.xlabel('Time (s)')
plt.ylabel('Concentration (mol/L)')
plt.legend()
plt.show()
In this example, the chempy library is used to define a reaction between benzene and phenol, with forward and reverse rate constants. The initial conditions and reaction time are defined, and the reaction kinetics are calculated using the reaction.kinetics method. The resulting time and concentrations for each species are then plotted using the matplotlib library, with the time axis labeled as ‘Time (s)’ and the concentration axis labeled as ‘Concentration (mol/L)’. The resulting plot shows how the concentrations of benzene and phenol change over time.