在 3D MatplotLib 散点图中绘制超平面

问题描述

我有以下代码。它可以绘制 3D 散点图(没有平面)。我也想添加飞机。我有 Z 轴值,但是当我尝试使用这些值时,出现以下错误 shape mismatch: objects cannot be broadcast to a single shape

如何绘制平面的 Z 轴?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

data = {
        'x': [30,48,15,25,19,11,13,43,37,30,20,41,18,39,14,26,44,36,24,28,17,45,46,40,17],'y': [38,35,31,27,32,34,38,22,21,23,34],'z': [1.57,1.6,1.64,1.67,1.75,1.79,1.87,1.95,1.96,1.99,2.01,2.34,2.45,2.47,2.54,2.67,2.85,2.92,2.94,3.29,3.4,3.43,3.52,3.54,3.63,3.69,3.82,3.99,4.35,4.37,4.92,4.94,5,5],'plane': [1,1,2,3,4,5]
        }

df = pd.DataFrame(data)

planes = [1,5]

cdict = {1: 'blue',2: 'yellow',3: 'orange',4: 'red',5: 'green'
        }

fig = plt.figure(figsize=(12,12))

ax = fig.add_subplot(111,projection='3d')

for i in planes:
    df2 = df[df['plane']==i]
    x = df2['x'].values.reshape(-1,1)
    y = df2['y'].values.reshape(-1,1)
    z = df2['z'].values.reshape(-1,1)
    
    ax.scatter(xs = x,ys = y,zs = z,label = i,c = cdict[i]
              )
    
    xx,yy = np.meshgrid(np.arange(min(x),max(x),1),np.arange(min(y),max(y),1))
    zz = np.array(np.meshgrid(np.arange(min(z),max(z),0.01)) * len(z)) #Multiply by count to have the same array size as xx and yy
    ax.plot_surface(xx,yy,zz,alpha=0.2)
    
ax.legend(loc="best")

plt.show()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)