当前位置:网站首页>Matplotlib draws three-dimensional scatter and surface graphs
Matplotlib draws three-dimensional scatter and surface graphs
2022-07-05 04:26:00 【Bai Yanling】
This blog post mainly introduces how to use matplotlib The library draws three-dimensional scatter diagrams and surface diagrams .
Three dimensional scatter plot
numpy Of random.rand(d0,d1,d2…) Function generates obedience according to dimension 0~1 Uniformly distributed random sample points ;
x、y、z They are all one-dimensional arrays
# -*- 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 Surface graph
Here we need to use numpy Of meshgrid() Function generation x and y Grid point coordinates (x,y), That is to say, put the first and second coordinates of the elements in the Cartesian product of two arrays into two matrices respectively . When drawing a surface ,z It has to be two-dimensional .
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()

When drawing a surface , We can also add color and 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 Indicates the line step size ,cmap Color the surface
surf = ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=plt.cm.viridis)
#colorbar You can add a color bar to the image
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()

边栏推荐
- Discussion on the dimension of confrontation subspace
- 【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
- Neural network and deep learning Chapter 1: introduction reading questions
- 机器学习 --- 神经网络
- 这是一个不确定的时代
- 【虚幻引擎UE】运行和启动的区别,常见问题分析
- Practice | mobile end practice
- 解密函数计算异步任务能力之「任务的状态及生命周期管理」
- 如何进行「小步重构」?
- Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
猜你喜欢

level18

Live broadcast preview | container service ack elasticity prediction best practice

【科普】热设计基础知识:5G光器件之散热分析
![[phantom engine UE] realize the animation production of mapping tripod deployment](/img/89/351641c3da7e2acdbf389bc298b75e.png)
[phantom engine UE] realize the animation production of mapping tripod deployment

Label exchange experiment

Network security - record web vulnerability fixes

C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)

Fonction (sujette aux erreurs)

Uncover the seven quirky brain circuits necessary for technology leaders

TPG x AIDU | AI leading talent recruitment plan in progress!
随机推荐
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
Ctfshow 2022 Spring Festival welcome (detailed commentary)
Common features of ES6
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
[Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
【UNIAPP】系统热更新实现思路
Ctfshow web entry code audit
Mixed compilation of C and CC
[untitled]
Burpsuite grabs app packets
【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
TPG x AIDU|AI领军人才招募计划进行中!
Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
如何优雅的获取每个分组的前几条数据
PR video clip (project packaging)
A應用喚醒B應該快速方法