Python 3d Plot With Data, Error Occured
I was trying to plot a 3D diagram with manual input data (x,y,z) using ax.plot_surface. Even though I used a similar code I found online, I still got some errors. 'Warning (from w
Solution 1:
Due to the interpolation on the grid, the outmost points of the resulting array are nan
(i.e. first and last column & first and last row). While nan
values can be ignored for plotting, they are unfortunately not for producing the colorization. In order to be able to use a colormap, an array without nan values should be provided (this is strictly only true for 3D plots).
While there are in general several options like replacing values and masking, here the easiest is to leave out the rows and columns from plotting. I.e. instead of ax.plot_surface(xi, yi, zi, cmap="hot")
you can use
ax.plot_surface(xi[1:-1,1:-1], yi[1:-1,1:-1], zi[1:-1,1:-1], cmap="hot")
Complete example:
frommpl_toolkits.mplot3dimportAxes3Dimportmatplotlib.pyplotaspltimportnumpyasnpimportscipy.interpolatefig=plt.figure()ax=fig.add_subplot(111,projection='3d')x= [1043.797,621.694,203.275,-213.783,-627.143,-1045.474,-1045.474,-628.403,-213.783,0.42,203.278,621.697,1043.801,1042.545,621.701,203.282,0.426,-213.778,-628.397,-1045.467,-0.834,1043.804,621.701,203.292,0.434,-213.77,-628.393,-1045.462,-1045.464,-628.395,-213.772,-0.829,203.29,621.707,1043.812,1043.807,621.706,203.287,-213.775,-628.398,-1045.466]
y= [-1210.936,-1211.146,-1210.931,-1210.819,-1210.916,-1210.916,-727.082,-726.768,-726.776,-726.883,-726.887,-727.101,-726.68,-242.741,-243.059,-242.846,-242.841,-242.732,-242.723,-243.037,19.801,241.133,241.025,241.248,241.148,241.154,241.167,241.07,725.216,725.208,724.565,725.401,724.976,724.97,724.975,1209.226,1209.324,1209.328,1209.338,1209.559,1209.254]
z= [3753.086,4054.802,4101.778,4064.706,3844.414,3614.887,4156.525,4184.521,4284.536,4269.797,4273.816,4298.024,4264.16,4224.935,4188.664,4200.863,4210.243,4164.851,4143.223,4148.073,3980.13,4094.025,4203.862,4260.099,4238.935,4233.248,4186.161,4072.293,4021.05,4311.022,4351.636,4359.61,4385.24,4382.892,4169.055,3927.979,4226.974,4237.096,4180.779,4082.677,3739.785]
x=np.asarray(x)y=np.asarray(y)N=100xi=np.linspace(x.min(),x.max(),N)yi=np.linspace(y.min(),y.max(),N)zi=scipy.interpolate.griddata((x,y),z,(xi[None,:],yi[:,None]),method='cubic')xi,yi=np.meshgrid(xi,yi)surf=ax.plot_surface(xi[1:-1,1:-1],yi[1:-1,1:-1],zi[1:-1,1:-1],cmap=plt.cm.hot)plt.show()
Post a Comment for "Python 3d Plot With Data, Error Occured"