Skip to content

Instantly share code, notes, and snippets.

@ZGainsforth
Last active February 2, 2016 19:53
Show Gist options
  • Select an option

  • Save ZGainsforth/4861d174a8324d5d3c2e to your computer and use it in GitHub Desktop.

Select an option

Save ZGainsforth/4861d174a8324d5d3c2e to your computer and use it in GitHub Desktop.
Example plotting ternary diagrams in python using image files as source data.
from matplotlib import pyplot
import ternary
import numpy as np
## Boundary and Gridlines
scale = 100
figure, tax = ternary.figure(scale=scale)
# Draw Boundary and Gridlines
tax.boundary(linewidth=2.0)
tax.gridlines(color="blue", multiple=10) # Every 5th gridline, can be a float
# Set Axis labels and Title
fontsize = 20
tax.set_title("Simplex Boundary and Gridlines", fontsize=fontsize)
tax.left_axis_label("Mg", fontsize=fontsize)
tax.bottom_axis_label("Fe", fontsize=fontsize)
tax.right_axis_label("Si", fontsize=fontsize)
# Set ticks
tax.ticks(axis='lbr', linewidth=1)
# Remove default Matplotlib Axes
tax.clear_matplotlib_ticks()
#Points = (19.2,22.0,9.5)
#tax.scatter(Points, color='red')
Fe=pyplot.imread('Fe-K.tif')
Mg=pyplot.imread('Mg-K.tif')
Si=pyplot.imread('SiMask.tif')
points=np.array([np.ravel(Mg), np.ravel(Fe), np.ravel(Si)]).T.astype('float')
#Points = [p[0]/np.sum(p[0]) for p in points]
Points = [p/np.sum(p)*100 for p in points if p[2] < 7000]#if p[0] > 2000]
v1 = Points[0]/(Points[0]+Points[1])
print 'mean Mg/(Mg+Fe): %g \n' % np.mean(v1)
print 'std Mg/(Mg+Fe): %g \n' % np.std(v1)
v2 = (Points[0]+Points[1])/Points[2]
print 'mean (Mg+Fe)/Si: %g \n' % np.mean(v2)
print 'std (Mg+Fe)/Si: %g \n' % np.std(v2)
#print Points
CenterPoint = [(19.2,9.5,22.0)]
#CenterPoint = [(10,30,60)]
CenterPoint = CenterPoint/np.sum(CenterPoint)*100
#print points
tax.scatter(Points, marker='s', color='red', label="Point analyses")
tax.scatter(CenterPoint, marker='s', color='blue', label="Whole object")
pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment