def __init__(self, ax, ellabels=['A', 'B', 'C', 'D'], offset=None, nintervals=10., outlinealpha=0.4): self.outlinealpha = outlinealpha self.ternaryplot = TernaryPlot(ax, outline=False) self.ax = ax self.ax.set_xlim(-.1, 2.6) self.ax.set_ylim(-.1, 3.**.5 / 2 + .1) self.cartendpts = numpy.float32([[0, 0], [.5, numpy.sqrt(3.) / 2.], [1, 0]]) self.ellabels = ellabels self.outline() self.nint = 1. * nintervals self.delta = 1. / self.nint self.patch_xyc = lambda x, y, c, **kwargs: self.ax.add_patch( CirclePolygon((x, y), radius=self.delta / 3.**.5, resolution=6, color=c, **kwargs)) if offset is None: self.offset = self.delta self.qindsfortern_skipind = [ [1, 2, 3], [2, 3, 0], [3, 0, 1], [0, 1, 2] ] #sets the order of elements assuming mirror over y for skipA and skipC
def make10ternaxes(ellabels=['A', 'B', 'C', 'D']): ax_xc=[] ax_yc=[] xcdel=[.22, .2, .1, .04, .04, .04, .03, .03, .03, .03] ax_yc=[.5, .6, .36, .65, .49, .33, .68, .55, .43, .31, .2] for i in range(10): if i==0: ax_xc+=[xcdel[i]] else: ax_xc+=[ax_xc[-1]+xcdel[i]] #ax_yc+=[.5+((i%2)*2.-1.)*((i>0)*.1+.072*i/10)] shape1=numpy.array([.35, 1.]) fig=pylab.figure(figsize=(12, 8)) axl=[] for i, xc, yc in zip(range(1, 11), ax_xc, ax_yc): w, l=shape1/i axl+=[fig.add_axes([xc-w/2, yc-l/2, w, l])] stpl=[] for count, ax in enumerate(axl): stp=TernaryPlot(ax, ellabels=ellabels[:3]) stp.label() stpl+=[stp] if count<9: stp.ax.text(.3, .8, '%s$_{%.2f-%.2f}$' %(ellabels[3], (count*.1), ((count+1)*.1)-.01), ha='right', va='center') else: stp.ax.text(.3, .8, '%s$_{%.2f-%d}$' %(ellabels[3], (count*.1), 1), ha='right', va='center') return axl, stpl
def __init__(self, ax, ellabels=['A', 'B', 'C', 'D'], offset=None, nintervals=10., outlinealpha=0.2): self.outlinealpha=outlinealpha self.nint=1.*nintervals self.delta=1./self.nint self.ternaryplot=TernaryPlot(ax, outline=False) self.ax=ax self.offset=offset #self.ax.set_xlim(-.1, 2.6) #self.ax.set_ylim(-.1, 3.**.5/2+.1) self.ax.set_ylim(-.1-3.**.5/4., .1+3.**.5/4.) self.cartendpts=numpy.float32([[0, 0], [.5, numpy.sqrt(3.)/2.], [1, 0]]) self.ellabels=ellabels self.scalefcn=lambda ntern:(self.nint-ntern)/self.nint shift=0. self.shift_ntern=[] perminds=[0, 1, 2] self.perminds_ntern=[] for ntern in range(int(self.nint)+1): self.shift_ntern+=[shift] shift+=self.delta*1.5+0.5*self.scalefcn(ntern) self.perminds_ntern+=[perminds] #if ntern%2==0: perminds=[perminds[i] for i in [1, 2, 0]] # else: # perminds=[perminds[i] for i in [1, 0, 2]] self.ax.set_xlim(-.1, shift+self.delta*1.5+1.*self.scalefcn(ntern)+.1) self.patch_xyc=lambda x, y, c, **kwargs:self.ax.add_patch(CirclePolygon((x, y),radius=self.delta/3.**.5,resolution=6, color=c, **kwargs)) self.outline() if offset is None: self.offset=self.delta
def __init__(self, ax, ellabels=['A', 'B', 'C', 'D'], offset=None, nintervals=10., outlinealpha=0.2, patchscale=1.): self.outlinealpha=outlinealpha self.nint=1.*nintervals self.delta=1./self.nint self.ternaryplot=TernaryPlot(ax, outline=False) self.ax=ax self.ax.set_aspect(1.) #self.ax.set_xlim(-.1, 2.6) #self.ax.set_ylim(-.1, 3.**.5/2+.1) self.ax.set_ylim(-.1-3.**.5/4., .1+3.**.5/4.) self.cartendpts=numpy.float32([[0, 0], [.5, numpy.sqrt(3.)/2.], [1, 0]]) self.ellabels=ellabels self.scalefcn=lambda nshell:(self.nint-4.*nshell)/self.nint shift=0. self.shift_nshell=[] for nshell in range(int(self.nint//4)+1): self.shift_nshell+=[shift] shift+=self.delta*2.+2.*self.scalefcn(nshell) self.ax.set_xlim(-.1, shift+self.delta+1.*self.scalefcn(nshell)) self.patch_xyc=lambda x, y, c, **kwargs:self.ax.add_patch(CirclePolygon((x, y),radius=patchscale*self.delta/3.**.5,resolution=6, color=c, **kwargs)) if outlinealpha>0: self.outline() if offset is None: self.offset=self.delta self.qindsfortern_skipind=[[1, 2, 3], [2, 3, 0], [3, 0, 1], [0, 1, 2]]#sets the order of elements assuming mirror over y for skipA and skipC
def make9of100ternaxes(ellabels=['A', 'B', 'C', 'D'], fig=None): if fig is None: fig = pylab.figure(figsize=(8, 6)) axl = [] for i in [1, 4, 7, 2, 5, 8, 3, 6, 9]: axl += [fig.add_subplot(3, 3, i)] fig.subplots_adjust(left=0.05, right=.8, bottom=.05, top=.95, hspace=.08, wspace=.08) stpl = [] xpos = [.27] * 9 for count, (ax, xp) in enumerate(zip(axl, xpos)): stp = TernaryPlot(ax, ellabels=ellabels[:3], offset=.03) stp.label(fontsize=15) #,fontdict={'fontname':'Times New Roman'}) stpl += [stp] #if count<4: stp.ax.text(xp, .8, '%s$_{%.2f}$' % (ellabels[3], (count * .01)), ha='right', va='center', fontsize=15) #else: # stp.ax.text(xp, .8, '%s$_{%.2f-%d}$' %(ellabels[3], (count*.2), 1), ha='right', va='center', fontsize=17) return axl, stpl
def make30ternaxes(ellabels=['A', 'B', 'C', 'D'], fig=None): if fig is None: fig = pylab.figure(figsize=(14, 8)) axl = [] delw = .02 delh = .04 yscalefct = [1.] * 6 #[.7, .7, .7, .7, .7, .7] xscalefct = [1., .9, .67, .67, .67, .67] npercol = numpy.array([3, 4, 5, 6, 6, 6]) colw = (1. / npercol) colw[0] *= .85 colw[1] *= .9 colw = (colw / colw.sum()) * (.9 - len(npercol) * delw) #colw/=colw.sum() plotcount = 0 cumwidth = 0 for nc, cw, xsf, ysf in zip(npercol, colw, xscalefct, yscalefct): w = xsf * cw h = ysf * ((1. - delh * (nc + 1)) / nc) cumwidth += cw + delw for ic in range(nc): axl += [ fig.add_axes([ cumwidth - cw / 2. - w / 2., 1. - (delh + ic * (h + delh)) - h, w, h ]) ] stpl = [] xpos = [.27] * 30 # xpos[0:3]=[.35, .29, .28] # xpos[-1]=.26 for count, (ax, xp) in enumerate(zip(axl, xpos)): stp = TernaryPlot(ax, ellabels=ellabels[:3], offset=.03) stp.label(fontsize=15) #,fontdict={'fontname':'Times New Roman'}) stpl += [stp] if count < 29: stp.ax.text(xp, .8, '%s$_{%.2f}$' % (ellabels[3], (count * .0333)), ha='right', va='center', fontsize=17) else: stp.ax.text(xp, .8, '%s$_{%.2f-%d}$' % (ellabels[3], (count * .0333), 1), ha='right', va='center', fontsize=17) return axl, stpl
def make10ternaxes(ellabels=['A', 'B', 'C', 'D'], fig=None, fontsize=17): if fig is None: fig = pylab.figure(figsize=(12, 8)) ax_xc = [] ax_yc = [] xcdel = [.18, .19, .065, .1, .04, .05, .055, .03, .02, .02] ax_yc = [.49, .68, .30, .74, .48, .24, .78, .58, .39, .21] for i in range(10): if i == 0: ax_xc += [xcdel[i]] else: ax_xc += [ax_xc[-1] + xcdel[i]] #ax_yc+=[.5+((i%2)*2.-1.)*((i>0)*.1+.072*i/10)] shape1 = numpy.array([.35, 1.]) scales = [.82, 0.51, 0.39, 0.3, 0.22, 0.2, 0.17, 0.14, 0.11, 0.09] axl = [] for sc, xc, yc in zip(scales, ax_xc, ax_yc): w, l = shape1 * sc axl += [fig.add_axes([xc - w / 2, yc - l / 2, w, l])] stpl = [] xpos = [.27] * 10 xpos[0:3] = [.38, .36, .33] xpos[-1] = .18 for count, (ax, xp) in enumerate(zip(axl, xpos)): stp = TernaryPlot(ax, ellabels=ellabels[:3], offset=.03) if not fontsize is None: stp.label( fontsize=fontsize) #,fontdict={'fontname':'Times New Roman'}) stpl += [stp] if not fontsize is None: if count < 9: stp.ax.text(xp, .8, '%s$_{%.2f-%.2f}$' % (ellabels[3], (count * .1), ((count + 1) * .1) - .01), ha='right', va='center', fontsize=fontsize) else: stp.ax.text(xp, .8, '%s$_{%.2f-%d}$' % (ellabels[3], (count * .1), 1), ha='right', va='center', fontsize=fontsize) return axl, stpl
def make4ternaxes(ellabels=['A', 'B', 'C', 'D'], fig=None): if fig is None: fig = pylab.figure(figsize=(12, 5)) w = .21 h = .9 axl = [] for i in range(4): axl += [fig.add_axes([.02 + i * (w + .01), (1. - h) / 2., w, h])] stpl = [] for i, ax in zip([3, 2, 1, 0], axl): eltemp = copy.copy(ellabels) eltemp.pop(i) stp = TernaryPlot(ax, ellabels=eltemp) stp.label() stpl += [stp] return axl, stpl
def make10ternaxes(ellabels=['A', 'B', 'C', 'D'], fig=None): if fig is None: fig=pylab.figure(figsize=(12, 8)) ax_xc=[] ax_yc=[] xcdel=[.22, .19, .1, .04, .04, .05, .03, .03, .03, .03] ax_yc=[.5, .6, .36, .65, .49, .33, .67, .55, .445, .32] for i in range(10): if i==0: ax_xc+=[xcdel[i]] else: ax_xc+=[ax_xc[-1]+xcdel[i]] #ax_yc+=[.5+((i%2)*2.-1.)*((i>0)*.1+.072*i/10)] shape1=numpy.array([.35, 1.]) axl=[] for i, xc, yc in zip(range(1, 11), ax_xc, ax_yc): w, l=shape1/i axl+=[fig.add_axes([xc-w/2, yc-l/2, w, l])] stpl=[] xpos=[.27]*10 xpos[0:3]=[.35, .29, .28] xpos[-1]=.26 for count, (ax, xp) in enumerate(zip(axl, xpos)): stp=TernaryPlot(ax, ellabels=ellabels[:3], offset=.03) stp.label(fontsize=17)#,fontdict={'fontname':'Times New Roman'}) stpl+=[stp] if count<9: stp.ax.text(xp, .8, '%s$_{%.2f-%.2f}$' %(ellabels[3], (count*.1), ((count+1)*.1)-.01), ha='right', va='center', fontsize=17) else: stp.ax.text(xp, .8, '%s$_{%.2f-%d}$' %(ellabels[3], (count*.1), 1), ha='right', va='center', fontsize=17) return axl, stpl
Y = cmp.values / 100 #define subset Xsubset, Ysubset that is fed to the GPR model #and Xfit where fitting is done x = np.linspace(np.min(X[:, 0]), np.max(X[:, 0]), 50) y = np.linspace(np.min(X[:, 1]), np.max(X[:, 1]), 50) xv, yv = np.meshgrid(x, y) Xfit_ = np.array([xv.flatten(), yv.flatten()]) idx = [] for i in range(len(Xfit_.T)): if is_in_hull(Xfit_[:, i], X): idx.append(i) Xfit = Xfit_[:, idx] #%% Predict using gaussian process regressor kernel = RBF([1.0]) + WhiteKernel(noise_level=0.0336) gpr = GaussianProcessRegressor(kernel=kernel) gpr.fit(Xsubset, Ysubset) Ygpr, Ystd = gpr.predict(Xfit.T, return_std=True) #%% plot predictions ax = pylab.gca() stp = TernaryPlot(ax, ellabels=cmp.columns) x, y = stp.toCart(Ygpr) plt.scatter(x, y, s=10, c=Ystd) stp.label(fontsize=16) plt.colorbar() plt.show()
f=open(newpath, mode='w') f.write('\n'.join(writelines)) f.close() sys.path.append('C:/Users/Gregoire/Documents/PythonCode/ternaryplot') from myquaternaryutility import QuaternaryPlot from myternaryutility import TernaryPlot for d in dlist: c=numpy.array([d[el] for el in ['A', 'B', 'C', 'D']]) if c.sum()>0: c/=c.sum() d['compositions']=c carr=numpy.array([d['compositions'] for d in dlist]) stpq=QuaternaryPlot(111) stpq.scatter(carr) pylab.figure() for count, tv in enumerate(comps_d): stpq=TernaryPlot((4, 2, count+1)) tvnorm=[tvv/tvv.sum() for tvv in tv] stpq.scatter(tvnorm, marker='.', c='r', edgecolor='none') if count<5: ttt=tc else: ttt=tc19 stpq.scatter(ttt, marker='.', c='g', edgecolor='none') pylab.show()
13.9, 14.3, 11.3, 12.5, 12.2, 9.7, 11, 11.7, 12.2, 9.7, 10.5, 11.6, 9.7, 11.5, 9.3, 31.8, 34.4, 28.2, 29.9, 33.9, 36.3, 30.5, 33.3, 35.8, 31.2, 35.6, 34.9 ]) comps = numpy.array([au, cu, si]).T comps = numpy.array([c / c.sum() for c in comps]) aumin = comps[:, 0].min() cumin = comps[:, 1].min() simin = comps[:, 2].min() pylab.figure(figsize=(6, 3)) ax = pylab.gca() #stp = TernaryPlot(ax, ellabels=['Au', 'Cu', 'Si']) stp = TernaryPlot(ax, ellabels=['Au', 'Cu', 'Si'], minlist=[aumin, cumin, simin]) stp.grid(nintervals=10, printticklabels=[4]) stp.label(fontsize=12) stp.colorcompplot(comps, '.', markersize=9) colors = stp.color_comp_calc(comps) pylab.figure(figsize=(4, 4)) #pylab.scatter(x, y, c=colors, s=40) for xv, yv, c in zip(x, y, colors): pylab.plot([xv], [yv], '.', color=c, ms=12) pylab.gca().set_aspect(1) rmax = numpy.max(x**2 + y**2)**.5 rlab = rmax + 1.5 pylab.text(rlab, 0., 'Au', fontsize=12, color='r', ha='center', va='center') pylab.text(-0.5 * rlab,
def interwithinsegs(p0, p1, p2, p3): x0, y0 = p0 x1, y1 = p1 x2, y2 = p2 x3, y3 = p3 d = (x0 - x1) * (y2 - y3) - (y0 - y1) * (x2 - x3) x = ((x2 - x3) * (x0 * y1 - y0 * x1) - (x0 - x1) * (x2 * y3 - y2 * x3)) / d y = ((y2 - y3) * (x0 * y1 - y0 * x1) - (y0 - y1) * (x2 * y3 - y2 * x3)) / d betweentest = lambda a, b, c: (a >= min(b, c)) and (a <= max(b, c)) return numpy.array([x, y]), betweentest(x, x0, x1) and betweentest( x, x2, x3) and betweentest(y, y0, y1) and betweentest(y, y2, y3) q = TernaryPlot(111) #define these to be modified for each end member z = numpy.zeros(3, dtype='float64') ctr2 = numpy.ones(3, dtype='float64') / 2. endmembers = [] lineendpairs = [] #iterate over 4 end members and draw a line from there to center of opposing face, e.g. (0,.33,.33,.33) for i in range(3): a = copy.copy(z) a[i] = 1. b = copy.copy(ctr2) b[i] = 0. q.line(a, b, fmt='b-') q.scatter([b], c='b', s=15) endmembers += [a]