python环境下不规则网格上的曲面绘制

2024-09-30 01:20:20 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以创建函数的曲面图和等高线图,例如u(x,y) = x^2 + y^2

关于以方程为界的跟随域

r(t) = 1+(cos(4*t))^2, x = r(t)*cos(t),y = r(t)*sin(t), 0 < t < 2*pi

enter image description here

我想要下面散点图的曲面图变体。 enter image description here 我还使用了scipygriddata,如下所示

^{pr2}$

但结果却不尽如人意 enter image description here


Tags: 函数pisin变体cos方程曲面pr2
1条回答
网友
1楼 · 发布于 2024-09-30 01:20:20

首先计算矩形网格上的函数!在

scipy.interpolate.griddata在凸壳上插值。你可以在the docs里读到。这意味着它也在你的肺叶之间插入。这就是为什么你没有得到正确的域名。插值本身也比只计算网格上的函数更不精确。在

plot_wireframe需要一个已经创建的矩形网格。你需要做的就是计算矩形网格上的函数值。若要仅在域边界上绘制值,请将其外部的所有值设置为np.NaN(不是-a-number)。在

方法如下:

import matplotlib.pyplot as plt
import numpy as np

# cartesian coordinates
xx,yy = np.meshgrid(np.linspace(-2,2,100),np.linspace(-2,2,100))

# function value across square domain
Ua = xx**2 + yy**2

# polar coordinates
tt = np.arctan2(yy, xx)
rr = np.sqrt(Ua) # re-using x^2 + y^2   only works for this function

# r coordinate of domain boundary
domain_boundary = 1 + (np.cos(4*tt))**2

# function value across actual domain, with rest set to NaN
Ua[rr > domain_boundary] = np.NaN

# plotting
fig = plt.figure(2)
ax = fig.gca(projection='3d')
ax.plot_wireframe(xx,yy,Ua,rstride=1,cstride=1,linewidth=.5)

enter image description here

该解决方案并不完美,因为您可以在结果中识别出矩形网格。您可以尝试在极坐标下工作,如this official matplotlib example所示。在

相关问题 更多 >

    热门问题