python 绘制3D图案例分享
作者:凭轩听雨199407 发布时间:2023-12-31 11:54:47
标签:python,绘制,3D图
1.散点图
代码
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
def randrange(n, vmin, vmax):
'''
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
'''
return (vmax - vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
ax.scatter(xs, ys, zs, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
输出:
输入的数据格式
这个输入的三个维度要求是三列长度一致的数据,可以理解为3个length相等的list。
用上面的scatter或者下面这段直接plot也可以。
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(h, z, t, '.', alpha=0.5)
plt.show()
输出:
2.三维表面 surface
代码
x = [12.7, 12.8, 12.9]
y = [1, 2, 3, 4]
temp = pd.DataFrame([[7,7,9,9],[2,3,4,5],[1,6,8,7]]).T
X,Y = np.meshgrid(x,y) # 形成网格化的数据
temp = np.array(temp)
fig = plt.figure(figsize=(16, 16))
ax = fig.gca(projection='3d')
ax.plot_surface(Y,X,temp,rcount=1, cmap=cm.plasma, linewidth=1, antialiased=False,alpha=0.5) #cm.plasma
ax.set_xlabel('zone', color='b', fontsize=20)
ax.set_ylabel('h2o', color='g', fontsize=20)
ax.set_zlabel('Temperature', color='r', fontsize=20)
output:
输入的数据格式
这里x和y原本都是一维list,通过np.meshgrid可以将其形成4X3的二维数据,如下图所示:
而第三维,得是4X3的2维的数据,才能进行画图
scatter + surface图形展示
3. 三维瀑布图waterfall
代码
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import numpy as np
axes=plt.axes(projection="3d")
def colors(arg):
return mcolors.to_rgba(arg, alpha=0.6)
verts = []
z1 = [1, 2, 3, 4]
x1 = np.arange(0, 10, 0.4)
for z in z1:
y1 = np.random.rand(len(x1))
y1[0], y1[-1] = 0, 0
verts.append(list(zip(x1, y1)))
# print(verts)
poly = PolyCollection(verts, facecolors=[colors('r'), colors('g'), colors('b'),
colors('y')])
poly.set_alpha(0.7)
axes.add_collection3d(poly, zs=z1, zdir='y')
axes.set_xlabel('X')
axes.set_xlim3d(0, 10)
axes.set_ylabel('Y')
axes.set_ylim3d(-1, 4)
axes.set_zlabel('Z')
axes.set_zlim3d(0, 1)
axes.set_title("3D Waterfall plot")
plt.show()
输出:
输入的数据格式
这个的输入我还没有完全搞懂,导致我自己暂时不能复现到其他数据,等以后懂了再回来补充。
4. 3d wireframe
code
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(
2, 1, figsize=(8, 12), subplot_kw={'projection': '3d'})
# Get the test data
X, Y, Z = axes3d.get_test_data(0.05)
# Give the first plot only wireframes of the type y = c
ax1.plot_wireframe(X, Y, Z, rstride=10, cstride=0)
ax1.set_title("Column (x) stride set to 0")
# Give the second plot only wireframes of the type x = c
ax2.plot_wireframe(X, Y, Z, rstride=0, cstride=10)
ax2.set_title("Row (y) stride set to 0")
plt.tight_layout()
plt.show()
output:
输入的数据格式
与plot_surface的输入格式一样,X,Y原本为一维list,通过np.meshgrid形成网格化数据。Z为二维数据。其中注意调节rstride、cstride这两个值实现行列间隔的调整。
自己试了下:
来源:https://blog.csdn.net/weixin_46870583/article/details/125318214


猜你喜欢
- 因为项目需要数据验证,看bootstrapValidator 还不错,就上手一直,完美兼容,话不多说。bootstrap:能够增加兼容性的强
- 通过配置VIP,在进行主备切换时,出现的报错信息:1.当主备节点当前binlog文件名称相同时,原主节点的position小于主备切换后的p
- 通过界面设计上是能手工操作的,无法达到我批量修改几千台服务器。 因为此了一个脚本来批量执行。 环境:redgate + mssql 2008
- 环境:Ubuntu14.04,tensorflow=1.4(bazel源码安装),Anaconda python=3.6声明变量主要有两种方
- Go+ 语言的安装和环境配置有些复杂,官方教程也没有写的很详细。通过控制台编写和运行 Go+ 程序很不方便。本文从零开始,详细介绍 Go+
- 本文通过Python3+PyQt5实现自定义部件–Counters自定 窗口部件。这个窗口是3*3的网格。本文有两个例子如下: /home/
- 使用python写爬虫时,优选selenium,由于PhantomJS因内部原因已经停止更新,最新版的selenium已经使用headles
- 将进程挂起(Suspend) 而非 阻塞(Block)如果用sleep() 进程将阻塞假设进程下有两个线程 那么这两个线程会继续运行要使进程
- mysql找不到my.ini文件问题描述刚刚在修改mysql默认配置的时候,发现找不到my.ini文件。按照其他搬运工的说法,打开隐藏的文件
- 单一数据读取方式:第一种:slice_input_producer()# 返回值可以直接通过 Session.run([images, la
- 1 概述在日常 Web 端产品的使用中,一般都会支持扫码登录,这种方式操作简单,相对传统的手机号登录等方式速度更快、安全性更高,还可以增加自
- SQL SERVER用来判断表或视图存在的语句在ORACLE中不能用,请问该怎么写。谢谢。答案exists(select tnam
- 写接口case时,有时需要对cae做一些共性的操作,最典型的场景如:获取case执行时间、打印log等。有没有一种办法来集中处理共性操作从而
- 通过前面内容的介绍,我们对 Surface 对象有了大体上的认识。Pygame 针对文本、图像、颜色提供了不同模块来生成它们各自的 Surf
- 语言的内存管理是语言设计的一个重要方面。它是决定语言性能的重要因素。无论是C语言的手工管理,还是Java的垃圾回收,都成为语言最重要的特征。
- 假设mysql安装在c:盘,mysql数据库的用户名是root,密码是123456,数据库名是database_name,在d:盘根目录下面
- sql 在使用中每次查询都会生成日志,但是如果你长久不去清理,可能整个硬都堆满哦,笔者就遇到这样的情况,直接网站后台都进不去了,今天到数据库
- 我们学习完推导式之后发现,推导式就是在容器中使用一个for循环而已,为什么没有元组推导式?原因就是“元组推导式&
- 为表和字段取别名阿文之前介绍过MySQL的分组查询、集合函数查询和嵌套子查询,在编写SQL语句时有的地方使用到AS关键字为查询结果中的某一列
- 本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下目录简介实现过程结语简介使用python实现pygame版的五