【问题标题】:Plotly plot_trisurf isn't working with arange arraysPlotly plot_trisurf 不适用于范围数组
【发布时间】:2019-07-22 08:15:52
【问题描述】:

我基本上只是复制了 Matplotlib 网站上的示例代码,但我用简单的 arange 数组替换了它们的半径和角度。

我尝试了不同的数组函数,但我似乎无法弄清楚任何事情。

from mpl_toolkits.mplot3d import Axes3D  

import matplotlib.pyplot as plt
import numpy as np
from Equation import Expression


x = np.arange(0,100,0.01)
y = np.arange(0,100,0.01)
x2 = np.append(0,x.flatten())
y2 = np.append(0,y.flatten())
z = x2 + y2
print(z)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)
plt.show()

我只是想制作z = x + y 的图表,但我遇到了一个令人困惑的错误。

“RuntimeError: qhull Delaunay 三角剖分计算错误:奇异输入数据 (exitcode=2);使用 python 详细选项 (-v) 查看原始 qhull 错误。”

编辑:我也尝试过没有调用flatten(),但我得到了相同的结果。

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    你得到的错误是因为你的z 不是一个表面而是一条线。您需要使用至少 3 个点来定义一个平面。一种选择是使用 np.meshgrid 创建用于绘图的表面,然后将所有内容展平以插入到函数中。尝试回到一些示例代码here。请注意,您可能还想根据表面的细节更改分辨率。

    from mpl_toolkits.mplot3d import Axes3D  
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    
    x = np.arange(0,100,1)
    y = np.arange(0,100,1)
    x2 = np.append(0,x.flatten())
    y2 = np.append(0,y.flatten())
    
    x2,y2 = np.meshgrid(x2,y2) #This is what you were missing
    
    z = x2 + y2
    
    fig = plt.figure(figsize=(12,12))
    ax = fig.gca(projection='3d')
    ax.plot_trisurf(x2.flatten(), y2.flatten(), z.flatten(), linewidth=0.2, antialiased=True) #flatten all the arrays here
    
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 2012-07-28
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      相关资源
      最近更新 更多