我正在尝试创建适合数据集的线性模型的3D图。我可以在R中相对容易地做到这一点,但我真的很努力地在Python中做同样的事情。使用Matplotlib在3D中绘制线性模型
这里就是我在Python进行:以下是我在R来
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.formula.api as sm
csv = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0)
model = sm.ols(formula='Sales ~ TV + Radio', data = csv)
fit = model.fit()
fit.summary()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(csv['TV'], csv['Radio'], csv['Sales'], c='r', marker='o')
xx, yy = np.meshgrid(csv['TV'], csv['Radio'])
# Not what I expected :(
# ax.plot_surface(xx, yy, fit.fittedvalues)
ax.set_xlabel('TV')
ax.set_ylabel('Radio')
ax.set_zlabel('Sales')
plt.show()
什么我做错了,我该怎么办呢?
谢谢。
这类作品,但由此产生的平面非常糟糕绘制。我感到困惑的是,似乎没有更好更简单的方法来做到这一点。 – ivanmp 2014-10-17 21:14:53
不好怎么样?有许多参数可以提供给plot_surface。如果你想要3D,但你可能想看看mayavi。 – mdurant 2014-10-17 21:25:31
这是http://imgur.com/A5w55U6是我得到的(我删除了'color ='None')。我正在寻找这样的东西:http://stackoverflow.com/questions/15229896/matplotlib-3d-plots-combining-scatter-plot-with-surface-plot。顺便说一句,感谢您的帮助。 – ivanmp 2014-10-17 21:29:09