iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python绘制三维图
  • 371
分享到

python绘制三维图

pythonmatplotlib开发语言 2023-10-27 19:10:16 371人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

一、初始化 假设已经安装了matplotlib工具包。 利用matplotlib.figure.Figure创建一个图框: 1 2 3 4 import matplotlib.pyplot as plt from mpl_toolkit

一、初始化

假设已经安装了matplotlib工具包。

利用matplotlib.figure.Figure创建一个图框:

1

2

3

4

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3D import Axes3D

fig = plt.figure()

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

二、直线绘制(Line plots)

基本用法:

1

ax.plot(x,y,z,label=' ')

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import matplotlib as mpl

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

import matplotlib.pyplot as plt

mpl.rcParams['legend.fontsize'= 10

fig = plt.figure()

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

theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)

= np.linspace(-22100)

= z**2 + 1

= * np.sin(theta)

= * np.cos(theta)

ax.plot(x, y, z, label='parametric curve')

ax.legend()

plt.show()

三、散点绘制(Scatter plots)

基本用法:

1

ax.scatter(xs, ys, zs, s=20, c=None, depthshade=True*args, *kwargs)

  • xs,ys,zs:输入数据;
  • s:scatter点的尺寸
  • c:颜色,如c = 'r'就是红色;
  • depthshase:透明化,True为透明,默认为True,False为不透明
  • *args等为扩展变量,如maker = 'o',则scatter结果为’o‘的形状

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

import numpy as np

def randrange(n, vmin, vmax):

    '''

    Helper function to make an array of random numbers having shape (n, )

    with each number distributed UnifORM(vmin, vmax).

    '''

    return (vmax - vmin)*np.random.rand(n) + vmin

fig = plt.figure()

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

= 100

# For each set of style and range settings, plot n random points in the box

# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].

for c, m, zlow, zhigh in [('r''o'-50-25), ('b''^'-30-5)]:

    xs = randrange(n, 2332)

    ys = randrange(n, 0100)

    zs = randrange(n, zlow, zhigh)

    ax.scatter(xs, ys, zs, c=c, marker=m)

ax.set_xlabel('X Label')

ax.set_ylabel('Y Label')

ax.set_zlabel('Z Label')

plt.show()

四、线框图(Wireframe plots)

基本用法:

1

ax.plot_wireframe(X, Y, Z, *args, **kwargs)

  • X,Y,Z:输入数据
  • rstride:行步长
  • cstride:列步长
  • rcount:行数上限
  • ccount:列数上限

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

from mpl_toolkits.mplot3d import axes3d

import matplotlib.pyplot as plt

fig = plt.figure()

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

# Grab some test data.

X, Y, Z = axes3d.get_test_data(0.05)

# Plot a basic wireframe.

ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()

五、表面图(Surface plots)

基本用法:

1

ax.plot_surface(X, Y, Z, *args, **kwargs)

  • X,Y,Z:数据
  • rstride、cstride、rcount、ccount:同Wireframe plots定义
  • color:表面颜色
  • cmap:图层

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

from matplotlib import cm

from matplotlib.ticker import LinearLocator, FormatStrFormatter

import numpy as np

fig = plt.figure()

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

# Make data.

= np.arange(-550.25)

= np.arange(-550.25)

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

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

= np.sin(R)

# Plot the surface.

surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,

                       linewidth=0, antialiased=False)

# Customize the z axis.

ax.set_zlim(-1.011.01)

ax.zaxis.set_major_locator(LinearLocator(10))

ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

六、三角表面图(Tri-Surface plots)

基本用法:

1

ax.plot_trisurf(*args, **kwargs)

  • X,Y,Z:数据
  • 其他参数类似surface-plot

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

import numpy as np

n_radii = 8

n_angles = 36

# Make radii and angles spaces (radius r=0 omitted to eliminate duplication).

radii = np.linspace(0.1251.0, n_radii)

angles = np.linspace(02*np.pi, n_angles, endpoint=False)

