📜  在Python更改 3D 曲面图中的网格线粗细 – Matplotlib

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

在Python更改 3D 曲面图中的网格线粗细 – Matplotlib

先决条件: Matplotlib

使用 Matplotlib 库,我们可以通过导入 mplot3d 工具包来绘制三维图。在此图中,我们将更改三维曲面图中网格线的厚度。 Surface Plot 是 3D 数据图,它显示了因变量 (Y) 和两个自变量(X 和 Y)之间的函数关系,而不是显示单个数据点。

网格线是穿过绘图以显示轴分区的线。绘图的网格线可帮助图表查看者查看绘图的特定未标记数据点代表什么值。尤其是当绘图过于复杂而无法分析时,它会有所帮助,因此我们可以根据需要调整或更改网格线的粗细或网格线的样式。

方法

  • 导入必要的库。
  • 创建用于绘图的 3D 数据。
  • 使用ax = matplotlib.pyplot.gca('projection=3d')创建三维坐标系,这里 gca 代表获取当前网格,通过参数投影 = 3d 它将生成三维坐标系.
  • 现在要更改网格线厚度,我们必须使用ax.xaxis.update({'linewidth':3})或您要为 x 轴设置的任何网格宽度来更新各个轴的轴信息您可以分别为 Y 轴和 Z 轴设置 yaxis、zaxis 代替 xaxis。
  • 如果想更改相应轴的颜色,只需在字典ax.xaxis.update({'linewidth':3,'color':'red'}) 中传递 'color':'red' 或您想要的任何颜色放。
  • 现在在更改后使用ax.plot_surface(x,y,z)在该传递 3d 数据中绘制曲面图。
  • 使用参数中的 ax.set_xlabel(), ax.set_ylabel(), ax.set_zlabel() 设置 X、Y 和 Z 标签并传递字符串。
  • 现在使用 matplotlib.pyplot.show()函数可视化绘图。

示例 1:使用 Matplotlib 更改 3D 曲面图中的 X 轴网格线粗细。

Python
# importing necessary libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# function to create data for plotting
def data_creation():
   
    # creating 3d data
    x = np.outer(np.linspace(-3, 3, 30), np.ones(30))
    y = x.copy().T # transpose
    z = np.cos(x ** 2 + y ** 2)
    return (x,y,z)
 
# main function
if __name__ == '__main__':
   
    # creating three dimensional co-ordinate system
    ax = plt.gca(projection='3d')
 
    # calling data creation function and storing in
    # the variables
    data_x,data_y,data_z = data_creation()
 
    # changing grid lines thickness of x axis to 3
    ax.xaxis._axinfo["grid"].update({"linewidth":3})
 
    # plotting surface plot
    ax.plot_surface(data_x,data_y,data_z)
 
    # giving label name to x,y and z axis
    ax.set_xlabel("X axis")
    ax.set_ylabel("Y axis")
    ax.set_zlabel("Z axis")
     
    # visualizing the plot
    plt.show()


Python
# importing necessary libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# function to create data for plotting
def data_creation():
   
    # creating 3d data
    x = np.outer(np.linspace(-3, 3, 30), np.ones(30))
    y = x.copy().T # transpose
    z = np.cos(x ** 2 + y ** 2)
    return (x,y,z)
 
# main function
if __name__ == '__main__':
    # creating three dimensional co-ordinate system
    ax = plt.gca(projection='3d')
 
    # calling data creation function and storing in the variables
    data_x,data_y,data_z = data_creation()
 
    # changing grid lines thickness of Y axis to 3
    ax.yaxis._axinfo["grid"].update({"linewidth":3})
 
    # plotting surface plot
    ax.plot_surface(data_x,data_y,data_z)
 
    # giving label name to x,y and z axis
    ax.set_xlabel("X axis")
    ax.set_ylabel("Y axis")
    ax.set_zlabel("Z axis")
     
    # visualizing the plot
    plt.show()


Python
# importing necessary libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# function to create data for plotting
def data_creation():
    # creating 3d data
    x = np.outer(np.linspace(-3, 3, 30), np.ones(30))
    y = x.copy().T # transpose
    z = np.cos(x ** 2 + y ** 2)
    return (x,y,z)
 
# main function
if __name__ == '__main__':
    # creating three dimensional co-ordinate system
    ax = plt.gca(projection='3d')
 
    # calling data creation function and storing in the variables
    data_x,data_y,data_z = data_creation()
 
    # changing grid lines thickness of Z axis to 3
    ax.zaxis._axinfo["grid"].update({"linewidth":3})
 
    # plotting surface plot
    ax.plot_surface(data_x,data_y,data_z)
 
    # giving label name to x,y and z axis
    ax.set_xlabel("X axis")
    ax.set_ylabel("Y axis")
    ax.set_zlabel("Z axis")
     
    # visualizing the plot
    plt.show()


Python
# importing necessary libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# function to create data for plotting
def data_creation():
   
    # creating 3d data
    x = np.outer(np.linspace(-3, 3, 30), np.ones(30))
    y = x.copy().T # transpose
    z = np.cos(x ** 2 + y ** 2)
    return (x,y,z)
 
