当前位置:网站首页>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应用唤醒B应该快速方法
- FFmepg使用指南
- Mxnet imports various libcudarts * so、 libcuda*. So not found
- 自动语音识别(ASR)研究综述
- Study notes 7
- Threejs implements labels and displays labels with custom styles
- Official announcement! The third cloud native programming challenge is officially launched!
- 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
- Neural networks and deep learning Chapter 3: linear model reading questions
- Network security - record web vulnerability fixes
猜你喜欢
Function (basic: parameter, return value)
[phantom engine UE] realize the animation production of mapping tripod deployment
level17
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
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)
Serpentine matrix
网络安全-记录web漏洞修复
假设检验——《概率论与数理统计》第八章学习笔记
Raki's notes on reading paper: code and named entity recognition in stackoverflow
Function (error prone)
随机推荐
函数(易错)
FFmepg使用指南
Is "golden nine and silver ten" the best time to find a job? Not necessarily
电源管理总线 (PMBus)
Official announcement! The third cloud native programming challenge is officially launched!
mysql的七种join连接查询
[untitled]
指针函数(基础)
Seven join join queries of MySQL
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)
直播预告 | 容器服务 ACK 弹性预测最佳实践
CSDN正文自动生成目录
You Li takes you to talk about C language 7 (define constants and macros)
kubernetes集群之调度系统
【thingsboard】替换首页logo的方法
Label exchange experiment
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
Uncover the seven quirky brain circuits necessary for technology leaders
A應用喚醒B應該快速方法
Basic analysis of IIC SPI protocol