Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中绘制多元回归3D绘图_Python_Matplotlib_3d_Regression_Mayavi - Fatal编程技术网

如何在python中绘制多元回归3D绘图

如何在python中绘制多元回归3D绘图,python,matplotlib,3d,regression,mayavi,Python,Matplotlib,3d,Regression,Mayavi,我不是科学家,所以请假设我不懂有经验的程序员的行话,也不懂复杂的科学绘图技术。Python是我所知道的唯一语言(初学者+,可能是中级) 任务:将多元回归(z=f(x,y))的结果绘制为三维图形上的二维平面(例如,我可以使用OSX的绘图实用程序,或者使用R实现) 经过一周的搜索和阅读matplotlib、seaborn和mayavi的各种文档,我终于找到了听起来很有希望的。这是我的数据和代码: 首先尝试matplotlib: 我得到的只是: 如果这很重要,我将在OSX 10.9.3上使用64位版

我不是科学家,所以请假设我不懂有经验的程序员的行话,也不懂复杂的科学绘图技术。Python是我所知道的唯一语言(初学者+,可能是中级)

任务:将多元回归(z=f(x,y))的结果绘制为三维图形上的二维平面(例如,我可以使用OSX的绘图实用程序,或者使用R实现)

经过一周的搜索和阅读matplotlib、seaborn和mayavi的各种文档,我终于找到了听起来很有希望的。这是我的数据和代码:

首先尝试matplotlib:

我得到的只是:

如果这很重要,我将在OSX 10.9.3上使用64位版本的Enthough's Canopy

如果您对我的错误有任何意见,我将不胜感激

编辑:发布最终有效的代码,以防对某人有所帮助

'''After the usual imports'''
def multiple3(tpl_lst):
    mul = []
    for tpl in tpl_lst:
        calc = (.0001*tpl[0]) + (.017*tpl[1])+ 6.166
        mul.append(calc)
    return mul

fig = plt.figure()
ax = fig.gca(projection='3d')
'''some skipped code for the scatterplot'''
X = np.arange(0, 40000, 500)
Y = np.arange(0, 40, .5)
X, Y = np.meshgrid(X, Y)
Z = multiple3(zip(X,Y))

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,cmap=cm.autumn,
                       linewidth=0, antialiased=False, alpha =.1)
ax.set_zlim(1.01, 11.01)
ax.set_xlabel(' x = IPP')
ax.set_ylabel('y = UNRP20')
ax.set_zlabel('z = DI')

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

对于matplotlib,您可以基于(缺少plt.meshgrid):


对于xy值的二维栅格,数据不是z=f(x,y),而是沿着y=x的直线。您不需要Mayavi,它可以绘制数据=f(x,y,z)。对于z=f(x,y)数据,Matplotlib很好。您能解释一下我的数据在结构上与链接页面“Python绘制3d曲面的最简单方法”上的数据有什么不同吗?我试图复制该数据的结构和类型…对于matplotlib,您可以基于(您缺少
plt.meshgrid
)@FelipeLema非常感谢,您的建议最终对我有效。你能把它作为一个答案贴出来,这样我就可以投票并接受它了吗?哦,太好了!我以为这只是一个远射!
type: <type 'numpy.ndarray'> 
X: [    0   500  1000  1500  2000  2500  3000 ….]

type: <type 'numpy.ndarray'> 
Y: [  0.    0.5   1.    1.5   2.    2.5   3.  ….]

type: <type 'numpy.ndarray'> 
Z: [  5.5272   5.5922   5.6572   5.7222   5.7872   5.8522   5.9172  ….] 
from mayavi import mlab
def multiple3_triple(tpl_lst):

X = xs
Y = ys
Z = zs


# Define the points in 3D space
# including color code based on Z coordinate.
pts = mlab.points3d(X, Y, Z, Z)

# Triangulate based on X, Y with Delaunay 2D algorithm.
# Save resulting triangulation.
mesh = mlab.pipeline.delaunay2d(pts)

# Remove the point representation from the plot
pts.remove()

# Draw a surface based on the triangulation
surf = mlab.pipeline.surface(mesh)

# Simple plot.
mlab.xlabel("x")
mlab.ylabel("y")
mlab.zlabel("z")
mlab.show()
'''After the usual imports'''
def multiple3(tpl_lst):
    mul = []
    for tpl in tpl_lst:
        calc = (.0001*tpl[0]) + (.017*tpl[1])+ 6.166
        mul.append(calc)
    return mul

fig = plt.figure()
ax = fig.gca(projection='3d')
'''some skipped code for the scatterplot'''
X = np.arange(0, 40000, 500)
Y = np.arange(0, 40, .5)
X, Y = np.meshgrid(X, Y)
Z = multiple3(zip(X,Y))

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,cmap=cm.autumn,
                       linewidth=0, antialiased=False, alpha =.1)
ax.set_zlim(1.01, 11.01)
ax.set_xlabel(' x = IPP')
ax.set_ylabel('y = UNRP20')
ax.set_zlabel('z = DI')

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()