Python Forum

Full Version: Python 3 Jupyter notebook ternary plot data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using the ternary library and Matplotlib to plot a heat map of gibbs free energy of mixing and a thermodynamic phase diagram for a ternary solution of drug, polymer and water across a range of ~5000 composition combinations.

Currently my code can generate these 2 plots by solving the Flory-Huggins equation for the ternary system in question, but the solutions are calculated and plotted in terms of volume fraction of each component in the system. I wish to convert the respective volume fractions to mole fractions and plot this data in the ternary diagram instead.

The format of the data used by ternary to generate the heat map is a string as follows, with the VOLUME fractions highlighted in purple (3 values inside parentheses):

{(1.0, 1.0, 98.0): 0.019617699619267185, (1.0, 2.0, 97.0): 0.012496629067697308, (1.0, 3.0, 96.0): 0.0054235597962929635, (1.0, 4.0, 95.0): -0.0016045949027236467...

To convert each volume fraction to mole fraction, I would need to perform a simple equation on each of these vol. fraction values inside the brackets
e.g.

mole fraction of drug nD = vD*rhoD/mrD/(vD*rhoD/mrD+vP*rhoP/mrP+vW*rhoW/mrW)

D = drug, P = polymer, W = water, nD = mole fraction of drug, vX = volume fraction of component, rho = density, mr = molar mass.

Is there a simple way to carryout this operation systematically across all volume fraction data points. I was thinking if I could convert this string into a dataframe it would be easier to target each component value but there are many colons, parentheses, commas etc in the data string of values so can this string even be parsed effectively? Also there is then the issue of converting it back to the appropriate format I assume?

Hope this makes sense

Thanks