Matplotlib的三维可视化,mplot3d教程

Matplotlib的三维可视化,mplot3d教程。

mplot3d tutorial

Contents

Getting started

An Axes3D object is created just like any other axes using the projection=‘3d’ keyword. Create a new matplotlib.figure.Figure and add a new axes to it of type Axes3D:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

New in version 1.0.0: This approach is the preferred method of creating a 3D axes.

Note

Prior to version 1.0.0, the method of creating a 3D axes was different. For those using older versions of matplotlib, changeax = fig.add_subplot(111, projection='3d')to ax = Axes3D(fig).

Line plots

  • Axes3D.plot(xs, ys, *args, **kwargs)

  • Plot 2D or 3D data.

    ArgumentDescription
    xs, ysx, y coordinates of vertices
    zsz value(s), either one for all points or one for each point.
    zdirWhich direction to use as z (‘x’, ‘y’ or ‘z’) when plotting a 2D set.

    Other arguments are passed on toplot()

(Source code, png, hires.png, pdf)

../../_images/lines3d_demo1.png

Scatter plots

  • Axes3D.scatter(xs, ys, zs=0, zdir='z', s=20, c='b', depthshade=True, *args, **kwargs)

  • Create a scatter plot.

    ArgumentDescription
    xs, ysPositions of data points.
    zsEither an array of the same length as xs andys or a single value to place all points in the same plane. Default is 0.
    zdirWhich direction to use as z (‘x’, ‘y’ or ‘z’) when plotting a 2D set.
    sSize in points^2.  It is a scalar or an array of the same length as x and y.
    cA color. c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using thecmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped.  c can be a 2-D array in which the rows are RGB or RGBA, however.
    depthshadeWhether or not to shade the scatter markers to give the appearance of depth. Default is True.

    Keyword arguments are passed on toscatter().

    Returns a Patch3DCollection

(Source code, png, hires.png, pdf)

../../_images/scatter3d_demo1.png

Wireframe plots

  • Axes3D.plot_wireframe(X, Y, Z, *args, **kwargs)

  • Plot a 3D wireframe.

    The rstride and cstride kwargs set the stride used to sample the input data to generate the graph. If either is 0 the input data in not sampled along this direction producing a 3D line plot rather than a wireframe plot.

    ArgumentDescription
    X, Y,Data values as 2D arrays
    Z
    rstrideArray row stride (step size), defaults to 1
    cstrideArray column stride (step size), defaults to 1

    Keyword arguments are passed on toLineCollection.

    Returns a Line3DCollection

(Source code, png, hires.png, pdf)

../../_images/wire3d_demo1.png

Surface plots

  • Axes3D.plot_surface(X, Y, Z, *args, **kwargs)

  • 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 cmapargument.

    The rstride and cstride kwargs set the stride used to sample the input data to generate the graph.  If 1k by 1k arrays are passed in the default values for the strides will result in a 100x100 grid being plotted.

    ArgumentDescription
    X, Y, ZData values as 2D arrays
    rstrideArray row stride (step size), defaults to 10
    cstrideArray column stride (step size), defaults to 10
    colorColor of the surface patches
    cmapA colormap for the surface patches.
    facecolorsFace colors for the individual patches
    normAn instance of Normalize to map values to colors
    vminMinimum value to map
    vmaxMaximum value to map
    shadeWhether to shade the facecolors

    Other arguments are passed on toPoly3DCollection

(Source code, png, hires.png, pdf)

../../_images/surface3d_demo1.png

(Source code, png, hires.png, pdf)

../../_images/surface3d_demo21.png

(Source code, png, hires.png, pdf)

../../_images/surface3d_demo31.png

Tri-Surface plots

  • Axes3D.plot_trisurf(*args, **kwargs)

  • ArgumentDescription
    X, Y, ZData values as 1D arrays
    colorColor of the surface patches
    cmapA colormap for the surface patches.
    normAn instance of Normalize to map values to colors
    vminMinimum value to map
    vmaxMaximum value to map
    shadeWhether to shade the facecolors

    The (optional) triangulation can be specified in one of two ways; either:

    plot_trisurf(triangulation, ...)

    where triangulation is a Triangulationobject, or:

    plot_trisurf(X, Y, ...)plot_trisurf(X, Y, triangles, ...)plot_trisurf(X, Y, triangles=triangles, ...)

    in which case a Triangulation object will be created.  SeeTriangulation for a explanation of these possibilities.

    The remaining arguments are:

    plot_trisurf(..., Z)

    where Z is the array of values to contour, one per point in the triangulation.

    Other arguments are passed on toPoly3DCollection

    Examples:

    (Source code, png, hires.png, pdf)

    ../../_images/trisurf3d_demo1.png

    (Source code)

    ../../_images/trisurf3d_demo2_001.png

    (png, hires.png, pdf)

    ../../_images/trisurf3d_demo2_011.png

    (png, hires.png, pdf)

    New in version 1.2.0: This plotting function was added for the v1.2.0 release.