# Repeat all angles for each radius.

angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)

# Convert polar (radii, angles) coords to cartesian (x, y) coords.

# (0, 0) is manually added at this stage,  so there will be no duplicate

# points in the (x, y) plane.

= np.append(0, (radii*np.cos(angles)).flatten())

= np.append(0, (radii*np.sin(angles)).flatten())

# Compute z to make the pringle surface.

= np.sin(-x*y)

fig = plt.figure()

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

ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)

plt.show()

七、等高线(Contour plots)

基本用法:

1

ax.contour(X, Y, Z, *args, **kwargs)

code:

1

2

3

4

5

6

7

8

9

10

11

from mpl_toolkits.mplot3d import axes3d

import matplotlib.pyplot as plt

from matplotlib import cm

fig = plt.figure()

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

X, Y, Z = axes3d.get_test_data(0.05)

cset = ax.contour(X, Y, Z, cmap=cm.coolwarm)

ax.clabel(cset, fontsize=9, inline=1)

plt.show()

二维的等高线,同样可以配合三维表面图一起绘制:

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

from mpl_toolkits.mplot3d import axes3d

from mpl_toolkits.mplot3d import axes3d

import matplotlib.pyplot as plt

from matplotlib import cm

fig = plt.figure()

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

X, Y, Z = axes3d.get_test_data(0.05)

ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)

cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)

cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)

cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

ax.set_xlabel('X')

ax.set_xlim(-4040)

ax.set_ylabel('Y')

ax.set_ylim(-4040)

ax.set_zlabel('Z')

ax.set_zlim(-100100)

plt.show()

也可以是三维等高线在二维平面的投影:

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

from mpl_toolkits.mplot3d import axes3d

import matplotlib.pyplot as plt

from matplotlib import cm

fig = plt.figure()

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

X, Y, Z = axes3d.get_test_data(0.05)

ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)

cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)

cset = ax.contourf(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)