# main function
if __name__ == '__main__':
    # creating three dimensional co-ordinate system
    ax = plt.gca(projection='3d')
 
    # calling data creation function and storing in the variables
    data_x,data_y,data_z = data_creation()
 
    # changing grid lines thickness of x axis to 1
    ax.xaxis._axinfo["grid"].update({"linewidth":1})
 
    # changing grid lines thickness of Y axis to 1 and giving color to red
    ax.yaxis._axinfo["grid"].update({"linewidth":1,'color':'red'})
 
    # changing grid lines thickness of Z axis to 1 and giving color to green
    ax.zaxis._axinfo["grid"].update({"linewidth":1,'color':'green'})
 
    # plotting surface plot
    ax.plot_surface(data_x,data_y,data_z)
 
    # giving label name to x,y and z axis
    ax.set_xlabel("X axis")
    ax.set_ylabel("Y axis")
    ax.set_zlabel("Z axis")
     
    # visualizing the plot
    plt.show()


输出:

在上面的例子中,我们改变了 X 轴的网格线粗细,如上图所示,X 轴网格线有一条较粗的灰色线。可以通过更新上面示例代码中各个轴的_axinfo字典来完成,我们各自的轴是X轴。



示例 2:使用 Matplotlib 更改 3D 曲面图中的 X 轴网格线粗细。

Python

# importing necessary libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# function to create data for plotting
def data_creation():
   
    # creating 3d data
    x = np.outer(np.linspace(-3, 3, 30), np.ones(30))
    y = x.copy().T # transpose
    z = np.cos(x ** 2 + y ** 2)
    return (x,y,z)
 
# main function
if __name__ == '__main__':
    # creating three dimensional co-ordinate system
    ax = plt.gca(projection='3d')
 
    # calling data creation function and storing in the variables
    data_x,data_y,data_z = data_creation()
 
    # changing grid lines thickness of Y axis to 3
    ax.yaxis._axinfo["grid"].update({"linewidth":3})
 
    # plotting surface plot
    ax.plot_surface(data_x,data_y,data_z)
 
    # giving label name to x,y and z axis
    ax.set_xlabel("X axis")
    ax.set_ylabel("Y axis")
    ax.set_zlabel("Z axis")
     
    # visualizing the plot
    plt.show()

输出:

在上面的例子中,我们改变了 Y 轴的网格线粗细,如上图所示,Y 轴网格线有一条较粗的灰色线。我们已经将 Y 轴的网格线粗细设置为 3。



示例 3:使用 Matplotlib 更改 3D 曲面图中的 Z 轴网格线粗细。

Python

# importing necessary libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# function to create data for plotting
def data_creation():
    # creating 3d data
    x = np.outer(np.linspace(-3, 3, 30), np.ones(30))
    y = x.copy().T # transpose
    z = np.cos(x ** 2 + y ** 2)
    return (x,y,z)
 
# main function
if __name__ == '__main__':
    # creating three dimensional co-ordinate system
    ax = plt.gca(projection='3d')
 
    # calling data creation function and storing in the variables
    data_x,data_y,data_z = data_creation()
 
    # changing grid lines thickness of Z axis to 3
    ax.zaxis._axinfo["grid"].update({"linewidth":3})
 
    # plotting surface plot
    ax.plot_surface(data_x,data_y,data_z)
 
    # giving label name to x,y and z axis
    ax.set_xlabel("X axis")
    ax.set_ylabel("Y axis")
    ax.set_zlabel("Z axis")
     
    # visualizing the plot
    plt.show()

输出:



在上面的例子中,我们改变了 Z 轴的网格线粗细,如上图所示,Z 轴网格线有一条较粗的灰色线。我们已将 Z 轴的网格线粗细设置为 3。

示例 4:使用 Matplotlib 更改所有三个轴的网格线粗细和颜色。

Python

# importing necessary libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# function to create data for plotting
def data_creation():
   
    # creating 3d data
    x = np.outer(np.linspace(-3, 3, 30), np.ones(30))
    y = x.copy().T # transpose
    z = np.cos(x ** 2 + y ** 2)
    return (x,y,z)
 
# main function
if __name__ == '__main__':
    # creating three dimensional co-ordinate system
    ax = plt.gca(projection='3d')
 
    # calling data creation function and storing in the variables
    data_x,data_y,data_z = data_creation()
 
    # changing grid lines thickness of x axis to 1
    ax.xaxis._axinfo["grid"].update({"linewidth":1})
 
    # changing grid lines thickness of Y axis to 1 and giving color to red
    ax.yaxis._axinfo["grid"].update({"linewidth":1,'color':'red'})
 
    # changing grid lines thickness of Z axis to 1 and giving color to green
    ax.zaxis._axinfo["grid"].update({"linewidth":1,'color':'green'})
 
    # plotting surface plot
    ax.plot_surface(data_x,data_y,data_z)
 
    # giving label name to x,y and z axis
    ax.set_xlabel("X axis")
    ax.set_ylabel("Y axis")
    ax.set_zlabel("Z axis")
     
    # visualizing the plot
    plt.show()

输出:

在上面的例子中,我们通过更新 _axinfo 和更新我们设置的字典,将 X、Y 和 Z 轴的网格线粗细设置为 1,并将 Y 轴的颜色更改为红色,将 Z 轴的颜色更改为绿色3D 绘图的线宽和颜色。