iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >怎么用Python+Matplotlib绘制三维折线图
  • 625
分享到

怎么用Python+Matplotlib绘制三维折线图

2023-07-05 14:07:29 625人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

这篇文章主要介绍了怎么用python+Matplotlib绘制三维折线图的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么用Python+Matplotlib绘制三维折线图文章都会有所收获,下面我们一起来看看吧

这篇文章主要介绍了怎么用python+Matplotlib绘制三维折线图的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么用Python+Matplotlib绘制三维折线图文章都会有所收获,下面我们一起来看看吧。

1.0简介

三维图像技术是现在国际最先进的计算机展示技术之一,任何普通电脑只需要安装一个插件,就可以在网络浏览器中呈现三维的产品,不但逼真,而且可以动态展示产品的组合过程,特别适合远程浏览。

立体图视觉上层次分明色彩鲜艳,具有很强的视觉冲击力,让观看的人驻景时间长,留下深刻的印象。立体图给人以真实、栩栩如生,人物呼之欲出,有身临其境的感觉,有很高的艺术欣赏价值。

2.0三维图画法与类型

首先要安装Matplotlib库可以使用pip:

pip install matplotlib

假设已经安装了matplotlib工具包。

利用matplotlib.figure.Figure创建一个图框:

import matplotlib.pyplot as pltfrom mpl_toolkits.mplot3D import Axes3Dfig = plt.figure()ax = fig.add_subplot(111, projection='3d')

怎么用Python+Matplotlib绘制三维折线图

1、直线绘制(Line plots)

基本用法:ax.plot(x,y,z,label=' ')

代码如下:

import matplotlib as mplfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as npimport matplotlib.pyplot as plt mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure()ax = fig.add_subplot(projection='3d')theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)z = np.linspace(-2, 2, 100)r = z ** 2 + 1x = r * np.sin(theta)y = r * np.cos(theta)ax.plot(x, y, z, label='parametric curve')ax.legend()

效果如下:

怎么用Python+Matplotlib绘制三维折线图

2、散点绘制(Scatter plots)

基本语法:

ax.scatter(xs, ys, zs, s=20, c=None, depthshade=True, *args, *kwargs)

代码大意为:

  • xs,ys,zs:输入数据;

  • s:scatter点的尺寸

  • c:颜色,如c = 'r’就是红色;

  • depthshase:透明化,True为透明,默认为True,False为不透明

  • *args等为扩展变量,如maker = ‘o’,则scatter结果为’o‘的形状

示例代码:

from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltimport numpy as np  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 c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:    xs = randrange(n, 23, 32)    ys = randrange(n, 0, 100)    zs = randrange(n, zlow, zhigh)    ax.scatter(xs, ys, zs, c=c, marker=m) ax.set_xlabel('X Label')ax.set_ylabel('Y Label')ax.set_zlabel('Z Label') plt.show()

效果:

怎么用Python+Matplotlib绘制三维折线图

3、线框图(Wireframe plots)

基本用法:ax.plot_wireframe(X, Y, Z, *args, **kwargs)

  • X,Y,Z:输入数据

  • rstride:行步长

  • cstride:列步长

  • rcount:行数上限

  • ccount:列数上限

示例代码:

from mpl_toolkits.mplot3d import axes3dimport matplotlib.pyplot as plt  fig = plt.figure()ax = fig.add_subplot(100, projection='3d') # Grab some test data.X, Y, Z = axes3d.get_test_data(0.12) # Plot a basic wireframe.ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) plt.show()

怎么用Python+Matplotlib绘制三维折线图

4、三角表面图(Tri-Surface plots)

基本用法:ax.plot_trisurf(*args, **kwargs)

ax.plot_trisurf(*args, **kwargs)

X,Y,Z:数据

其他参数类似surface-plot

from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltimport numpy as np  n_radii = 8n_angles = 36 radii = np.linspace(0.125, 1.0, n_radii)angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False) angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)  # points in the (x, y) plane.x = np.append(0, (radii*np.cos(angles)).flatten())y = np.append(0, (radii*np.sin(angles)).flatten())  z = np.sin(-x*y) fig = plt.figure()ax = fig.add_subplot(projection='3d') ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True) plt.show()

运行效果图:

怎么用Python+Matplotlib绘制三维折线图

5.随机散点图 

利用scatter生成随机散点图。

函数定义:

#函数定义
matplotlib.pyplot.scatter(x, y, 
    s=None,   #散点的大小 array  Scalar
    c=None,   #颜色序列   array、sequency
    marker=None,   #点的样式
    cmap=None,    #colormap 颜色样式
    norm=None,    #归一化  归一化的颜色camp
    vmin=None, vmax=None,    #对应上面的归一化范围
     alpha=None,     #透明度
    linewidths=None,   #线宽
    verts=None,   #
    edgecolors=None,  #边缘颜色
    data=None, 
    **kwargs
    )

示例代码:

import numpy as npimport matplotlib.pyplot as plt#定义坐标轴fig4 = plt.figure()ax4 = plt.axes(projection='3d') #生成三维数据xx = np.random.random(20)*10-5   #取100个随机数,范围在5~5之间yy = np.random.random(20)*10-5X, Y = np.meshgrid(xx, yy)Z = np.sin(np.sqrt(X**2+Y**2)) #作图ax4.scatter(X,Y,Z,alpha=0.3,c=np.random.random(400),s=np.random.randint(10,20,size=(20, 20)))     #生成散点.利用c控制颜色序列,s控制大小 plt.show()

效果:

怎么用Python+Matplotlib绘制三维折线图

关于“怎么用Python+Matplotlib绘制三维折线图”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“怎么用Python+Matplotlib绘制三维折线图”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网Python频道。

--结束END--

本文标题: 怎么用Python+Matplotlib绘制三维折线图

本文链接: https://www.lsjlt.com/news/352298.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作