http://blog.csdn.net/pipisorry/article/details/40008005

Matplotlib mplot3d 工具包简介

The mplot3d toolkit adds simple 3D plotting capabilities to matplotlib by supplying an axes object that can create a 2D projection of a 3D scene. The resulting graph will have the same look and feel as regular 2D plots.

创建Axes3D对象

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 di erent. For those using older versions of matplotlib, change ax = fig.add_subplot(111, projection=’3d’) to ax = Axes3D(fig).

要注意的地方

Axes3D展示三维图形时,其初始视图中x,y轴与我们一般看的视图(自己画的时候的视图)是反转的,matlab也是一样。 可以通过设置初始视图来改变角度:ax.view_init(30, 35)

Note: 不过这样图形可能会因为旋转而有阴影,也可以通过代码中XY轴互换来实现视图中XY互换。不知道有没有其它方法,如matlab中就有surf(x,y,z);set(gca,'xdir','reverse','ydir','reverse')这样命令来实现这个功能[关于matlab三维图坐标轴原点位置的问题]。

lz总结绘制三维图形一般流程

创建Axes3D对象

fig =plt.figure()

ax =Axes3D(fig) 再进行坐标范围设定什么的(可选)

# 计算坐标极限值xs =list(itertools.chain.from_iterable([xi[0] forxi inx]))

x_max, x_min =max(xs), min(xs)

ys =list(itertools.chain.from_iterable([xi[1] forxi inx]))

y_max, y_min =max(ys), min(ys)

zs =list(itertools.chain.from_iterable([xi[2] forxi inx]))

z_max, z_min =max(zs), min(zs)

margin =0.1

再进行绘制,如

plt.scatter(x_new[0], x_new[1], c='r', marker='*', s=50, label='new x')

ax.scatter(xs, ys, zs, c=c, marker=marker, s=50, label=label)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, label='Discrimination Interface') 再进行一些坐标什么的设置

# 设置图形展示效果ax.set_xlim(x_min -margin, x_max +margin)

ax.set_ylim(y_min -margin, y_max +margin)

ax.set_zlim(z_min -margin, z_max +margin)

ax.set_xlabel('x')

ax.set_ylabel('y')

ax.set_zlabel('z')

ax.legend(loc='lower right')

ax.set_title('Plot of class0 vs. class1')

ax.view_init(30, 35) 最后显示出来

plt.show()

皮皮blog

绘制不同三维图形

Line plots线图

Scatter plots散点图

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

Create a scatter plot

Argument

Description

xs, ys

Positions of data points.

zs

Either an array of the same length as xs andys or a single value to place all points inthe same plane. Default is 0.

zdir

Which direction to use as z (‘x’, ‘y’ or ‘z’)when plotting a 2D set.

s

size in points^2. It is a scalar or an array of thesame length as x andy.

c

a color. c can be a single color format string, or asequence of color specifications of lengthN, or asequence ofN numbers to be mapped to colors using thecmap andnorm specified via kwargs (see below). Notethatc should not be a single numeric RGB or RGBAsequence because that is indistinguishable from an arrayof values to be colormapped.c can be a 2-D array inwhich the rows are RGB or RGBA, however.

depthshade

Whether or not to shade the scatter markers to givethe appearance of depth. Default isTrue. Keyword arguments are passed on to scatter().

Returns a Patch3DCollection

Note: scatter (

x,

y,

s=20,

c=u'b',

marker=u'o',

cmap=None,

norm=None,

vmin=None,

vmax=None,

alpha=None,

linewidths=None,

verts=None,

**kwargs )

Wireframe plots线框图

Surface plots曲面图

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()

ax = Axes3D(fig)

X = np.arange(-4, 4, 0.25)

Y = np.arange(-4, 4, 0.25)

X, Y = np.meshgrid(X, Y)

R = np.sqrt(X**2 + Y**2)

Z = np.sin(R)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)

ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)

ax.set_zlim(-2,2)

# savefig('../figures/plot3d_ex.png',dpi=48)

plt.show()结果图形输出:

[Matplotlib tutorial - 3D Plots]

皮皮blog

Tri-Surface plots三面图

Contour plots等高线图

Filled contour plots填充等高线图

Polygon plots多边形图

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

Add a 3D collection object to the plot.

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

Supported are:

PolyCollection

LineColleciton

PatchCollection 绘图原理是:设置好多面体(如正文体)的顶点坐标和所有面对应的顶点,加上一些属性(如颜色、宽度、透明度)

绘制正方体和四面体示例

#!/usr/bin/env python# -*- coding: utf-8 -*-"""__title__ = ''__author__ = '皮'__mtime__ = '9/27/2015-027'__email__ = 'pipisorry@126.com'"""importmatplotlib.pyplot asplt

frommpl_toolkits.mplot3d.art3d importPoly3DCollection

fig =plt.figure()

ax =fig.gca(projection='3d')

