matplotlib画三维散点图和曲面图

本篇博文主要来介绍如何利用matplotlib库画三维散点图和曲面图。

三维散点图

numpy的random.rand(d0,d1,d2…)函数根据维度生成服从0~1均匀分布的随机样本点;
x、y、z均为一维数组

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np

def randrange(n, vmin, vmax):
    return (vmax - vmin)*np.random.rand(n) 

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

n = 100
x = randrange(n,0,20)
y = randrange(n,0,20)
z = randrange(n,-30,-5)

ax.scatter(x,y,z,marker="^")
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
ax.set_zlabel("Z Label")

ax.set_title("3D scatter plot")
plt.show()

在这里插入图片描述

3D曲面图

在这里需要先利用numpy的meshgrid()函数生成x和y网格点坐标(x,y),也就是说把两个数组的笛卡尔积内的元素的第一二个坐标分别放入两个矩阵中。画曲面图时,z必须是二维的。

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

x = np.arange(-5,5,0.5)
y = np.arange(-5,5,0.5)
x,y = np.meshgrid(x, y)
z = np.sqrt(x**2 + y**2)

ax.plot_surface(x,y,z)
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
ax.set_zlabel("Z Label")

ax.set_title("3D surface plot")
plt.show()

在这里插入图片描述
在画曲面图的时候,我们还可以给图像加上颜色和colorbar

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

x = np.arange(-5,5,0.5)
y = np.arange(-5,5,0.5)
x,y = np.meshgrid(x, y)
z = np.sqrt(x**2 + y**2)

#rstride表示行步长,cmap给曲面加上色彩
surf = ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=plt.cm.viridis)
#colorbar可以给图像添加一个颜色条
fig.colorbar(surf)
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
ax.set_zlabel("Z Label")

ax.set_title("3D surface plot")
plt.show()

在这里插入图片描述

### 回答1: PythonMatplotlib库可以用来绘制三维形。要绘制三维形,需要使用mpl_toolkits.mplot3d模块。这个模块提供了一个名为Axes3D的类,它允许我们在三维空间中绘制形。我们可以使用这个类的方法来绘制各种类型的三维形,如散点图、线曲面等。要使用这个模块,我们需要在代码中导入它,如下所示: from mpl_toolkits.mplot3d import Axes3D 然后,我们可以创建一个Axes3D对象,并使用它的方法来绘制三维形。例如,要绘制一个三维散点图,可以使用以下代码: import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = np.random.normal(, 1, 100) y = np.random.normal(, 1, 100) z = np.random.normal(, 1, 100) ax.scatter(x, y, z) plt.show() 这个代码会生成一个随机生成的三维散点图。我们可以使用类似的方法来绘制其他类型的三维形。 ### 回答2: Python matplotlib是一个强大的数据可视化库,它可以帮助我们将复杂的数据可视化成漂亮的形。 在Python matplotlib中,我们可以使用mplot3d子包来绘制三维。mplot3d提供了许多三维形绘制的功能,例如绘制散点图、折线、柱状等。 下面我们以绘制三维散点图为例介绍如何使用Python matplotlib三维。 首先需要导入必要的模块: ``` import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D ``` 接下来,我们需要创建一些随机数据作为示例。这里我们使用NumPy库创建一个均值为0,标准差为1的正态分布样本: ``` np.random.seed(42) n = 100 x = np.random.normal(0, 1, n) y = np.random.normal(0, 1, n) z = np.random.normal(0, 1, n) ``` 然后,我们使用subplot中的projection参数创建3D: ``` fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ``` 接着,我们可以使用scatter函数绘制散点图。此处的x,y和z是数据点的坐标,c用来表示颜色,marker表示点的形状: ``` ax.scatter(x, y, z, c='r', marker='o') ``` 最后,我们通过设置轴标签和标题来完善形: ``` ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') ax.set_title('3D Scatter Plot') ``` 完整的代码如下: ``` import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 创建随机数据 np.random.seed(42) n = 100 x = np.random.normal(0, 1, n) y = np.random.normal(0, 1, n) z = np.random.normal(0, 1, n) # 创建3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # 绘制散点图 ax.scatter(x, y, z, c='r', marker='o') # 添加轴标签和标题 ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') ax.set_title('3D Scatter Plot') # 显示形 plt.show() ``` 运行结果如下: ![3D Scatter Plot](https://pic4.zhimg.com/80/v2-669225dcfbb8cce79fe3607f3f597305_1440w.jpg) 总之,使用Pythonmatplotlib库可以很方便地绘制三维形,让我们更好地了解和理解数据。 ### 回答3: Pythonmatplotlib库是一个非常强大的可视化库,可以用来三维Matplotlib提供了一个模块mpl_toolkits.mplot3d,该模块可以用于创建三维表,它允许我们在三个维度下绘制数据。我们可以选择绘制表面,散点图等信息,还可以设置轴标签和标题等信息。 首先,我们需要导入必要的库,包括matplotlib,mpl_toolkits.mplot3d和numpy库: ``` python import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np ``` 接下来,我们可以创建一个3D形对象: ``` python fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ``` 在创建形对象之后,我们可以使用numpy库生成一些随机的三维数据: ``` python x = np.random.randint(0, 10, size=50) y = np.random.randint(0, 10, size=50) z = np.random.randint(0, 10, size=50) ``` 我们可以将这些数据可视化成三维散点图: ``` python ax.scatter(x, y, z) plt.show() ``` 注意,我们还可以增加一些其他的3D形类型,例如平面曲面,线等,这些都可以通过mpl_toolkits.mplot3d模块中的其他函数来完成。 除此之外,我们还可以对横轴,纵轴和深度轴进行标注和命名: ``` python ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_zlabel('Z-axis') ``` 最后,我们还可以添加一些标题和例: ``` python fig.suptitle('3D Scatter Plot') ax.legend() ``` 综上所述,我们可以使用matplotlib和mpl_toolkits.mplot3d模块来绘制三维散点图和其他三维形。我们还可以对横轴,纵轴和深度轴进行标注和命名,并添加一些标题和例。这使得我们可以更容易地理解数据和抽象概念的关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值