当前位置:网站首页>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()
边栏推荐
- Study notes 7
- Un réveil de l'application B devrait être rapide
- 美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
- A real day for Beijing programmers!!!!!
- 【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
- 揭秘技术 Leader 必备的七大清奇脑回路
- Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
- PHP reads the INI file and writes the modified content
- web资源部署后navigator获取不到mediaDevices实例的解决方案(navigator.mediaDevices为undefined)
- WeNet:面向工业落地的E2E语音识别工具
猜你喜欢
How to get the first few pieces of data of each group gracefully
防护电路中的元器件
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
小程序中实现文章的关注功能
快手、抖音、视频号交战内容付费
MacBook installation postgresql+postgis
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
Fonction (sujette aux erreurs)
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
随机推荐
Number of possible stack order types of stack order with length n
如何进行「小步重构」?
Interview related high-frequency algorithm test site 3
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
快手、抖音、视频号交战内容付费
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
【虚幻引擎UE】实现测绘三脚架展开动画制作
Web开发人员应该养成的10个编程习惯
[finebi] the process of making custom maps using finebi
A应用唤醒B应该快速方法
Introduction to RT thread kernel (5) -- memory management
Scheduling system of kubernetes cluster
[phantom engine UE] the difference between running and starting, and the analysis of common problems
A application wakes up B should be a fast method
概率论与数理统计考试重点复习路线
Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
Neural networks and deep learning Chapter 2: machine learning overview reading questions
Uncover the seven quirky brain circuits necessary for technology leaders
函數(易錯)