matplotlib - Plotting three lists as a surface plot in python using mplot3d -
i have 3 lists, x
,y
,z
. each piece of data associated index.
x = [1,1,1,1,2,2,2,2,3,3,3,3] y = [1,4,5,6,1,4,5,6,1,4,5,6] z = [2,6,3,6,2,7,4,6,2,4,2,3]
the x , y lists contain 3 or 4 unique values - each combination of x , y unique , has associated z value.
i need produce surface plot using .plot_surface
. know need create meshgrid
this, don't know how produce given have lists containing duplicate data, , maintaining integrity z list crucial. use tri_surf
works straight away, not quite need.
i'm using mplot3d library of course.
given scattered nature of data set, i'd suggest tri_surf
. since you're saying "it not quite [you] need", other option create meshgrid
, interpolate input points scipy.interpolate.griddata
.
import numpy np import scipy.interpolate interp import matplotlib.pyplot plt mpl_toolkits.mplot3d import axes3d x = [1,1,1,1,2,2,2,2,3,3,3,3] y = [1,4,5,6,1,4,5,6,1,4,5,6] z = [2,6,3,6,2,7,4,6,2,4,2,3] plotx,ploty, = np.meshgrid(np.linspace(np.min(x),np.max(x),10),\ np.linspace(np.min(y),np.max(y),10)) plotz = interp.griddata((x,y),z,(plotx,ploty),method='linear') fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(plotx,ploty,plotz,cstride=1,rstride=1,cmap='viridis') # or 'hot'
result:
Comments
Post a Comment