python - How can I plot hysteresis in matplotlib? -
i trying plot development of pitchfork bifurcation on time. relationship between x , y starts off approximately linear, ends being sigmoidal s shape. final relationship not function; there multiple y values values of x.
matplotlib nice wire frames surface plots, these surface plots don't seem able handle non-functions.
there way of plotting surface of relationship? (if possible don't want solid shape.)
at moment data in 0 arrays 1s indicate approximation location of surface. i've included small sample data set, , sample code plot of location. how 'join dots'?
actual data sets larger (500x200x200) , varied, need develop flexible system.
this final figure might like:

from reading mplot3d documentation here seems may need convert data 2d arrays. if case please provide method this, , if possible please tell me these arrays represent.
i appreciate comment/suggestions advance this.
import numpy np mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot plt sample_data = np.array([ [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]] ] ) xs, ys, zs = [],[],[] g in xrange(np.shape(sample_data)[0]): row in xrange(np.shape(sample_data)[1]): col in xrange(np.shape(sample_data)[2]): if sample_data[g][row][col] == 1: xs.append(g) ys.append(col) zs.append(row) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs, ys, zs) plt.show() 
as suggested mrcl, in matplotlib can use trisurf. however, have provide own triangles delaunay won't work on 2d projection of points.
to build triangulation, suggest build parametric representation of surfece (in terms of s, t) , triangulate in space (s, t).
it give this

exemple based on code below (as data coarse, added bit of interpolation):
import numpy np mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot plt import matplotlib.tri mtri matplotlib import cm sample_data = np.array([ [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1.]], [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]] ] ) xs, ys, zs = [],[],[] g in xrange(np.shape(sample_data)[0]): row in xrange(np.shape(sample_data)[1]): col in xrange(np.shape(sample_data)[2]): if sample_data[g][row][col] == 1: xs.append(g) ys.append(col) zs.append(row) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs, ys, zs) xs = np.asarray(xs) ys = np.asarray(ys) zs = np.asarray(zs) def re_ordinate(x, y): ord = np.arange(np.shape(x)[0]) iter = true itermax = 10 n_iter = 0 while iter , n_iter < itermax: n_iter += 1 dist1 = (x[0:-2] - x[1:-1])**2 + (y[0:-2] - y[1:-1])**2 dist2 = (x[0:-2] - x[2:])**2 + (y[0:-2] - y[2:])**2 swap = np.argwhere(dist2 < dist1) s in swap: s += 1 t = x[s] x[s] = x[s+1] x[s+1] = t t = y[s] y[s] = y[s+1] y[s+1] = t t = ord[s] ord[s] = ord[s+1] ord[s+1] = t return ord / float(np.size(ord, 0)) # building parametrisation of surface s = np.zeros(np.shape(xs)[0]) t = np.zeros(np.shape(xs)[0]) begin = 0 end = 0 g in xrange(np.shape(sample_data)[0]): cut = np.argwhere(xs==g).flatten() begin = end end += np.size(cut, 0) x_loc = xs[cut] y_loc = ys[cut] z_loc = zs[cut] s[begin: end] = g / float(np.size(sample_data, 0)) t[begin: end] = re_ordinate(y_loc, z_loc) #ax.plot(x_loc, y_loc, z_loc, color="grey") triangles = mtri.triangulation(s, t).triangles refiner = mtri.uniformtrirefiner(mtri.triangulation(s, t)) subdiv = 2 _, x_refi = refiner.refine_field(xs, subdiv=subdiv) _, y_refi = refiner.refine_field(ys, subdiv=subdiv) triang_param, z_refi = refiner.refine_field(zs, subdiv=subdiv) #triang_param = refiner.refine_triangulation()#mtri.triangulation(xs, ys, triangles) #print triang_param.triangles triang = mtri.triangulation(x_refi, y_refi, triang_param.triangles) ax.plot_trisurf(triang, z_refi, cmap=cm.jet, lw=0.) plt.show()
Comments
Post a Comment