![](/img/trans.png)
[英]Changing the background color of the axes planes of a matplotlib 3D plot
[英]matplotlib not displaying intersection of 3D planes correctly
我想繪制兩個平面並找到它們的相交線,但我得到了這個結果,無法分辨它們在哪里相交,因為一個平面覆蓋另一個平面。
3D 投影應該隱藏平面的不可見部分,如何使用matplotlib獲得此結果?
你可以清楚地看到這些到平原應該相交。
這是我用來得到這個結果的代碼
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()
請參閱如何繪制相交平面? 詳細解釋+可能的解決方法。
matplotlib 的 3D 支持中的簡短回答是巧妙地使用投影來生成 3D 對象的 2D 視圖,然后將其渲染到畫布上。 由於 matplotlib 渲染(一次一位藝術家)的方式,一位藝術家要么完全高於另一位藝術家,要么完全低於另一位藝術家。 如果您需要真正的 3D 支持,請查看mayavi
。
使用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 中運行它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.