(Source code, png, hires.png, pdf)

../../_images/trisurf3d_demo1.png

Contour plots

  • Axes3D.contour(X, Y, Z, *args, **kwargs)

  • Create a 3D contour plot.

    ArgumentDescription
    X, Y,Data values as numpy.arrays
    Z
    extend3dWhether to extend contour in 3D (default: False)
    strideStride (step size) for extending contour
    zdirThe direction to use: x, y or z (default)
    offsetIf specified plot a projection of the contour lines on this position in plane normal to zdir

    The positional and other keyword arguments are passed on tocontour()

    Returns a contour

(Source code, png, hires.png, pdf)

../../_images/contour3d_demo1.png

(Source code, png, hires.png, pdf)

../../_images/contour3d_demo21.png

(Source code, png, hires.png, pdf)

../../_images/contour3d_demo31.png

Filled contour plots

  • Axes3D.contourf(X, Y, Z, *args, **kwargs)

  • Create a 3D contourf plot.

    ArgumentDescription
    X, Y,Data values as numpy.arrays
    Z
    zdirThe direction to use: x, y or z (default)
    offsetIf specified plot a projection of the filled contour on this position in plane normal to zdir

    The positional and keyword arguments are passed on tocontourf()

    Returns a contourf

    Changed in version 1.1.0: The zdir and offset kwargs were added.

(Source code, png, hires.png, pdf)

../../_images/contourf3d_demo1.png

(Source code, png, hires.png, pdf)

../../_images/contourf3d_demo22.png

New in version 1.1.0: The feature demoed in the second contourf3d example was enabled as a result of a bugfix for version 1.1.0.

Polygon plots

  • Axes3D.add_collection3d(col, zs=0, zdir='z')

  • Add a 3D collection object to the plot.

    2D collection types are converted to a 3D version by modifying the object and adding z coordinate information.

    • PolyCollection

    • LineColleciton

    • PatchCollection

    • Supported are:


(Source code, png, hires.png, pdf)

../../_images/polys3d_demo1.png

Bar plots

  • Axes3D.bar(left, height, zs=0, zdir='z', *args, **kwargs)

  • Add 2D bar(s).

    ArgumentDescription
    leftThe x coordinates of the left sides of the bars.
    heightThe height of the bars.
    zsZ coordinate of bars, if one value is specified they will all be placed at the same z.
    zdirWhich direction to use as z (‘x’, ‘y’ or ‘z’) when plotting a 2D set.

    Keyword arguments are passed onto bar().

    Returns a Patch3DCollection

(Source code, png, hires.png, pdf)

../../_images/bars3d_demo1.png

Quiver

  • Axes3D.quiver(*args, **kwargs)

  • Plot a 3D field of arrows.

    call signatures:

    quiver(X, Y, Z, U, V, W, **kwargs)

    Arguments:

    The arguments could be array-like or scalars, so long as they they can be broadcast together. The arguments can also be masked arrays. If an element in any of argument is masked, then that corresponding quiver element will not be plotted.

    Keyword arguments:

    Any additional keyword arguments are delegated toLineCollection

    • length: [1.0 | float]

    • The length of each quiver, default to 1.0, the unit is the same with the axes

    • arrow_length_ratio: [0.3 | float]

    • The ratio of the arrow head with respect to the quiver, default to 0.3

    • pivot: [ ‘tail’ | ‘middle’ | ‘tip’ ]

    • The part of the arrow that is at the grid point; the arrow rotates about this point, hence the name pivot.

    • X, Y, Z:

    • The x, y and z coordinates of the arrow locations (default is tip of arrow; see pivot kwarg)

    • U, V, W:

    • The x, y and z components of the arrow vectors

(Source code, png, hires.png, pdf)

../../_images/quiver3d_demo1.png

2D plots in 3D

(Source code, png, hires.png, pdf)

../../_images/2dcollections3d_demo1.png

Text

  • Axes3D.text(x, y, z, s, zdir=None, **kwargs)

  • Add text to the plot. kwargs will be passed on to Axes.text, except for the zdir keyword, which sets the direction to be used as the z direction.

(Source code, png, hires.png, pdf)

../../_images/text3d_demo1.png

Subplotting

Having multiple 3D plots in a single figure is the same as it is for 2D plots. Also, you can have both 2D and 3D plots in the same figure.

New in version 1.0.0: Subplotting 3D plots was added in v1.0.0.  Earlier version can not do this.

(Source code, png, hires.png, pdf)

../../_images/subplot3d_demo1.png

(Source code, png, hires.png, pdf)

../../_images/mixed_subplots_demo1.png


转载于:https://my.oschina.net/u/2306127/blog/600766

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值