python边写边总结(十一)Matplotlib了解
发布日期:2021-06-29 06:03:29 浏览次数:2 分类:技术文章

本文共 2161 字,大约阅读时间需要 7 分钟。

Matplotlib主要作用是做可视化,为了快速上手,最好的方式是查看demo,matplotlib就提供了很多demo,网址如下

我今天要关注的是3D显示,看看它提供的第一个3D example

  • Animated 3D random walk

import numpy as npimport matplotlib.pyplot as pltimport mpl_toolkits.mplot3d.axes3d as p3import matplotlib.animation as animation# Fixing random state for reproducibilitynp.random.seed(19680801) #随机种子# 生成一条线的点,如果是三维的就生成三维点(x,y,z)def Gen_RandLine(length, dims=2):    """    Create a line using a random walk algorithm     length is the number of points for the line.# length表示线上的点数    dims is the number of dimensions the line has.# dim表示线上number的维数,默认是二维    """    lineData = np.empty((dims, length))    lineData[:, 0] = np.random.rand(dims) #第一列为随机数    for index in range(1, length): # 剩下length-1列        # scaling the random numbers by 0.1 so        # movement is small compared to position.        # subtraction by 0.5 is to change the range to [-0.5, 0.5]        # to allow a line to move backwards.        step = ((np.random.rand(dims) - 0.5) * 0.1) #随机生成一列(-0.05,+0.05)        lineData[:, index] = lineData[:, index - 1] + step # 上一步加step就是下一步的数    return lineDatadef update_lines(num, dataLines, lines):    for line, data in zip(lines, dataLines):        # NOTE: there is no .set_data() for 3 dim data...        line.set_data(data[0:2, :num])        line.set_3d_properties(data[2, :num])    return lines# Attaching 3D axis to the figurefig = plt.figure() # 这相当于一个画布ax = p3.Axes3D(fig) # 这相当于将这个画布交给3D显示使用# Fifty lines of random 3-D lines # 形成50条随机3D线data = [Gen_RandLine(25, 3) for index in range(50)] #注意这种写法,在这里生成了数据# Creating fifty line objects.# NOTE: Can't pass empty arrays into 3d version of plot()#每条线的起点lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data]# 注意这里ax.plot的写法 # Setting the axes propertiesax.set_xlim3d([0.0, 1.0])ax.set_xlabel('X')ax.set_ylim3d([0.0, 1.0])ax.set_ylabel('Y')ax.set_zlim3d([0.0, 1.0])ax.set_zlabel('Z')ax.set_title('3D Test')# Creating the Animation object# 使用matplotlib的动画功能line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs=(data, lines),                                   interval=50, blit=False)# 更新方式是采用update_lines这个函数,后面的参数是给update_lines提供参数plt.show()

 

转载地址:https://blog.csdn.net/zhouzhouasishuijiao/article/details/85108468 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python边写边总结(十二)scipy文档
下一篇:python边写边总结(十)Numba文档阅读记录

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月13日 15时04分24秒