# 正文体顶点和面verts =[(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0), (0, 0, 1), (0, 1, 1), (1, 1, 1), (1, 0, 1)]

faces =[[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [0, 3, 7, 4]]

# 四面体顶点和面# verts = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 0, 1)]# faces = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]]# 获得每个面的顶点poly3d =[[verts[vert_id] forvert_id inface] forface infaces]

# print(poly3d)# 绘制顶点x, y, z =zip(*verts)

ax.scatter(x, y, z)

# 绘制多边形面ax.add_collection3d(Poly3DCollection(poly3d, facecolors='w', linewidths=1, alpha=0.3))

# ax.add_collection3d(Line3DCollection(poly3d, colors='k', linewidths=0.5, linestyles=':'))

# 设置图形坐标范围ax.set_xlabel('X')ax.set_xlim3d(-0.5, 1.5)ax.set_ylabel('Y')ax.set_ylim3d(-0.5, 1.5)ax.set_zlabel('Z')ax.set_zlim3d(-0.5, 1.5)plt.show()

皮皮blog

绘制结果截图

  

[Transparency for Poly3DCollection plot in matplotlib]

Bar plots条形图

2D plots in 3D三维图中的二维图

Text文本图

Subplotting子图

matplotlib绘制平行z轴的平面(垂直xy平面的平面)

方程:0*Z + A[0]X + A[1]Y + A[-1] = 0

X =np.arange(x_min-margin, x_max+margin, 0.05)

Z =np.arange(z_min-margin, z_max+margin, 0.05)

X, Z =np.meshgrid(X, Z)

Y = -1/A[1] *(A[0] *X +A[-1])

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, label='Discrimination Interface')

皮皮blog

Mayavi的mlab模块绘制3D曲面

NumPy快速产生能进行广播运算的数组的ogrid对象

>>>x,y = np.ogrid[:5,:5]

>>>x

array([[0], [1], [2], [3], [4]])

>>>y

array([[0, 1, 2, 3, 4]])

mgrid对象的用法和ogrid对象类似,但是它所返回的是进行广播之后的数组。请读者运行“np.mgrid[:5,:5]”试试看。

ogrid是一个很有趣的对象,它和多维数组一样,用切片元组作为下标,返回的是一组可以用来广播计算的数组。其切片下标有两种形式:

开始值:结束值:步长,和“np.arange(开始值, 结束值, 步长)”类似

开始值:结束值:长度j,当第三个参数为虚数时,它表示所返回的数组的长度,和“np.linspace(开始值, 结束值, 长度)”类似:

>>>x, y = np.ogrid[:1:4j, :1:3j]

>>>x

array([[ 0. ],

[ 0.33333333],

[ 0.66666667],

[ 1. ]])

>>>y

array([[ 0. , 0.5, 1. ]])

利用ogrid的返回值,我们能很容易计算二元函数在等间距网格上的值。

绘制三维曲面

02-numpy/numpy_ogrid_mlab.py

用ogird产生二维坐标网格,计算三维空间的曲面

import numpy as np

from enthought.mayavi import mlab

x, y = np.ogrid[-2:2:20j, -2:2:20j]

z = x * np.exp( - x**2 - y**2)

pl = mlab.surf(x, y, z, warp_scale="auto")

mlab.axes(xlabel='x', ylabel='y', zlabel='z')

mlab.outline(pl)

mlab.show()

此程序使用Mayavi的mlab模块快速绘制如【图:使用ogrid创建的三维曲面】所示的3D曲面,关于Mayavi的相关内容将在今后的章节进行介绍。

使用ogrid创建的三维曲面

使用ix_()构成网格上点的函数值

如果已经有了多个表示不同轴上的取值的一维数组。想计算出它们所构成的网格上的每点的函数值,可以使用ix_():

>>>x = np.array([0, 1, 4, 10])

>>>y = np.array([2, 3, 8])

>>>gy, gx = np.ix_(y, x)

>>>gx

array([[ 0, 1, 4, 10]])

>>>gy

array([[2], [3], [8]])

>>>gx+gy # 广播运算

array([[ 2, 3, 6, 12],

[ 3, 4, 7, 13],

[ 8, 9, 12, 18]])

在上面的例子中,通过ix_()将数组x和y转换成能进行广播运算的二维数组。注意数组y对应广播运算结果中的第0轴,而数组x与第1轴对应。 [ numpy教程- 通用函数ufunc]

from:http://blog.csdn.net/pipisorry/article/details/40008005

ref:mplot3d tutorial(inside source code)

mplot3d API

