加入星计划,您可以享受以下权益:

  • 创作内容快速变现
  • 行业影响力扩散
  • 作品版权保护
  • 300W+ 专业用户
  • 1.5W+ 优质创作者
  • 5000+ 长期合作伙伴
立即加入
  • 正文
  • 推荐器件
  • 相关推荐
  • 电子产业图谱
申请入驻 产业图谱

Python中的3D绘图命令

2020/11/18
684
阅读需 13 分钟
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

很多情况下,为了能够观察到数据之间的内部的关系,可以使用绘图来更好的显示规律。

比如在下面的几张动图中,使用 matplotlib 中的三维显示命令,使得我们可以对于 logistic 回归网络的性能与相关参数有了更好的理解。

下面的动图显示了在训练网络时,不同的学习速率对于算法收敛之间的影响。

下面给出了绘制这些动态曲线的相关的 python 指令:

➤01 3D plot


1. 基本语法

在安装 matplotlib 之后,自动安装有 mpl_toolkits.mplot3d。

#Importing Libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

#3D Plotting
fig = plt.figure()
ax = plt.axes(projection="3d")

#Labeling
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')

plt.show()

2.Python Cmd

使用 pythoncmd 插入相应的语句。

3. 举例

(1) Ex1

#!/usr/local/bin/python
# -*- coding: gbk -*-
#******************************
# TEST2.PY                     -- by Dr. ZhuoQing 2020-11-16
#
# Note:
#******************************

from headm import *
from mpl_toolkits.mplot3d import axes3d

ax = plt.axes(projection='3d')
x = [1,2,3,4,5,6,7,8,9]
y = [2,3,4,6,7,8,9,5,1]
z = [5,6,2,4,8,6,5,6,1]

ax.plot3D(x,y,z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')

plt.show()

#------------------------------------------------------------
#        END OF FILE : TEST2.PY
#******************************

▲ 3D plot 的演示

(2) Ex2

from mpl_toolkits.mplot3d import axes3d

ax = plt.axes(projection='3d')

angle = linspace(0, 2*pi*5, 400)
x = cos(angle)
y = sin(angle)
z = linspace(0, 5, 400)

ax.plot3D(x,y,z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')

plt.show()

▲ 3D 绘制的例子

(3) Ex3

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)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()

plt.show()

➤02 绘制 Scatter


利用和上面的相同的绘制命令,将原来的 plot3D 修改成为 scatter 即可。

from mpl_toolkits.mplot3d import axes3d

ax = plt.axes(projection='3d')

angle = linspace(0, 2*pi*5, 40)
x = cos(angle)
y = sin(angle)
z = linspace(0, 5, 40)

ax.scatter(x,y,z, color='b')
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')

plt.show()

▲ Scatter 的例子

➤03 绘制 3D Surface


(1) Ex1

▲ 3D surface 例子

#!/usr/local/bin/python
# -*- coding: gbk -*-
#******************************
# TEST2.PY                     -- by Dr. ZhuoQing 2020-11-16
#
# Note:
#******************************

from headm import *
from mpl_toolkits.mplot3d import axes3d

ax = plt.axes(projection='3d')

x = arange(-5, 5, 0.1)
y = arange(-5, 5, 0.1)
x,y = meshgrid(x, y)
R = sqrt(x**2+y**2)
z = sin(R)

ax.plot_surface(x, y, z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')

plt.show()

#------------------------------------------------------------
#        END OF FILE : TEST2.PY
#******************************

▲ 3D 绘制 Surface

▲ 绘制 3D 球表面

(2) 举例

'''
***********
3D surface (color map)
***********

Demonstrates plotting a 3D surface colored with the coolwarm color map.
The surface is made opaque by using antialiased=False.

Also demonstrates using the LinearLocator and custom formatting for the
z axis tick labels.
'''

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.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = 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.01, 1.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()

▲ 彩色表面绘制

推荐器件

更多器件
器件型号 数量 器件厂商 器件描述 数据手册 ECAD模型 风险等级 参考价格 更多信息
LMC7101BIM5/NOPB 1 Texas Instruments Single, 15.5-V, 1.1-MHz operational amplifier 5-SOT-23 -40 to 85

ECAD模型

下载ECAD模型
$1.93 查看
PTN78020WAH 1 Texas Instruments 6-A Wide-Input Voltage Adjustable Switching Regulator 7-Through-Hole Module -40 to 85

ECAD模型

下载ECAD模型
$37.59 查看
INA2133UA 1 Texas Instruments Dual 1.5-MHz, 450-µV offset, 0.95-mA power, precision difference amplifier 14-SOIC -55 to 125

ECAD模型

下载ECAD模型
$5.56 查看

相关推荐

电子产业图谱

公众号TsinghuaJoking主笔。清华大学自动化系教师,研究兴趣范围包括自动控制、智能信息处理、嵌入式电子系统等。全国大学生智能汽车竞赛秘书处主任,技术组组长,网称“卓大大”。