2014-10-20 89 views
0

我有一个算法可以由两个参数控制,所以现在我想根据这些参数绘制算法的运行时间。在三维图中连接点

我的代码:

from matplotlib import pyplot 
import pylab 
from mpl_toolkits.mplot3d import Axes3D 

fig = pylab.figure() 
ax = Axes3D(fig) 

sequence_containing_x_vals = [5,5,5,5,10,10,10,10,15,15,15,15,20,20,20,20] 
sequence_containing_y_vals = [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4] 
sequence_containing_z_vals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] 

ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals) 

pyplot.show() 

这将绘制在空间中的所有点,但我希望他们连接,并有这样的事情:

Example plot

(着色将是不错,但不需要)

回答

0

要绘制曲面,您需要使用plot_surface,并将数据作为常规二维数组(这反映了x-y平面的2D几何形状)。通常使用meshgrid用于此,但由于您的数据已经有适当重复的x和y值,因此您只需对其进行重塑。我用numpy reshape做了这个。

from matplotlib import pyplot, cm 
from mpl_toolkits.mplot3d import Axes3D 
import numpy as np 

fig = pyplot.figure() 
ax = Axes3D(fig) 

sequence_containing_x_vals = np.array([5,5,5,5,10,10,10,10,15,15,15,15,20,20,20,20]) 
X = sequence_containing_x_vals.reshape((4,4)) 

sequence_containing_y_vals = np.array([1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4]) 
Y = sequence_containing_y_vals.reshape((4,4)) 

sequence_containing_z_vals = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) 
Z = sequence_containing_z_vals.reshape((4,4)) 

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.hot) 

pyplot.show() 

enter image description here

注意X, Y = np.meshgrid([1,2,3,4], [5, 10, 15, 20])可以得到同样的XY上面一样,但更容易。

当然,这里显示的曲面只是一个平面,因为您的数据与z = x + y - -5一致,但是这种方法可以使用泛型曲面,正如可以在许多matplotlib surface示例中看到的那样。