回归平面颜色不均匀?

问题描述 投票:0回答:1

我不明白为什么回归平面的颜色不一样。 这里是代码和输出:

import numpy as np
import pandas as pandas
import statsmodels.api as sm
import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D

v = [1000, 1200, 1000, 900, 1500, 1000, 1400, 1500, 1500, 1600, 1100, 1300, 1000, 1600, 1600, 1600, 1600, 2200, 1600, 2000, 1600, 2000, 2100, 1600, 1600, 2000, 2000, 2000, 1600, 2000, 2100, 2000, 1600, 1600, 1600, 2500]
w = [790, 1160, 929, 865, 1140, 929, 1109, 1365, 1112, 1150, 980, 990, 1112, 1252, 1326, 1330, 1365, 1280, 1119, 1328, 1584, 1428, 1365, 1415, 1415, 1465, 1490, 1725, 1523, 1705, 1605, 1746, 1235, 1390, 1405, 1395]
c = [99, 95, 95, 90, 105, 105, 90, 92, 98, 99, 99, 101, 99, 94, 97, 97, 99, 104, 104, 105, 94, 99, 99, 99, 99, 102, 104, 114, 109, 114, 115, 117, 104, 108, 109, 120]

# Create the plot
fig=plt.figure()
ax = fig.add_subplot(111,projection='3d')

z=np.array(c)
x=np.array(v)
y=np.array(w)

# Add the data points
ax.scatter(x, y, z, color='red')

# Fit a plane using np.linalg.lstsq
A = np.vstack([x, y, np.ones_like(x)]).T
plane_coef, _, _, _ = np.linalg.lstsq(A, z, rcond=None)

# Create a meshgrid for the plane
x_plane, y_plane = np.meshgrid(x, y)
z_plane = plane_coef[0] * x_plane + plane_coef[2] * y_plane + plane_coef[2]

# Add the regression plane
ax.plot_surface(x_plane, y_plane, z_plane, zorder=15 ,color='c', alpha=0.01)

# Add labels and title
ax.set_xlabel('weight')
ax.set_ylabel('volume')
ax.set_zlabel('Co2')
plt.title('Multiple Linear Regression')

for ii in np.arange(-60,60,45):
    ax.view_init(elev=32, azim=ii)
    fig.savefig('gif_image%d.png' % ii)

fig.tight_layout()

plt.show()

output

我想要的颜色是无刻度的盐色或深色阿拉斯色。

python matplotlib matplotlib-3d
1个回答
1
投票

由于 x 和 y 数组不是严格递增(或严格递减),因此

np.meshgrid(x, y)
创建了一个高度自重叠的网格。由于透明度 (alpha),重叠较多的区域会变得更暗。

您可能还想更改

z_plane = plane_coef[0] * x_plane + plane_coef[2] * y_plane + plane_coef[2]
,因为它不使用
plane_coef[1]
(并且使用
plane_coef[2]
两次)。

请注意,matplotlib 不执行“真正的”3D,它以层的形式绘制元素,其中一组点被视为“一个元素”。 Matplotlib 的 3D FAQ 建议在需要真正的 3D 时使用mayavi 等库。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

v = [1000, 1200, 1000, 900, 1500, 1000, 1400, 1500, 1500, 1600, 1100, 1300, 1000, 1600, 1600, 1600, 1600, 2200, 1600, 2000, 1600, 2000, 2100, 1600, 1600, 2000, 2000, 2000, 1600, 2000, 2100, 2000, 1600, 1600, 1600, 2500]
w = [790, 1160, 929, 865, 1140, 929, 1109, 1365, 1112, 1150, 980, 990, 1112, 1252, 1326, 1330, 1365, 1280, 1119, 1328, 1584, 1428, 1365, 1415, 1415, 1465, 1490, 1725, 1523, 1705, 1605, 1746, 1235, 1390, 1405, 1395]
c = [99, 95, 95, 90, 105, 105, 90, 92, 98, 99, 99, 101, 99, 94, 97, 97, 99, 104, 104, 105, 94, 99, 99, 99, 99, 102, 104, 114, 109, 114, 115, 117, 104, 108, 109, 120]

# Create the plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

z = np.array(c)
x = np.array(v)
y = np.array(w)

# Add the data points
ax.scatter(x, y, z, color='red')

# Fit a plane using np.linalg.lstsq
A = np.vstack([x, y, np.ones_like(x)]).T
plane_coef, _, _, _ = np.linalg.lstsq(A, z, rcond=None)

# Create a meshgrid for the plane
####x_plane, y_plane = np.meshgrid(np.linspace(x.min(), x.max(), 10), np.linspace(y.min(), y.max(), 10))
x_plane, y_plane = np.meshgrid([x.min(), x.max()], [y.min(), y.max()])
z_plane = plane_coef[0] * x_plane + plane_coef[1] * y_plane + plane_coef[2]

# Add the regression plane
ax.plot_surface(x_plane, y_plane, z_plane, zorder=15, color='c', alpha=0.3)

# Add labels and title
ax.set_xlabel('weight')
ax.set_ylabel('volume')
ax.set_zlabel('Co2')
plt.title('Multiple Linear Regression')

fig.tight_layout()

plt.show()

plane in 3D

© www.soinside.com 2019 - 2024. All rights reserved.