使用 Matplotlib 绘制椭球体
有人有绘制椭球体的示例代码吗? matplotlib
网站上有一个关于球体的信息,但没有关于椭球体的信息。我试图绘制
x**2 + 2*y**2 + 2*z**2 = c
其中 c
是定义椭球体的常量(如 10)。我尝试了 meshgrid(x,y) 路线,重新设计了方程,使 z 位于一侧,但 sqrt 是一个问题。 matplotlib
球体示例适用于角度,u,v
,但我不确定如何将其应用于椭球体。
Does anyone have sample code for plotting ellipsoids? There is one for sphere on matplotlib
site, but nothing for ellipsoids. I am trying to plot
x**2 + 2*y**2 + 2*z**2 = c
where c
is a constant (like 10) that defines an ellipsoid. I tried the meshgrid(x,y)
route, reworked the equation so z
is on one side, but the sqrt
is a problem. The matplotlib
sphere example works with angles, u,v
, but I am not sure how to work that for ellipsoid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是通过球面坐标来完成此操作的方法:
生成的图类似于
上面的程序实际上生成看起来更好看的“方形”图形。
该解决方案深受 示例 的启发//matplotlib.sourceforge.net/gallery.html" rel="nofollow noreferrer">Matplotlib 的画廊。
Here is how you can do it via spherical coordinates:
The resulting plot is similar to
The program above actually produces a nicer looking "square" graphics.
This solution is strongly inspired from the example in Matplotlib's gallery.
以 EOL 的答案为基础。有时您有一个矩阵格式的椭球:
A 和 c 其中 A 是椭球矩阵,c 是表示椭球中心的向量。
因此,这里并没有太多新内容,但是如果您有一个矩阵形式的椭球体,该椭球体经过旋转并且可能不以 0,0,0 为中心并且想要绘制它,那么它会很有帮助。
Building on EOL's answer. Sometimes you have an ellipsoid in matrix format:
A and c Where A is the ellipsoid matrix and c is a vector representing the centre of the ellipsoid.
So, not too much new here, but helpful if you've got an ellipsoid in matrix form which is rotated and perhaps not centered at 0,0,0 and want to plot it.
如果您有一个由任意协方差矩阵
cov
和偏移量bias
指定的椭球体,则可以执行更简单的 @minillinim 的答案 通过向量化操作。从
制作单位球体
开始计算标准差矩阵,该矩阵具有与协方差相同的旋转,但按特征值的平方根缩放:
现在变换球体:
您可以像以前一样绘制结果:
作为参考,< code>u、
v
具有形状(100,)
,这使得x
、y
、z
进入(100, 100)
数组。sphere
是(100, 100, 3, 1)
,这使得它成为一个由 3x1 向量组成的 100x100 数组,就广播@
运算符而言担心的。s @sphere
具有相同的大小,因此挤压最后一个单位轴使其适合使用bias
进行加法广播。最后,ellipsoid.transpose(2, 0, 1)
的形状为(3, 100, 100)
,它可以星展开为三个独立的 x- 数组, y 和 z 值传入对plot_surface
的调用。If you have an ellipsoid specified by an arbitrary covariance matrix
cov
and offsetbias
, you perform a simpler version of @minillinim's answer by vectorizing the operations.Starting with
Make a unit sphere
Compute the standard deviation matrix, which has the same rotation as the covariance, but scales by the square root of the eigenvalues:
Now transform the sphere:
You can plot the result pretty much as before:
For reference,
u
,v
have shape(100,)
, which makesx
,y
,z
into(100, 100)
arrays.sphere
is(100, 100, 3, 1)
, which makes it a 100x100 array of 3x1 vectors as far as the broadcasting@
operator is concerned.s @ sphere
has the same size, so squeezing out the last unit axis makes it suitable for broadcasting for addition withbias
. Finally,ellipsoid.transpose(2, 0, 1)
has shape(3, 100, 100)
, which can be star-expanded as three separate arrays of x-, y- and z-values into the call toplot_surface
.