Matplotlib(小案例)

1、3D表面形状的绘制 

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

fig=plt.figure()
ax=fig.add_subplot(111,projection='3d')

u=np.linspace(0,2*np.pi,100)
v=np.linspace(0,np.pi,100)
x=10*np.outer(np.cos(u),np.sin(v))
y=10*np.outer(np.sin(u),np.cos(v))
z=10*np.outer(np.ones(np.size(u)),np.cos(v))

ax.plot_surface(x,y,z,color='r')

plt.show()

 

2.绘制3D线和3D点的示例

from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import random

fig = plt.figure()
ax = plt.axes(projection="3d")
z_line = np.linspace(0, 15, 1000)
x_line = np.cos(z_line)
y_line = np.sin(z_line)
ax.plot3D(x_line, y_line, z_line, ‘gray’)

z_points = 15 * np.random.random(100)
x_points = np.cos(z_points) + 0.1 * np.random.randn(100)
y_points = np.sin(z_points) + 0.1 * np.random.randn(100)
ax.scatter3D(x_points, y_points, z_points, c=z_points, cmap=’hsv’);

plt.show()

3.曲面图[Surface图] 

from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import random

fig=plt.figure()
ax=plt.axes(projection="3d")
def z_function(x,y):
    return np.sin(np.sqrt(x**2+y**2))
x=np.linspace(-6,6,30)
y=np.linspace(-6,6,30)

X,Y=np.meshgrid(x,y)
Z=z_function(X,Y)
ax.plot_wireframe(X,Y,Z,color='green')
ax.set_xlabel('x')
ax.set_xlabel('y')
ax.set_xlabel('z')

plt.show()

 

(2)添加方法: 

from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import random

fig=plt.figure()
ax=plt.axes(projection="3d")
def z_function(x,y):
    return np.sin(np.sqrt(x**2+y**2))
x=np.linspace(-6,6,30)
y=np.linspace(-6,6,30)

X,Y=np.meshgrid(x,y)
Z=z_function(X,Y)
ax.plot_wireframe(X,Y,Z,color='green')
ax.set_xlabel('x')
ax.set_xlabel('y')
ax.set_xlabel('z')

ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
                cmap='winter', edgecolor='none')
ax.set_title('surface');

plt.show()

 

4.3D条形图[bar图] 

from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import random

fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
x3 = [1,2,3,4,5,6,7,8,9,10]
y3 = [5,6,7,8,2,5,6,3,7,2]
z3 = np.zeros(10)
dx = np.ones(10)
dy = np.ones(10)
dz = [1,2,3,4,5,6,7,8,9,10]
ax1.bar3d(x3, y3, z3, dx, dy, dz)
ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
ax1.set_zlabel('z axis')
plt.show()

 

相关推荐

  1. Matplotlib

    2024-06-18 21:52:01       35 阅读
  2. Matplotlib plt.plot数据可视化应用案例

    2024-06-18 21:52:01       54 阅读
  3. 爬虫(案例

    2024-06-18 21:52:01       34 阅读
  4. 前端案例

    2024-06-18 21:52:01       30 阅读
  5. Flutter实战案例

    2024-06-18 21:52:01       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-18 21:52:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-18 21:52:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-18 21:52:01       87 阅读
  4. Python语言-面向对象

    2024-06-18 21:52:01       96 阅读

热门阅读

  1. 给wordpress网站添加瀑布流效果

    2024-06-18 21:52:01       37 阅读
  2. 文件系统更新initrd的方法

    2024-06-18 21:52:01       27 阅读
  3. 广东省省站节能检测试题库(2024年)

    2024-06-18 21:52:01       29 阅读
  4. git\repo

    git\repo

    2024-06-18 21:52:01      26 阅读
  5. Kotlin 中,data class 和普通 class

    2024-06-18 21:52:01       32 阅读
  6. 嵌入式跨平台编译:vsftpd

    2024-06-18 21:52:01       28 阅读