【问题标题】:matplotlib not displaying intersection of 3D planes correctlymatplotlib 未正确显示 3D 平面的交点
【发布时间】:2013-12-22 20:41:21
【问题描述】:

我想绘制两个平面并找到它们的相交线,但我得到了这个结果,因为一个平面覆盖另一个平面,所以无法分辨它们相交的位置。

3D 投影应该隐藏平面的不可见部分,我如何使用 ma​​tplotlib 获得这个结果?

你可以清楚地看到这些到平原应该相交。

这是我用来获取此结果的代码

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

values = range(-10, 11)

def plotPlane(plot, normal, d, values, colorName):
    # x, y, z
    x, y = np.meshgrid(values, values)
    z = (-normal[0] * x - normal[1] * y - d) * 1. / normal[2]

    # draw plot
    plot.plot_surface(x, y, z, color=colorName)

image = plt.figure().gca(projection='3d')

plotPlane(image, [3, 2, -4], 1, values, "red")
plotPlane(image, [5, -1, 2], 4, values, "gray")

plt.show()

【问题讨论】:

  • 看我的回答here

标签: python 3d matplotlib


【解决方案1】:

请参阅How to draw intersecting planes? 了解详细说明 + 可能的解决方法。

matplotlib 的 3D 支持中的简短回答是巧妙地使用投影来生成 3D 对象的 2D 视图,然后将其渲染到画布上。由于 matplotlib 渲染的方式(一次艺术家),一位艺术家要么完全高于或完全低于另一位艺术家。如果您需要真正的 3D 支持,请查看mayavi

【讨论】:

  • 谢谢!我在 MATLAB 中绕了一大圈,据我所知,它是纯 3D 的。但我也会去看看 mayavi。
  • 我一头雾水,MATLAB哪来的?
  • matplotlib 是工具,MATLAB 是工具,所以我只是切换工具。
  • this 不久前我发布的答案是一种更好的方法。
【解决方案2】:

使用plotly。您将获得一个交互式绘图,您可以从任何角度进行快照。

import numpy as np
import plotly.graph_objects as go
from typing import Tuple, Iterable


def plotPlane(fig: go.Figure,
              normal: Tuple[int, int, int],
              d: int,
              values: Iterable,
              colorScaleName: str) -> None:
    """
        :param fig: figure to plot on
        :param colorScaleName: choose from <https://plotly.com/javascript/colorscales/>
    """
    # x, y, z
    x, y = np.meshgrid(values, values)
    z = (-normal[0] * x - normal[1] * y - d) * 1. / normal[2]

    # draw plane
    surface = go.Surface(x=x, y=y, z=z, colorscale=colorScaleName, showscale=False)
    fig.add_trace(surface, row=1, col=1)


# create figure
fig = make_subplots(rows=1, cols=1, specs=[[{'type': 'surface'}]])
# plot two intersectioned surfaces
values = range(-10, 11)
plotPlane(fig, (3, 2, -4), 1, values, "Hot")
plotPlane(fig, (5, -1, 2), 4, values, "Greys")
fig.show()

我在 jupyter-notebbok 中运行它。

【讨论】:

    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2016-07-03
    • 1970-01-01
    • 2011-11-25
    • 2022-07-10
    • 1970-01-01
    相关资源
    最近更新 更多