cset = ax.contourf(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

ax.set_xlabel('X')

ax.set_xlim(-4040)

ax.set_ylabel('Y')

ax.set_ylim(-4040)

ax.set_zlabel('Z')

ax.set_zlim(-100100)

plt.show()

 八、Bar plots(条形图)

基本用法:

1

ax.bar(left, height, zs=0, zdir='z'*args, **kwargs

  • x,y,zs = z,数据
  • zdir:条形图平面化的方向,具体可以对应代码理解。

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

for c, z in zip(['r''g''b''y'], [3020100]):

    xs = np.arange(20)

    ys = np.random.rand(20)

    # You can provide either a single color or an array. To demonstrate this,

    # the first bar of each set will be colored cyan.

    cs = [c] * len(xs)

    cs[0= 'c'

    ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

plt.show()

九、子图绘制(subplot)

  A-不同的2-D图形,分布在3-D空间,其实就是投影空间不空,对应code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

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

# Plot a sin curve using the x and y axes.

= np.linspace(01100)

= np.sin(x * 2 * np.pi) / 2 + 0.5

ax.plot(x, y, zs=0, zdir='z', label='curve in (x,y)')

# Plot scatterplot data (20 2D points per colour) on the x and z axes.

colors = ('r''g''b''k')

= np.random.sample(20*len(colors))

= np.random.sample(20*len(colors))

c_list = []

for in colors:

    c_list.append([c]*20)

# By using zdir='y', the y value of these points is fixed to the zs value 0

# and the (x,y) points are plotted on the x and z axes.

ax.scatter(x, y, zs=0, zdir='y', c=c_list, label='points in (x,z)')

# Make legend, set axes limits and labels

ax.legend()

ax.set_xlim(01)

ax.set_ylim(01)

ax.set_zlim(01)

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

   B-子图Subplot用法

与MATLAB不同的是,如果一个四子图效果,如:

MATLAB:

1

2

3

subplot(2,2,1)

subplot(2,2,2)

subplot(2,2,[3,4])

python:

1

2

3

subplot(2,2,1)

subplot(2,2,2)

subplot(2,1,2)

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data

from matplotlib import cm

import numpy as np

# set up a figure twice as wide as it is tall

fig = plt.figure(figsize=plt.figaspect(0.5))

#===============

#  First subplot

#===============

# set up the axes for the first plot

ax = fig.add_subplot(221, projection='3d')

# plot a 3D surface like in the example mplot3d/surface3d_demo

= np.arange(-550.25)

= np.arange(-550.25)

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

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

= np.sin(R)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,

                       linewidth=0, antialiased=False)

ax.set_zlim(-1.011.01)

fig.colorbar(surf, shrink=0.5, aspect=10)

#===============

# Second subplot

#===============

# set up the axes for the second plot

ax = fig.add_subplot(2,1,2, projection='3d')

# plot a 3D wireframe like in the example mplot3d/wire3d_demo

X, Y, Z = get_test_data(0.05)

ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()

 补充:

文本注释的基本用法:

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

fig = plt.figure()

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

# Demo 1: zdir

zdirs = (None'x''y''z', (110), (111))

xs = (144941)

ys = (2581012)

zs = (1038918)

for zdir, x, y, z in zip(zdirs, xs, ys, zs):

    label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)

    ax.text(x, y, z, label, zdir)

# Demo 2: color

ax.text(900"red", color='red')

# Demo 3: text2D

# Placement 0, 0 would be the bottom left, 1, 1 would be the top right.

ax.text2D(0.050.95"2D Text", transform=ax.transAxes)

# Tweaking display region and labels

ax.set_xlim(010)

ax.set_ylim(010)

ax.set_zlim(010)

ax.set_xlabel('X axis')

ax.set_ylabel('Y axis')

ax.set_zlabel('Z axis')

plt.show()

来源地址:https://blog.csdn.net/SherryJin/article/details/130427848

--结束END--

本文标题: python绘制三维图

本文链接: https://www.lsjlt.com/news/500129.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python绘制三维图
    一、初始化 假设已经安装了matplotlib工具包。 利用matplotlib.figure.Figure创建一个图框: 1 2 3 4 import matplotlib.pyplot as plt from mpl_toolkit...
    99+
    2023-10-27
    python matplotlib 开发语言
  • Python怎么绘制三维图_Python绘制三维图教程
    1、首先打开python。 2、然后创建python文件。 3、引入相关python库。 4、接着定义数据。 5、定义三维函数。 6、再绘制三维图。 7、点击【Run】运行程序...
    99+
    2024-04-02
  • Python绘制三维图形
    需要安装numpy和matplotlib库,我都是pip库安装,这样比较简单。 import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.mpl...
    99+
    2023-01-31
    图形 Python
  • matlab绘图(三)绘制三维图像
    目录 一、绘制三维曲线  二、绘制三维曲面 1.meshgrid函数  2.mesh和surf函数 一、绘制三维曲线 1.最基本的绘制三维曲线的函数—plot3 plot3(x1,y1,z1, 选项 1,x2,y2,...
    99+
    2023-09-07
    matlab 开发语言 python
  • Python绘制三维图详解
    利用Python绘制三维图 目标: 绘制图像 z2 = x2 + y2 z^2 = x^2 + y^2 ...
    99+
    2023-09-14
    python numpy 开发语言
  • Python三维绘图--Matplotl
    Python三维绘图 在遇到三维数据时,三维图像能给我们对数据带来更加深入地理解。python的matplotlib库就包含了丰富的三维绘图工具。 1.创建三维坐标轴对象Axes3D 创建Axes3D主要有两种方式,一种是利用...
    99+
    2023-01-31
    Python Matplotl
  • python如何绘制三维函数图像图
    在python中使用matplotlib库绘制三维函数图像图,具体方法如下:import matplotlib as mplfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as npi...
    99+
    2024-04-02
  • Python+Matplotlib实现绘制三维折线图
    目录1.0简介2.0三维图画法与类型1、直线绘制(Line plots)2、散点绘制(Scatter plots)3、线框图(Wireframe plots)4、三角表面图(Tri-...
    99+
    2023-03-21
    Python Matplotlib绘制三维折线图 Python Matplotlib 三维折线图 Python Matplotlib
  • 怎么用Python+Matplotlib绘制三维折线图
    这篇文章主要介绍了怎么用Python+Matplotlib绘制三维折线图的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么用Python+Matplotlib绘制三维折线图文章都会有所收获,下面我们一起来看看吧...
    99+
    2023-07-05
  • 如何用java绘制三维图
    要使用Java绘制三维图,你可以使用Java的图形库和3D图形引擎。下面是一个简单的示例代码,可以帮助你开始绘制一个简单的三维图:`...
    99+
    2023-10-07
    java
  • python绘制三维图的详细新手教程
    目录一、初始化二、直线绘制(Line plots)三、散点绘制(Scatter plots)四、线框图(Wireframe plots)五、表面图(Surface plots)六、三...
    99+
    2024-04-02
  • Python使用Matplotlib绘制三维折线图(进阶篇)
    1.0简介: 三维图像技术是现在国际最先进的计算机展示技术之一,任何普通电脑只需要安装一个插件,就可以在网络浏览器中呈现三维的产品,不但逼真,而且可以动态展示产品的组合过程,特别适合远程浏览。 立体图视觉上层次分明色彩鲜艳,具有很强的视觉冲...
    99+
    2023-09-29
    python 开发语言
  • QT利用QPainter绘制三维饼状图
    目录一、项目介绍二、项目基本配置三、UI界面设置四、主程序实现4.1 widget.h头文件4.2 widget.cpp源文件五、效果演示一、项目介绍 本文介绍利用QPainter实...
    99+
    2024-04-02
  • 利用python绘制二三维曲面和矢量流线图
           为了实现不同数据的可视化,最近研究了python环境下的可视化方案,为后续的流体运动仿真模拟做好储备,由于python处理数据的便利性,导致目前很多后端处理或者可视化成图操作都在python中实现,比如前端是vue,加上简单的...
    99+
    2023-09-15
    python 开发语言 matplotlib
  • QT如何利用QPainter绘制三维饼状图
    这篇“QT如何利用QPainter绘制三维饼状图”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“QT如何利用QPainter绘...
    99+
    2023-07-02
  • 基于python,Matplotlib绘制函数的等高线与三维图像
    目录1. 网格点2. 等高线3. 三维图像本篇文章记录一下函数的等高线及其三维图像的可视化方法。 本例绘制的函数为: 1. 网格点   在绘制曲线之前,先了解一...
    99+
    2024-04-02
  • C++调用matlab引擎实现三维图的绘制
    目录VS2012设置matlab程序VS2012控制台程序运行结果说明VS2012设置 项目–项目属性–配置属性–VC++目录–包含目...
    99+
    2022-12-27
    C++ matlab绘制三维图 C++ 绘制三维图 matlab 三维图
  • 数据可视化——用python绘制气泡图、三维散点图、多重柱形图案例
    目录 前言 一、气泡图的绘制 1、什么是气泡图?他适用于什么数据? 2、图形效果展示 3、导入需要用到的库 4、读取要分析的数据 5、检查数据是否有问题 6、将要对比数据提取出来 7、画图 二、三维散点图的绘制 1、什么是三维散点图? 2、...
    99+
    2023-09-13
    python 3d
  • Word怎么绘制三线图
    要在Word中绘制三线图,可以按照以下步骤操作:1. 打开Word文档,点击“插入”选项卡,选择“图表”按钮。2. 在弹出的图表选择...
    99+
    2023-09-15
    Word
  • 如何基于python和Matplotlib绘制函数的等高线与三维图像
    这篇文章主要介绍了如何基于python和Matplotlib绘制函数的等高线与三维图像,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。本例绘制的函数为:1. 网格点&e...
    99+
    2023-06-26
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作