我是靠谱客的博主 健忘牛排,最近开发中收集的这篇文章主要介绍使用matplotlib python数据可视化系列创建3d视频可视化,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

用MATPLOTLIB设计 (DESIGNING WITH MATPLOTLIB)

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

我们的目标是创建这样的视频

image for post
3D video visualization with matplotlib
使用matplotlib进行3D视频可视化

Firstly, we are going to create a simple contour 3D as mentioned in the previous article using the following code

首先,我们将使用以下代码创建一个简单的轮廓3D,如上一篇文章中所述

# 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)

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。 我们无法隐藏面板和网格颜色,但是可以使其类似于背景图片(白色),如注释“#使面板和网格颜色为白色”中所述。 该代码将显示如下结果

3d amazing video visualization with matplotlib
Simple 3D contour plot
简单的3D等高线图

A video is a collection of figures. So, we will create many figures with different azimuth axis point of view. We can adjust this.

视频是人物的集合。 因此,我们将创建许多具有不同方位轴角度的图形。 我们可以调整这个。

# adjust point of view
ax.view_init(60, 90)

We change the value of 90 to a sequence number from 0 to 360 using the following code

我们使用以下代码将90的值更改为从0到360的序列号

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)

You will get 180 figures with different names. The list of figures is like this

您将获得180个不同名称的数字。 数字清单是这样的

3d amazing video visualization with matplotlib
List of figure names in 3d directory
3D目录中的图形名称列表

or you can check it in 3d directory from Jupyter Notebook using

或者您可以使用Jupyter Notebook在3d目录中检查它

! ls 3d/
3d amazing video visualization with matplotlib
List of figure names from Jupyter Notebook
Jupyter Notebook中的图形名称列表

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

为了将所有这些结合起来,我们需要使用此代码制作一个由图形名称组成的数组

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)

The result is

结果是

3d amazing video visualization with matplotlib
Array of figure names
图形名称数组

Then, we combine it all in a single video. The extension of the video is in GIF. You combine it using this code

然后,我们将所有内容合并到一个视频中。 视频的扩展名是GIF。 您可以使用此代码将其合并

# 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)

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视频的结果。 我只是修改角度和颜色图。

image for post

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 python数据可视化系列创建3d视频可视化的全部内容,希望文章能够帮你解决使用matplotlib python数据可视化系列创建3d视频可视化所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部