📜  如何使用 Matplotlib 为 3D 图形制作动画?

📅  最后修改于: 2022-05-13 01:55:09.589000             🧑  作者: Mango

如何使用 Matplotlib 为 3D 图形制作动画?

先决条件: Matplotlib、NumPy

图形表示总是易于理解,并且在任何书面或口头交流之前被采用和优选。使用 Matplotlib,我们可以绘制不同类型的图形数据。在本文中,我们将尝试了解,如何使用 matplotlib 创建漂亮的图形并使用 Matplotlib 创建 3D 动画图形。

方法:

  • 导入所需的模块。
  • 创建一个 3d 图形
  • 创建示例数据
  • 动画 360 度视图。
  • 显示图表。

第一步:导入库。

Python3
from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d


Python3
from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
  
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')


Python3
from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection='3d')
  
t = np.linspace(0, 1, 1000, endpoint=True)
ax.plot3D(t, signal.square(2 * np.pi * 5 * t))


Python3
from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection='3d')
  
t = np.linspace(0, 1, 1000, endpoint=True)
ax.plot3D(t, signal.square(2 * np.pi * 5 * t))
  
for angle in range(0, 360):
   ax.view_init(angle,30)
   plt.draw()
   plt.pause(.001)


Python3
from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
# Creating 3D figure
fig = plt.figure(figsize = (8, 8))
ax = plt.axes(projection = '3d')
  
# Creating Dataset
t = np.linspace(0, 1, 1000, endpoint = True)
ax.plot3D(t, signal.square(2 * np.pi * 5 * t))
  
# 360 Degree view
for angle in range(0, 360):
   ax.view_init(angle, 30)
   plt.draw()
   plt.pause(.001)
      
plt.show()


Python3
from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
# Creating 3D figure
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection='3d')
  
# Creating Dataset
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
  
# 360 Degree view
for angle in range(0, 360):
    ax.view_init(angle, 30)
    plt.draw()
    plt.pause(.001)
  
plt.show()


Python3
from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
# Creating 3D figure
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')
  
# Creating Dataset
color_cycle = plt.rcParams['axes.prop_cycle']()
x = linspace(0, 1, 51)
a = x*( 1 - x)    
b = 0.25 - a    
c = x*x*(1 - x)
d = 0.25-c    
  
ax.plot3D(x, a, **next(color_cycle))
  
# 360 Degree view
for angle in range(0, 360):
   ax.view_init(angle, 30)
   plt.draw()
   plt.pause(.001)
  
plt.show()


第 2 步:使用 plt.figure() 的目的是创建一个图形对象。我们将使用 plt.axes () 创建单独的轴集,您将在其中绘制每个轴。

蟒蛇3

from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
  
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')

第 3 步:在这一步中,我们将创建数据并绘制不同的图形。

蟒蛇3

from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection='3d')
  
t = np.linspace(0, 1, 1000, endpoint=True)
ax.plot3D(t, signal.square(2 * np.pi * 5 * t))

第 4 步:图形的 360 度移动。

蟒蛇3

from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection='3d')
  
t = np.linspace(0, 1, 1000, endpoint=True)
ax.plot3D(t, signal.square(2 * np.pi * 5 * t))
  
for angle in range(0, 360):
   ax.view_init(angle,30)
   plt.draw()
   plt.pause(.001)

示例 1:在本示例中,我们绘制一个方波,我们将看到它的 360 度视图。

蟒蛇3

from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
# Creating 3D figure
fig = plt.figure(figsize = (8, 8))
ax = plt.axes(projection = '3d')
  
# Creating Dataset
t = np.linspace(0, 1, 1000, endpoint = True)
ax.plot3D(t, signal.square(2 * np.pi * 5 * t))
  
# 360 Degree view
for angle in range(0, 360):
   ax.view_init(angle, 30)
   plt.draw()
   plt.pause(.001)
      
plt.show()

输出:

示例 2:在本示例中,我们绘制了一个螺旋图,我们将看到它的 360 度视图

蟒蛇3

from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
# Creating 3D figure
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection='3d')
  
# Creating Dataset
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
  
# 360 Degree view
for angle in range(0, 360):
    ax.view_init(angle, 30)
    plt.draw()
    plt.pause(.001)
  
plt.show()

输出:

例 3:在本例中,我们将显示抛物线图。

蟒蛇3

from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
  
# Creating 3D figure
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')
  
# Creating Dataset
color_cycle = plt.rcParams['axes.prop_cycle']()
x = linspace(0, 1, 51)
a = x*( 1 - x)    
b = 0.25 - a    
c = x*x*(1 - x)
d = 0.25-c    
  
ax.plot3D(x, a, **next(color_cycle))
  
# 360 Degree view
for angle in range(0, 360):
   ax.view_init(angle, 30)
   plt.draw()
   plt.pause(.001)
  
plt.show()

输出: