当前位置:网站首页>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()

边栏推荐
- 假设检验——《概率论与数理统计》第八章学习笔记
- A solution to the problem that variables cannot change dynamically when debugging in keil5
- MySQL: view with subquery in the from clause limit
- 官宣!第三届云原生编程挑战赛正式启动!
- Threejs Internet of things, 3D visualization of farm (III) model display, track controller setting, model moving along the route, model adding frame, custom style display label, click the model to obt
- Open graph protocol
- 机器学习 --- 神经网络
- 蛇形矩阵
- Longyuan war "epidemic" 2021 network security competition web easyjaba
- Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
猜你喜欢

Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)

【UNIAPP】系统热更新实现思路

蛇形矩阵

Label exchange experiment

【thingsboard】替换首页logo的方法

mxnet导入报各种libcudart*.so、 libcuda*.so找不到

Live broadcast preview | container service ack elasticity prediction best practice

【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)

Burpsuite grabs app packets

Components in protective circuit
随机推荐
函数(基本:参数,返回值)
Behavior perception system
Judge whether the stack order is reasonable according to the stack order
10 programming habits that web developers should develop
Common features of ES6
指针函数(基础)
[Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
Ctfshow web entry code audit
Neural network and deep learning Chapter 1: introduction reading questions
Introduction to RT thread kernel (4) -- clock management
揭秘技术 Leader 必备的七大清奇脑回路
函數(易錯)
Components in protective circuit
Bit operation skills
Threejs factory model 3DMAX model obj+mtl format, source file download
FFmepg使用指南
托管式服务网络:云原生时代的应用体系架构进化
Practice | mobile end practice
Is "golden nine and silver ten" the best time to find a job? Not necessarily