如何在Python中绘制多变量函数?

时间:2015-05-19 21:05:00

标签: python numpy matplotlib linear-regression

使用matplotlib在Python中绘制单个变量函数非常简单。但我试图在散点图中添加第三个轴,以便可以看到我的多变量模型。

这是一个示例代码段,有30个输出:

import numpy as np
np.random.seed(2)
## generate a random data set
x = np.random.randn(30, 2)
x[:, 1] = x[:, 1] * 100
y = 11*x[:,0] + 3.4*x[:,1] - 4 + np.random.randn(30) ##the model

如果这只是一个单一的变量模型我可能会使用这样的东西来生成一个情节&最合适的线:

%pylab inline
import matplotlib.pyplot as pl 
pl.scatter(x_train, y_train)
pl.plot(x_train, ols.predict(x_train))
pl.xlabel('x')
pl.ylabel('y')

多变量可视化的等价物是什么?

3 个答案:

答案 0 :(得分:6)

最常见的方法是改变散布符号的颜色和/或大小。例如:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2)

## generate a random data set
x, y = np.random.randn(2, 30)
y *= 100
z = 11*x + 3.4*y - 4 + np.random.randn(30) ##the model

fig, ax = plt.subplots()
scat = ax.scatter(x, y, c=z, s=200, marker='o')
fig.colorbar(scat)

plt.show()

enter image description here

答案 1 :(得分:4)

您可以使用mplot3d。对于散点图,您可以使用类似

的内容
NE

答案 2 :(得分:4)