温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2023-03-21 11:13:36 来源:亿速云 阅读:123 作者:iii 栏目:开发技术

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

1.0简介

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

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

2.0三维图画法与类型

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

pip install matplotlib

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

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

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

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

1、直线绘制(Line plots)

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

代码如下:

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import 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 + 1
x = 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 Axes3D
import matplotlib.pyplot as plt
import 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 axes3d
import 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 Axes3D
import matplotlib.pyplot as plt
import numpy as np
 
 
n_radii = 8
n_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 np
import 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-5
X, 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绘制三维折线图”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI