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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eqwaak0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值