matlab三维绘图poly,matplotlib绘制三维图形mplot3d(包含Mayavi.mlab模块)相关推荐

  1. python画图三维-Python使用matplotlib绘制三维图形示例

    本文实例讲述了Python使用matplotlib绘制三维图形.分享给大家供大家参考,具体如下: 用二维泡泡图表示三维数据 泡泡的坐标2维,泡泡的大小三维,使用到的函数 plt.scatter(P[: ...

  2. python画三维立体图-Python+matplotlib绘制三维图形5个精选案例

    如果要绘制三维图形,首先需要使用下面的语句导入相应的对象: from mpl_toolkits.mplot3d import Axes3D 然后使用下面的两种方式之一声明要创建三维子图: ax = f ...

  3. python画三维图-Python+matplotlib绘制三维图形5个精选案例

    如果要绘制三维图形,首先需要使用下面的语句导入相应的对象: from mpl_toolkits.mplot3d import Axes3D 然后使用下面的两种方式之一声明要创建三维子图: ax = f ...

  4. Python可视化matplotlib绘制三维可视化图形(Three-Dimensional)

    Python可视化matplotlib绘制三维可视化图形(Three-Dimensional) Matplotlib最初设计时只考虑二维可视化绘图.大约在1.0版本发布的时候,在Matplotlib的 ...

  5. python绘制三维曲面图-Python中使用Matplotlib绘制3D图形示例

    原标题:Python中使用Matplotlib绘制3D图形示例 3D图形能给我们对数据带来更加深入地理解.python的matplotlib库就包含了丰富的3D绘图工具.3D图形在数据分析.数据建模. ...

  6. python代码示例图形-Python使用matplotlib绘制三维图形示例

    本文实例讲述了Python使用matplotlib绘制三维图形.分享给大家供大家参考,具体如下: 用二维泡泡图表示三维数据 泡泡的坐标2维,泡泡的大小三维,使用到的函数 plt.scatter(P[: ...

  7. python画三维图-Python使用matplotlib绘制三维图形示例

    本文实例讲述了Python使用matplotlib绘制三维图形.分享给大家供大家参考,具体如下: 用二维泡泡图表示三维数据 泡泡的坐标2维,泡泡的大小三维,使用到的函数 plt.scatter(P[: ...

  8. python画圆形螺旋线-Python使用matplotlib绘制三维图形示例

    本文实例讲述了Python使用matplotlib绘制三维图形.分享给大家供大家参考,具体如下: 用二维泡泡图表示三维数据 泡泡的坐标2维,泡泡的大小三维,使用到的函数 plt.scatter(P[: ...

  9. 用python画动态三维轨迹_Python学习(一) —— matplotlib绘制三维轨迹图

    在研究SLAM时常常需要对其输出的位姿进行复现以检测算法效果,在ubuntu系统中使用Python可以很好的完成相关的工作. 一. Ubuntu下Python的使用 在Ubuntu下使用Python有 ...

最新文章

  1. 荣耀:想成功要敢于推翻重来
  2. Win2008 r2 下修改mysql data目录的方法
  3. spss和python stata matlab_(SPSS,Matlab,stata,Python)相关性?
  4. Tomcat最大线程数的设置
  5. 假期延长,抢票软件到底还行不?
  6. 关于uboot中tftp上传内存数据到tftp服务器
  7. jQuery中find和filter的区别
  8. 在线随机图片与网易云音乐解析API网页源码
  9. 怎样设置rotacast插件_老照片修复工具,降噪插件
  10. 浏览器的id_亚马逊账号关联因素之浏览器是重中之重-亚马逊运营必知
  11. 第五章 区块链怎么玩 [25]
  12. 详细介绍文本检索基准BEIR: A Heterogeneous Benchmark for Zero-shot Evaluation of Information Retrieval Models
  13. LAMP 技术简介(5)
  14. YouTube:如何删除油管频道Channel
  15. Python基础 F-03 函数-命名空间与作用域
  16. 背阔肌(05):史密斯机俯身划船
  17. java期末总结报告,请查收
  18. 凉凉怎么用计算机弹,用计算器弹凉凉乐谱 | 手游网游页游攻略大全
  19. HELLOWIN程序(窗口类)
  20. 课程设计-商店管理系统(二)----前端页面的制作(二)

热门文章

  1. 【从零开始学习 SystemVerilog】3.5、SystemVerilog 控制流——阻塞(Blocking)与非阻塞(Non-Blocking)
  2. 知乎话题读后感2——计算机专业真的那么完美吗?
  3. strcat函数--字符串连接函数
  4. 【免费分享】苹果cmsv10自适应粉色影视类型模板,已测试,完美无错,自适应手机端,只有影视模块,修复友情链接调用。
  5. 阿里云盘正式上架,亲测下载速度超 27MB/s!(附下载链接+邀请码)
  6. 51单片机 4个独立按键控制LED灯 (protues仿真)(C语言版)
  7. 阿里云自定义RAM策略之【对象存储服务(OSS)的权限】
  8. WPS表格打印-标题行重复
  9. r5 5600h和i5 11400h对比
  10. PPT英文翻译成中文工具有哪些?教你怎样把英文ppt翻译成中文