【问题标题】:Issue in plotting 3d surface in python在 python 中绘制 3d 表面的问题
【发布时间】:2021-03-21 02:16:52
【问题描述】:

我有以下数据,试图绘制一个 3d 表面。

data = [[10, 10, 0.84496124031007758],
    [10, 20, 0.87209302325581395],
    [10, 30, 0.88139534883720927],
    [20, 10, 0.86201550387596892],
    [20, 20, 0.87441860465116272],
    [20, 30, 0.88992248062015500],
    [30, 10, 0.87984496124031009],
    [30, 20, 0.89922480620155043],
    [30, 30, 0.92015503875968996]]

x, y, z = zip(*data)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
plt.show()

我正在检索以下错误,

File "C:/Users/40227422/PycharmProjects/VideoDetection.py", line 29, in <module>
   ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
File "C:\Users\40227422\AppData\Local\Continuum\miniconda3\envs\tensorflow\lib\site- 
packages\mpl_toolkits\mplot3d\axes3d.py", line 1496, in plot_surface
   if Z.ndim != 2:
AttributeError: 'tuple' object has no attribute 'ndim'

提前致谢

【问题讨论】:

    标签: python numpy matplotlib plot 3d


    【解决方案1】:

    Axes3D.plot_surface的前三个参数都需要是二维数组。

    将您的电话替换为以下内容将起作用:

    ax.plot_surface(np.array(x).reshape(3,3), np.array(y).reshape(3,3), np.array(z).reshape(3,3), cmap='viridis', edgecolor='none')
    

    或者使用列表理解+解构:

    ax.plot_surface(*[np.array(d).reshape(3,3) for d in zip(*data)], cmap='viridis', edgecolor='none')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多