markdown matplotlib绘制三维曲面和散点图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()
```
以上是关于markdown matplotlib绘制三维曲面和散点图的主要内容,如果未能解决你的问题,请参考以下文章
python matplotlib绘制 3D图像专题 (三维柱状图曲面图散点图曲线图合集)
[Python系列-22]:Python之人工智能 - 基本工具 - 6- 绘制二元函数的三维曲面图
利用Axes3D绘制三维性能曲面
基于matplotlib的数据可视化 - 三维曲面图gca
matlab 用surf绘制出的曲面上颜色代表的含义
matlab三维阴影曲面绘图函数surfsurfcfurflmatlab图行绘制六