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
0
投稿
猜你喜欢
- 活在当下的程序员应该都听过“面向对象编程”一词,也经常有人问能不能用一句话解释下什么是“面向对象编程”,我们先来看看比较正式的说法。把一组数
- 今天遇到一个奇怪的现象,使用tensorflow-gpu的时候,出现内存超额~~如果我训练什么大型数据也就算了,关键我就写了一个y=W*x…
- 两个并发事务同时访问数据库表相同的行时,可能存在以下三个问题:1、幻想读:事务T1读取一条指定where条件的语句,返回结果集。此时事务T2
- 本文为大家分享了童年掌机游戏,基于pygame实现打砖块的具体代码,供大家参考,具体内容如下项目背景: 小时候有过一个掌机游戏机,里面有俄罗
- 本文实例讲述了python通过函数属性实现全局变量的方法。分享给大家供大家参考。具体分析如下:python的函数可以定义属性,而且是全局的,
- 语音识别是人工智能中的一个领域,它允许计算机理解人类语音并将其转换为文本。该技术用于 Alexa 和各种聊天机器人应用程序等设备。而我们最常
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 2 - Selectors如果你还没有准备好,请先
- 不使用int()函数的情况下把字符串转换为数字,如把字符串"12345"转换为数字12345。方法一:利用str函数既然
- 1、Numpy ndarray对象numpy ndarray对象是一个n维数组对象,ndarray只能存储一系列相同元素。#一维数组[1,2
- 本文将介绍使用Dreamweaver来制作滑动菜单的方法,言归正传,废话少说。准备工作如下: 1. 在dw中新建一个空白文档(或者打开你要添
- Python在程序并行化方面多少有些声名狼藉。撇开技术上的问题,例如线程的实现和GIL,我觉得错误的教学指导才是主要问题。常见的经典Pyth
- 通过优化CSS代码,减小对系统资源的占用。自己整理出几个能减少系统资源占用的CSS写法,要优化网站的页面加载速度,这些注意点不能忽视!一、尽
- 我们这里所说的head区域,是指页页html代码的<head>和</head>之间的内容。在以前的文章中,主要介绍了
- 本文实例总结了php处理json格式数据的方法。分享给大家供大家参考,具体如下:1.json简介:何为json?简 单地说,JSON 可以将
- 代码如下:'************************************ '截取文字长度函数,支持UT
- 一个动态数组 a,如果你已经使用redim 语句给它设定了大小,那么在此之后使用 ubound(a) 就可以得到它的上边界。如果你没有使用
- memcache 的工作就是在专门的机器的内存里维护一张巨大的hash表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率,减
- 本文实例为大家分享了python自动化生成IOS的图标,供大家参考,具体内容如下每次上架之前都要生成十几个图片感觉无聊麻烦,考虑使用脚本处理
- 前言python 作为一种脚本语言, 已经逐渐大量用于 web 后台开发中, 而基于 python 的 web 应用程序框架也越来越多, B
- 1、设置web.config文件。以下为引用的内容:<system.web> ...... <globalization