概述
Hi, we will give you an excellent technique to create 3D video visualization with matplotlib. In the previous article, we offer you some code to show a 3D plot in matplotlib. If you haven’t read it, you can check this link out. 嗨,我们将为您提供一种出色的技术,用matplotlib创建3D视频可视化。 在上一篇文章中,我们为您提供了一些代码来显示matplotlib中的3D图。 如果您尚未阅读,可以查看此链接。 Our goal is creating a video like this 我们的目标是创建这样的视频 Firstly, we are going to create a simple contour 3D as mentioned in the previous article using the following code 首先,我们将使用以下代码创建一个简单的轮廓3D,如上一篇文章中所述 The code will give you a figure with no axis because we declare axis3don = False. We can’t hide the panel and grid color, but we can make it similar to the background picture (white) like mentioned in the comment “# make the panel and grid color white”. The code will show the result like this 该代码将为您提供一个没有轴的图形,因为我们声明axis3don = False。 我们无法隐藏面板和网格颜色,但是可以使其类似于背景图片(白色),如注释“#使面板和网格颜色为白色”中所述。 该代码将显示如下结果 A video is a collection of figures. So, we will create many figures with different azimuth axis point of view. We can adjust this. 视频是人物的集合。 因此,我们将创建许多具有不同方位轴角度的图形。 我们可以调整这个。 We change the value of 90 to a sequence number from 0 to 360 using the following code 我们使用以下代码将90的值更改为从0到360的序列号 You will get 180 figures with different names. The list of figures is like this 您将获得180个不同名称的数字。 数字清单是这样的 or you can check it in 3d directory from Jupyter Notebook using 或者您可以使用Jupyter Notebook在3d目录中检查它 The figure names is listed from 3d_vis_0.png to 3d_vis_358_.png. 图形名称从3d_vis_0.png到3d_vis_358_.png列出。 To combine it all, we need to make an array which is consisted of the figure names, using this code 为了将所有这些结合起来,我们需要使用此代码制作一个由图形名称组成的数组 The result is 结果是 Then, we combine it all in a single video. The extension of the video is in GIF. You combine it using this code 然后,我们将所有内容合并到一个视频中。 视频的扩展名是GIF。 您可以使用此代码将其合并 It will save in a GIF file named 3d_vis.gif that has a duration of 40 seconds in a single loop. 它将保存在一个名为3d_vis.gif的GIF文件中,该文件在单个循环中的持续时间为40秒。 Here is the result of the modified 3D video. I just modify the angle and colormaps. 这是修改后的3D视频的结果。 我只是修改角度和颜色图。 If you want to change the colormaps, 3D plot model, you can check this article 如果要更改颜色图,3D绘图模型,可以查看本文 Thanks. 谢谢。 翻译自: https://medium.com/swlh/creating-3d-video-visualization-with-matplotlib-python-data-visualization-series-d8f5dfe1c460 用MATPLOTLIB设计 (DESIGNING WITH MATPLOTLIB)
# create data points
x = np.linspace(-10, 10, 100)
y = np.linspace(-15, 15, 100)# create grid
X, Y = np.meshgrid(x, y)Z = np.sin(X) + np.cos(Y)fig = plt.figure(figsize=(9, 6))
ax = plt.axes(projection = '3d')# hide the axis
ax._axis3don = False# 3d contour plot
ax.contour3D(X, Y, Z, 100, cmap = 'viridis')# make panel color white
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))# make grid color white
ax.xaxis._axinfo['grid']['color'] = (1, 1, 1, 0)
ax.yaxis._axinfo['grid']['color'] = (1, 1, 1, 0)
ax.zaxis._axinfo['grid']['color'] = (1, 1, 1, 0)# adjust point of view
ax.view_init(60, 90)# adjust point of view
ax.view_init(60, 90)for angle in range(0,360,2):
plt.figure(figsize=(16, 9))
ax = plt.axes(projection = '3d')
ax._axis3don = False
# 3d contour plot
ax.contour3D(X, Y, Z, 200, cmap = 'viridis')
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.xaxis._axinfo['grid']['color'] = (1, 1, 1, 0)
ax.yaxis._axinfo['grid']['color'] = (1, 1, 1, 0)
ax.zaxis._axinfo['grid']['color'] = (1, 1, 1, 0)
# adjust view from 0, 2, 4, 6, ..., 360
ax.view_init(60, angle)
# save figure with different names depend on the view
filename='3d/3d_vis_'+str(angle)+'.png'
plt.savefig(filename, dpi=75)! ls 3d/
from PIL import Imagepng_count = 180
files = []
for i in range(png_count):
seq = str(i*2)
file_names = '3d_vis_'+ seq +'.png'
files.append(file_names)
print(files)# Create the frames
frames = []
files
for i in files:
new_frame = Image.open(i)
frames.append(new_frame)
# Save into a GIF file that loops forever
frames[0].save('3d_vis.gif', format='GIF',
append_images=frames[1:],
save_all=True,
duration=40, loop=0)
最后
以上就是健忘牛排为你收集整理的使用matplotlib python数据可视化系列创建3d视频可视化的全部内容,希望文章能够帮你解决使用matplotlib python数据可视化系列创建3d视频可视化所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复