• Python绘制3D曲面图


    👽发现宝藏

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。【点击进入巨牛的人工智能学习网站】。

    探索Python中绘制3D曲面图的艺术

    数据可视化的世界中,3D曲面图是一种强大的工具,能够将复杂的数据模式以清晰直观的方式展现出来。Python提供了多种库和工具,使得创建和定制3D曲面图变得简单而令人兴奋。本文将介绍如何使用Python中的Matplotlib和mpl_toolkits.mplot3d库绘制令人印象深刻的3D曲面图。

    准备工作

    首先,确保你的Python环境中安装了Matplotlib库。如果还没有安装,可以使用pip进行安装:

    pip install matplotlib
    
    • 1

    导入必要的库

    在开始之前,让我们先导入必要的库:

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    • 1
    • 2
    • 3

    创建数据

    在我们绘制3D曲面图之前,我们需要创建一些数据。我们可以使用NumPy库来生成一些数据集。这里我们以一个简单的函数为例:

    def f(x, y):
        return np.sin(np.sqrt(x**2 + y**2))
    
    • 1
    • 2

    创建网格点

    接下来,我们需要定义我们要在曲面上显示的坐标点。我们可以使用numpy.meshgrid函数来生成这些点:

    x = np.linspace(-5, 5, 100)
    y = np.linspace(-5, 5, 100)
    x, y = np.meshgrid(x, y)
    z = f(x, y)
    
    • 1
    • 2
    • 3
    • 4

    绘制3D曲面图

    现在,我们已经准备好绘制我们的3D曲面图了。我们可以使用Matplotlib的plot_surface函数来实现:

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(x, y, z, cmap='viridis')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4

    定制曲面图

    我们可以通过一些可选参数来定制我们的曲面图,以使其更具吸引力。例如,我们可以添加轮廓线、更改颜色映射、更改视角等:

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none')  # 添加轮廓线
    ax.view_init(45, 60)  # 更改视角
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    添加标签和标题

    在创建3D曲面图时,添加标签和标题是非常重要的,这样可以使图形更具可读性和易理解性。我们可以通过调用set_xlabelset_ylabelset_zlabel方法来添加坐标轴标签,以及使用set_title方法添加标题:

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none')
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title('3D Surface Plot')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    添加色标

    为了更清楚地理解曲面图中数值的含义,我们可以添加一个色标。色标可以显示颜色与数值之间的对应关系。我们可以使用colorbar方法添加色标:

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    surf = ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none')
    fig.colorbar(surf, shrink=0.5, aspect=5)  # 添加色标
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title('3D Surface Plot with Colorbar')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    完整示例代码

    下面是一个完整的示例代码,包括了所有的定制选项:

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    def f(x, y):
        return np.sin(np.sqrt(x**2 + y**2))
    
    x = np.linspace(-5, 5, 100)
    y = np.linspace(-5, 5, 100)
    x, y = np.meshgrid(x, y)
    z = f(x, y)
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    surf = ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none')
    fig.colorbar(surf, shrink=0.5, aspect=5)  # 添加色标
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title('3D Surface Plot with Colorbar')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    通过这些定制选项,我们可以创建出更具信息量和美观度的3D曲面图。掌握这些技巧后,你将能够根据自己的需求创建出各种各样的3D可视化效果。

    添加透明度和阴影

    除了标签、标题和色标之外,我们还可以通过调整透明度和阴影效果来增强3D曲面图的视觉效果。透明度可以使得曲面图中的数据分布更加清晰,而阴影则可以增加立体感。

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    surf = ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none', alpha=0.7)  # 调整透明度
    fig.colorbar(surf, shrink=0.5, aspect=5)  
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title('3D Surface Plot with Colorbar and Transparency')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    此外,我们还可以通过设置shade参数为True来添加阴影效果:

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    surf = ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none', shade=True)  # 添加阴影
    fig.colorbar(surf, shrink=0.5, aspect=5)  
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title('3D Surface Plot with Colorbar and Shadow')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    其他定制选项

    除了上述提到的定制选项外,Matplotlib还提供了许多其他参数和方法,用于进一步定制3D曲面图,如修改坐标轴范围、设置视角、更改颜色映射等。你可以根据具体的需求来选择合适的选项进行定制。

    进一步定制颜色映射

    在3D曲面图中,颜色映射是一种重要的视觉工具,它能够帮助我们更直观地理解数据的分布和变化。除了使用内置的颜色映射外,我们还可以自定义颜色映射以满足特定需求。

    from matplotlib.colors import Normalize
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    # 自定义颜色映射
    norm = Normalize(vmin=np.min(z), vmax=np.max(z))
    colors = plt.cm.cool(norm(z))
    
    surf = ax.plot_surface(x, y, z, facecolors=colors, shade=False)
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title('Customized 3D Surface Plot with Color Mapping')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    添加网格线

    有时候,我们希望在3D曲面图中添加网格线以帮助更好地理解数据的分布和形状。我们可以通过设置grid参数为True来添加网格线:

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    surf = ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none')
    fig.colorbar(surf, shrink=0.5, aspect=5)  
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title('3D Surface Plot with Colorbar and Grid')
    ax.grid(True)  # 添加网格线
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    总结

    本文介绍了如何使用Python中的Matplotlib库创建令人印象深刻的3D曲面图,并展示了一系列定制选项,包括标签、标题、色标、透明度、阴影、颜色映射和网格线等。通过学习这些技巧,我们能够更好地展示和理解数据,从而为数据可视化工作提供了丰富的可能性。

    通过创建3D曲面图,我们可以将复杂的数据模式以直观、清晰的方式呈现出来,帮助我们发现数据中的规律和趋势。定制选项使我们能够根据特定需求调整图形的外观和表现形式,从而更好地满足我们的分析和展示需求。

    总而言之,掌握如何创建和定制3D曲面图是数据科学和数据可视化领域中的重要技能之一。希望本文能够为你提供一些启发和帮助,激发你对数据可视化的兴趣,并在实践中不断探索和创新。
    在这里插入图片描述

  • 相关阅读:
    在Debian系统上安装StoneDB数据库
    Java 实习生(月薪 3k-5k 水平)应具备哪些知识、能力?给学弟学妹们支招
    Java反序列化:CC1链 详解
    算法 分糖果-(贪心)
    Rust错误处理简介
    Python 多重继承时metaclass conflict问题解决与原理探究
    QT 5.8
    vue3中若v-model绑定的响应字段出现三级,该如何实现rules验证规则
    聊一聊异构系统间数据一致性
    STM32使用PWM控制舵机
  • 原文地址:https://blog.csdn.net/weixin_52908342/article/details/138173792