python 绘制3D图案例分享

 更新时间:2022年07月31日 14:58:13   作者:凭轩听雨199407  
这篇文章主要介绍了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这两个值实现行列间隔的调整。

自己试了下:

到此这篇关于python 绘制3D图案例分享的文章就介绍到这了,更多相关python 绘制3D图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python列表的定义及使用

    Python列表的定义及使用

    这篇文章主要介绍了Python列表的定义及使用,在Python中,列表是由一系列元素按照特定的顺序构成的数据结构,也就是说列表类型的变量可以存储多个数据,且可以重复,下面一起进入文章学习学习内容,需要的朋友可以参考一下
    2021-11-11
  • python作图基础之plt.contour实例详解

    python作图基础之plt.contour实例详解

    contour和contourf都是画三维等高线图的,下面这篇文章主要给大家介绍了关于python作图基础操作之plt.contour的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • Python中的super()面向对象编程

    Python中的super()面向对象编程

    这篇文章主要介绍了Python的面向对象编程 super,super在Pyhon是一个特殊的的类,想具体了解的朋友请参考下面文章内容
    2021-09-09
  • pyftplib中文乱码问题解决方案

    pyftplib中文乱码问题解决方案

    这篇文章主要介绍了pyftplib中文乱码问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • Python3中的re.findall()方法及re.compile()

    Python3中的re.findall()方法及re.compile()

    这篇文章主要介绍了Python3中的re.findall()方法及re.compile(),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式

    python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式

    这篇文章主要介绍了python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • 使用Pandas 实现MySQL日期函数的解决方法

    使用Pandas 实现MySQL日期函数的解决方法

    这篇文章主要介绍了用Pandas 实现MySQL日期函数的效果,Python是很灵活的语言,达成同一个目标或有多种途径,我提供的只是其中一种解决方法,需要的朋友可以参考下
    2023-02-02
  • python实现合并两个有序列表的示例代码

    python实现合并两个有序列表的示例代码

    这篇文章主要介绍了python实现合并两个有序列表的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • python,Django实现的淘宝客登录功能示例

    python,Django实现的淘宝客登录功能示例

    这篇文章主要介绍了python,Django实现的淘宝客登录功能,结合实例形式分析了Django框架基于淘宝接口的登录功能相关操作技巧,需要的朋友可以参考下
    2019-06-06
  • Flask接收上传图片方法实现

    Flask接收上传图片方法实现

    本文主要介绍了Flask接收上传图片方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07

最新评论