matplotlib plot_surface 图 [英] matplotlib plot_surface plot
问题描述
matplotlib教程为如何绘制球形表面提供了一个很好的示例:
The matplotlib tutorial provides a nice example of how to draw a spherical suface:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
plt.show()
据我了解,这会为与参数乘积相对应的每个 x
, y
和 z
变量创建一个2D网格u
和 v
.然后,计算出的 x
, y
和 z
变量是从 u
中的球坐标创建的笛卡尔坐标,并且v
.
From what I understand, this creates a 2D grid for each x
, y
, and z
variable corresonding to the product of the parameters u
and v
. The calculated x
, y
, and z
variables are then the cartesian coordinates created from the spherical coordinates in u
and v
.
我的问题如下:为什么对 plot_surface
的输入必须位于2D数组中?
My question is the following: Why does the input to plot_surface
have to be in 2D arrays?
我怀疑这与计算每个表面的法线有关,但是我似乎无法弄清楚.有一些详细的文档对此进行描述吗?
I suspect it has something to do with calculating the normals of each of the surface faces, but I can't seem to figure it out. Is there some detailed documentation that describes this?
这个问题似乎提出了类似的问题,但单个答案并没有特别的启发性.
This question seems to ask something similar, but the single answer isn't particularly enlightening.
推荐答案
A:因为接口规范要求.
不管这看起来多么奇怪,二维参数网格,
A: Because the interface specification orders that.
However strange does that look, the 2D-parametric grid,
描述表面<代码>[ R = const, u = <0, 2pi >, v = <0,pi>] 从球形坐标空间通过映射转换为笛卡尔空间,
describing the surface
[ R = const, u = < 0, 2pi >, v = < 0, pi > ]
from Spherical coordinate space is translated into a cartesian-space via a mapping,
存储在一组 [MAT2Dx [,],MAT2Dy [,],MAT2Dz [,]]
因为这是 .plot_surface()
方法要求接收表面数据的要求.
because that is the requirement the .plot_surface()
method requires the surface-data to be received.
>>> print ax.plot_surface.__doc__
Create a surface plot.
By default it will be colored in shades of a solid color,
but it also supports color mapping by supplying the *cmap*
argument.
============= ================================================
Argument Description
============= ================================================
*X*, *Y*, *Z* Data values as 2D arrays
*rstride* Array row stride (step size)
*cstride* Array column stride (step size)
*color* Color of the surface patches
*cmap* A colormap for the surface patches.
*facecolors* Face colors for the individual patches
*norm* An instance of Normalize to map values to colors
*vmin* Minimum value to map
*vmax* Maximum value to map
*shade* Whether to shade the facecolors
============= ================================================
Other arguments are passed on to
:class:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
根据设计,表面是2D实体,此处通过[R,u,v]或[X,Y,Z]坐标系进行参数化,并且其易用性[R,u,v]球面的解析描述,网格划分是由一对 .linspace()
方法生成的 [u,v]
-网格开始的,而其余的 R =const=10
.
By design, a surface is a 2D-entity, here parametrised either by in [R,u,v] or [X,Y,Z] coordinate system, and due to the ease of [R,u,v] analytic description of a sphere surface, the meshing started in [u,v]
-grid produced by a pair of .linspace()
methods, whereas remained R=const=10
.
进一步:
>>> print np.outer.__doc__
Compute the outer product of two vectors.
Given two vectors, ``a = [a0, a1, ..., aM]`` and
``b = [b0, b1, ..., bN]``,
the outer product [1]_ is::
[[a0*b0 a0*b1 ... a0*bN ]
[a1*b0 .
[ ... .
[aM*b0 aM*bN ]]
已创建形状为[100,100]的 x
, y
, z
个矩阵,作为 [u,v]->的基于三角律的映射.x(u,v), y(u,v), z(u,v)
has created x
, y
, z
matrices in a shape of [100,100],
as a trigonometry-laws-based mapping of [u,v] -> x(u,v), y(u,v), z(u,v)
最终, .plot_surface()
方法已在其中使用了这些
finally, .plot_surface()
method has consumed these in
x,y,z = np.broadcast_matrices( x, y, z )
在开始生成 2D 表面对象列表(要绘制)之前,迭代原始 [u,v]
-2Dgrid 的范围.
before starting to produce a list of 2D-surface-objects ( to be plot ), iterating over the scope of the original [u,v]
-2Dgrid.
这篇关于matplotlib plot_surface 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!