from numpy import linspace,meshgrid,column_stack import matplotlib.cm as cmap import matplotlib.pyplot as plt def plane(coord,a,b,c): """Function to define a plane""" return c-(coord[0]*a+coord[1]*b) coeefs=[1,-0.5,-1] col=linspace(-10,10,6) X,Y=meshgrid(col,col) Z=plane((X,Y),*coeefs)+normal(size=X.shape,scale=7.0) d=Data(column_stack((X.ravel(),Y.ravel(),Z.ravel())),filename="Fitting a Plane",setas="xyz") d.column_headers=["X","Y","Z"] d.figure(projection="3d") d.plot_xyz(plotter="scatter",c=cmap.jet(d.z)) d.curve_fit(plane,[0,1],2,result=True) d.setas="xy.z" d.plot_xyz(linewidth=0,cmap=cmap.jet) txt="$z=c-ax+by$\n" txt+="\n".join([d.format("plane:{}".format(k),latex=True) for k in ["a","b","c"]]) ax=plt.gca(projection="3d") ax.text(15,5,-50,txt) d.draw()
# Create some points approximately spherical distribution p=uniform(low=-pi/2,high=pi/2,size=250) q=uniform(low=-pi,high=pi,size=250) r=normal(loc=3.0,size=250,scale=0.5) x,y,z=transform(r,q,p) x+=3.0 y-=4.0 z+=2.0 # Construct the DataFile object d=Data(column_stack((x,y,z)),setas="xyz",filename="Best fit sphere") d.template.fig_width=5.2 d.template.fig_height=5.0 # Square aspect ratio d.plot_xyz(plotter="scatter") #curve_fit does the hard work popt,pcov=d.curve_fit(sphere,(0,1,2),zeros_like(d.x)) # This manually constructs the best fit sphere a,b,c,r=popt p=linspace(-pi/2,pi/2,16) q=linspace(-pi,pi,31) P,Q=meshgrid(p,q) R=ones_like(P)*r x,y,z=transform(R,Q,P) x+=a y+=b z+=c
def plane(coord, a, b, c): """Function to define a plane""" return c - (coord[0] * a + coord[1] * b) coeefs = [1, -0.5, -1] col = linspace(-10, 10, 6) X, Y = meshgrid(col, col) Z = plane((X, Y), *coeefs) + normal(size=X.shape, scale=7.0) d = Data( column_stack((X.ravel(), Y.ravel(), Z.ravel())), filename="Fitting a Plane", setas="xyz", ) d.column_headers = ["X", "Y", "Z"] d.figure(projection="3d") d.plot_xyz(plotter="scatter") popt, pcov = d.curve_fit(plane, [0, 1], 2, result=True) d.setas = "xy.z" d.plot_xyz(linewidth=0, cmap=cmap.jet) txt = "$z=c-ax+by$\n" txt += "\n".join([d.format("plane:{}".format(k), latex=True) for k in ["a", "b", "c"]]) ax = plt.gca(projection="3d") ax.text(15, 5, -50, txt) d.draw()
"""Function to define a plane""" return c - (coord[0] * a + coord[1] * b) coeefs = [1, -0.5, -1] col = linspace(-10, 10, 8) X, Y = meshgrid(col, col) Z = plane((X, Y), *coeefs) + normal(size=X.shape, scale=2.0) d = Data( column_stack((X.ravel(), Y.ravel(), Z.ravel())), filename="Fitting a Plane", setas="xyz", ) d.column_headers = ["X", "Y", "Z"] d.figure(projection="3d") d.plot_xyz(plotter="scatter") popt, pcov = d.curve_fit(plane, [0, 1], 2, result=True) d.setas = "xy.z" d.plot_xyz(linewidth=0, cmap=cmap.jet) txt = "$z=c-ax+by$\n" txt += "\n".join( [d.format("plane:{}".format(k), latex=True) for k in ["a", "b", "c"]]) ax = plt.gca(projection="3d") ax.text(15, 5, -50, txt) d.draw()
# Create some points approximately spherical distribution p = uniform(low=-pi / 2, high=pi / 2, size=250) q = uniform(low=-pi, high=pi, size=250) r = normal(loc=3.0, size=250, scale=0.5) x, y, z = transform(r, q, p) x += 3.0 y -= 4.0 z += 2.0 # Construct the DataFile object d = Data(column_stack((x, y, z)), setas="xyz", filename="Best fit sphere") d.template.fig_width = 5.2 d.template.fig_height = 5.0 # Square aspect ratio d.plot_xyz(plotter="scatter") # curve_fit does the hard work popt, pcov = d.curve_fit(sphere, (0, 1, 2), zeros_like(d.x)) # This manually constructs the best fit sphere a, b, c, r = popt p = linspace(-pi / 2, pi / 2, 16) q = linspace(-pi, pi, 31) P, Q = meshgrid(p, q) R = ones_like(P) * r x, y, z = transform(R, Q, P) x += a y += b z += c
"""3D surface plot example.""" from Stoner import Data import numpy as np import matplotlib.cm x, y = np.meshgrid(np.linspace(-2, 2, 100), np.linspace(-2, 2, 100)) x = x.ravel() y = y.ravel() z = np.cos(4 * np.pi * np.sqrt(x ** 2 + y ** 2)) * np.exp(-np.sqrt(x ** 2 + y ** 2)) p = Data() p = p & x & y & z p.column_headers = ["X", "Y", "Z"] p.setas = "xyz" p.plot_xyz(cmap=matplotlib.cm.jet) p.title = "Surface plot"