📜  如何在Python中更改 3D 绘图的角度?

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

如何在Python中更改 3D 绘图的角度?

先决条件: Matplotlib、NumPy

在本文中,我们将看到如何从不同角度查看我们的图表,这里我们使用三种不同的方法来绘制我们的图表。在开始之前,让我们先看看这个目标所需模块的一些基本概念。

  • Matplotlib 是一个基于 NumPy 数组的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈一起使用
  • Numpy是一个通用的数组处理包。它提供了一个高性能的多维数组对象,以及用于处理这些数组的工具。它是使用Python进行科学计算的基础包
  • mpl_toolkits提供了一些基本的 3D 绘图(散点、冲浪、线、网格)工具。

方法:

  • 导入所需的库。
  • 创建一个图形。
  • 创建数据表。
  • 更改 3D 绘图的角度
  • 显示图表。

第 1 步:导入库。

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


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


Python3
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
  
fig = plt.figure(figsize = (8, 8))
ax = plt.axes(projection = '3d')
  
# Data for a three-dimensional line
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')


Python3
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')
  
# Data for a three-dimensional line
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
  
ax.view_init(-140, 60)


Python3
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')
  
# Data for a three-dimensional line
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
  
ax.view_init(-140, 60)
  
plt.show()


Python3
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
  
fig = plt.figure(figsize = (8, 8))
ax = plt.axes(projection = '3d')
  
# Data for a three-dimensional line
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
  
ax.view_init(120, 30)
  
plt.show()


Python3
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from math import sin, cos
  
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(111, projection = '3d')
  
#creating Datasheet
y = np.linspace(-1, 1, 200)
x = np.linspace(-1, 1, 200)
x,y = np.meshgrid(x, y)
  
#set z values
z = x + y 
  
# rotate the samples by changing the value of 'a'
a = 50 
  
t = np.transpose(np.array([x, y, z]), ( 1, 2, 0))
  
m = [[cos(a), 0, sin(a)],[0, 1, 0],
     [-sin(a), 0, cos(a)]]
  
X,Y,Z = np.transpose(np.dot(t, m), (2, 0, 1))
  
#label axes
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
  
#plot figure
ax.plot_surface(X,Y,Z, alpha = 0.5,
                color = 'red')
  
plt.show()


第 2 步:绘图 3-D 轴图。

蟒蛇3

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

步骤 3:为样本的所有 3 轴创建数据表。

蟒蛇3

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
  
fig = plt.figure(figsize = (8, 8))
ax = plt.axes(projection = '3d')
  
# Data for a three-dimensional line
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')

第 4 步:使用 view_init() 可用于以编程方式旋转轴。

蟒蛇3

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')
  
# Data for a three-dimensional line
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
  
ax.view_init(-140, 60)

以下是完整的实现:

示例 1:在本示例中,我们将绘制一个仰角为 -140 度和水平角为 60 度的曲线图。

蟒蛇3

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
  
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')
  
# Data for a three-dimensional line
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
  
ax.view_init(-140, 60)
  
plt.show()

输出:

例 2:在本例中,我们将绘制一个仰角为 120 度,水平角为 30 度的曲线图。

蟒蛇3

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
  
fig = plt.figure(figsize = (8, 8))
ax = plt.axes(projection = '3d')
  
# Data for a three-dimensional line
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
  
ax.view_init(120, 30)
  
plt.show()

输出:

示例 3:在本示例中,我们将绘制一个角度为 50 度的矩形板图。

蟒蛇3

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from math import sin, cos
  
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(111, projection = '3d')
  
#creating Datasheet
y = np.linspace(-1, 1, 200)
x = np.linspace(-1, 1, 200)
x,y = np.meshgrid(x, y)
  
#set z values
z = x + y 
  
# rotate the samples by changing the value of 'a'
a = 50 
  
t = np.transpose(np.array([x, y, z]), ( 1, 2, 0))
  
m = [[cos(a), 0, sin(a)],[0, 1, 0],
     [-sin(a), 0, cos(a)]]
  
X,Y,Z = np.transpose(np.dot(t, m), (2, 0, 1))
  
#label axes
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
  
#plot figure
ax.plot_surface(X,Y,Z, alpha = 0.5,
                color = 'red')
  
plt.show()

输出: