4

I am trying to plot a 3D surface plot using a data having 4 columns ie it would x , y ,z , c. the data shown below -

0.452  -172.799  -172.800 0.000000
0.452  -172.799  -158.400 -9.305322
0.452  -172.799  -144.000 -12.062071
0.452  -172.799  -129.600 -9.008698
0.452  -172.799  -115.200 -5.402975
0.452  -172.799  -100.800 -3.957608
0.452  -172.799  -86.400 -5.113347
0.452  -172.799  -72.000 -4.784993
0.452  -172.799  -57.600 -6.659095
0.452  -172.799  -43.200 -4.405937
0.452  -172.799  -28.800 -6.110385
0.452  -172.799  -14.400 -5.896424
0.452  -172.799  -0.001 -4.405937
0.452  -172.799  14.399 -6.489442
0.452  -172.799  28.799 -5.662057
0.452  -172.799  43.199 -7.710180
0.452  -172.799  57.599 -7.710180
0.452  -172.799  71.999 -8.011659
0.452  -172.799  86.399 -6.489442
0.452  -172.799  100.799 -6.817796
0.452  -172.799  115.199 -5.662057
0.452  -172.799  129.599 -5.402975
0.452  -172.799  143.999 -5.662057
0.452  -172.799  158.399 -3.957608
0.452  -172.799  172.799 -2.701488
0.615  -172.799  -172.800 -8.444175
0.615  -172.799  -158.400 -13.183417`

The first three columns represent the x,y,z and 4th column c would be the colorbar ie the surface color would be given according to c.

for now i had tried doing few simple codes but still getting errors. what i tried out are -

import pylab as p
import numpy as np 
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.cm as cm
data = np.genfromtxt('test1.pmf')
x=data[:,1]
y=data[:,2]
h=data[:,3]
z=data[:,0]
datamin=min(h)
datamax=max(h)
fig=p.figure()
ax=fig.add_subplot(111, projection='3d')
pmf = ax.plot_surface(x, y, z, facecolors=cm.ocean(h))
p.colorbar(pmf)
p.show()    

i got an error such as

pmf = ax.plot_surface(x, y, z, facecolors=cm.ocean(h))
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3/axes3d.py",line663,in            
plot_surface rows, cols = Z.shape ValueError: need more than 1 value to unpack

any idea where am doing wrong in this?

Thank you so much!

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
xyz123
  • 41
  • 3
  • The `x`-coordinates in the data that you provide does not change (second column). How do you expect that to be a surface? – hitzg Nov 11 '14 at 21:22

1 Answers1

0

The plot_surface function requires 2D arrays as an arguments. In your code snippet Z variable is a 1D array, so Z.shape couldn't be unpacked into two elements (rows and cols).

I believe, that something like this can be correct:

x, y = np.meshgrid(x, y)
z = z.reshape(x.shape)

See the example

erthalion
  • 3,094
  • 2
  • 21
  • 28
  • i tried giving a meshgrid, since my data been 4D array i still get an error, the changes i made to the script- – xyz123 Nov 11 '14 at 07:08
  • i tried giving a meshgrid, since my data been 4D array i still get an error, the changes i made to the script- `X, Y = np.meshgrid(xi, yi) Z = griddata(x, y, z, xi, yi)` still i get an error saying `TypeError: unhashable type: 'numpy.ndarray'` – xyz123 Nov 11 '14 at 07:22
  • Can you update your question and show the new code & new error backtrace? – erthalion Nov 12 '14 at 14:56