matplotlib怎么画三维散点图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matplotlib怎么画三维散点图相关的知识,希望对你有一定的参考价值。

参考技术A matplotlib实现了大多数matlab的函数,scatter3
去这一页找到你需要的源码下下来改改就行了
#scatter-plots

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

Create a scatter plot.
Argument Description
xs, ys Positions of data points.
zs Either an array of the same length as xs and ys or a single value to place all points in the 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 the same length as x and y.
c a 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 the cmap 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.

markdown matplotlib绘制三维曲面和散点图

更多例子https://matplotlib.org/gallery.html


曲面图
```
# -*- coding: utf-8 -*-

from matplotlib import pyplot as plt
import numpy as np
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)

# 具体函数方法可用 help(function) 查看,如:help(ax.plot_surface)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')

plt.show()
```


散点图

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

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

data = np.random.randint(0, 255, size=[40, 40, 40])

x, y, z = data[0], data[1], data[2]
ax = plt.subplot(111, projection='3d')  # 创建一个三维的绘图工程
#  将数据点分成三部分画,在颜色上有区分度
ax.scatter(x[:10], y[:10], z[:10], c='y')  # 绘制数据点
ax.scatter(x[10:20], y[10:20], z[10:20], c='r')
ax.scatter(x[30:40], y[30:40], z[30:40], c='g')

ax.set_zlabel('Z')  # 坐标轴
ax.set_ylabel('Y')
ax.set_xlabel('X')
plt.show()
```



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

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

fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-100, 100, 1)
Y = np.arange(-100, 100, 1)
X, Y = np.meshgrid(X, Y)
Z = 50

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')

data = np.random.randint(-100, 100, size=[20, 20, 20])
data1 = np.random.randint(-100, 0, size=[20, 20, 20])
data2 = np.random.randint(0, 100, size=[20, 20, 20])

x1, y1 = data[0], data[1]
z1 = data1[2]
x2, y2 = data[0], data[1]
z2 = data2[2]

ax.scatter(x1[:10], y1[:10], z1[:10], c='y')
ax.scatter(x2[10:20], y2[10:20], z2[10:20], c='r')

plt.show()
```

以上是关于matplotlib怎么画三维散点图的主要内容,如果未能解决你的问题,请参考以下文章

三维散点图:Python 之matplotlib篇

用matplotlib画散点图

matplotlib-直方图、箱体图、散点图

python matplotlib模块——绘制三维图形三维数据散点图(转)

markdown matplotlib绘制三维曲面和散点图

如何将颜色作为第三维添加到 matplotlib 散点图? [复制]