简体   繁体   English

matplotlib plot_surface图

[英]matplotlib plot_surface plot

The matplotlib tutorial provides a nice example of how to draw a spherical suface: matplotlib教程提供了如何绘制球形表面的漂亮示例:

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()

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 . 据我了解,这将为每个xyz变量创建一个二维网格,对应于参数uv的乘积。 The calculated x , y , and z variables are then the cartesian coordinates created from the spherical coordinates in u and v . 然后,计算出的xyz变量是从uv的球坐标创建的笛卡尔坐标。

My question is the following: Why does the input to plot_surface have to be in 2D arrays? 我的问题如下: 为什么 plot_surface 的输入 必须是2D数组?

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. 这个问题似乎提出了类似的问题,但是单个答案并不是特别有启发性。

The Equation of a surface is: 表面的等式为:

  f(x,y,z)=c

where the constants characterize the surfaces. 常数表征表面。 In the case of a circle it is: 如果是圆形,则为:

(x^2 + y^2 + z^2)^(1/2) = c

Where c is the radius. 其中c是半径。 each value of gives one surface. 的每个值给出一个表面。 In other words, f(x,y,z) can be written as z=g(x,y). 换句话说,f(x,y,z)可以写成z = g(x,y)。 Now if you have to span an area with two independent variables x & y, both will have to be 2D arrays. 现在,如果您必须使用两个自变量x和y来跨越一个区域,则两个都必须是2D数组。 note that both x and y will be 2D arrays and so will z. 请注意,x和y均为二维数组,z也是如此。

A: Because the interface specification orders that. 答:因为接口规范要求。

However strange does that look, the 2D-parametric grid, 不管看上去多么奇怪,二维参数网格

describing the surface [ R = const, u = < 0, 2pi >, v = < 0, pi > ] from Spherical coordinate space is translated into a cartesian-space via a mapping, 从球坐标空间描述表面[ R = const, u = < 0, 2pi >, v = < 0, pi > ]通过映射将其转换为笛卡尔空间,

stored in a set of [ MAT2Dx[,], MAT2Dy[,], MAT2Dz[,] ] 存储在一组[ MAT2Dx[,], MAT2Dy[,], MAT2Dz[,] ]

because that is the requirement the .plot_surface() method requires the surface-data to be received. 因为这是.plot_surface()条件,所以.plot_surface()方法需要接收表面数据。

>>> 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`

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 . 根据设计,表面是2D实体,在这里可以通过[R,u,v]或[X,Y,Z]坐标系进行参数化,并且由于[R,u,v]的解析描述容易在球体表面上,啮合是通过一对.linspace()方法生成的[u,v] -grid开始的,而保持R=const=10

Further: 进一步:

>>> 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 ]]

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) 已创建形状为[100,100]的xyz矩阵,作为[u,v] -> x(u,v), y(u,v), z(u,v)

finally , .plot_surface() method has consumed these in 最后.plot_surface()方法已将它们消耗在

 x,y,z = np.broadcast_matrices( x, y, z )

before starting to produce a list of 2D-surface-objects ( to be plot ), iterating over the scope of the original [u,v] -2Dgrid. 在开始生成2D曲面对象列表(待绘制)之前,请迭代原始[u,v] -2Dgrid的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM