alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【704】Python绘制三维曲线

参考:用三维的视角理解二维世界

参考:3D绘图&ax.plot_surface()

参考:Matplotlib - 3D Surface plot

参考:PLOT_SURFACE(AXES3D)方法:绘制3D图形

参考:Python进阶学习---matplotlib的使用

参考:python 3d图

1
2
3
4
5
6
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
 
fig = plt.figure()
ax = Axes3D(fig)

1
2
3
4
5
6
7
8
9
10
11
12
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
# height value
Z = np.sin(R)
 
fig = plt.figure(figsize=(9, 6))
ax = fig.gca(projection='3d')
 
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow'))

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
strike = np.arange(-4, 4, 0.25)
ttm = np.arange(-4, 4, 0.25)
strike, ttm = np.meshgrid(strike, ttm)
iv = np.sqrt(strike ** 2 + ttm ** 2)
# generate fake implied volatilities
 
fig = plt.figure(figsize=(9, 6))
ax = fig.gca(projection='3d')
 
'''同上面两行代码
fig = plt.figure(figsize=(12, 8))
ax = Axes3D(fig)
'''
 
surf = ax.plot_surface(strike, ttm, iv, rstride=2,
                       cstride=2, cmap=plt.get_cmap('rainbow'),
                       linewidth=0.5, antialiased=True)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(12, 8))
ax = Axes3D(fig)
delta = 0.125
# 生成代表X轴数据的列表
x = np.arange(-3.0, 3.0, delta)
# 生成代表Y轴数据的列表
y = np.arange(-2.0, 2.0, delta)
# 对x、y数据执行网格化
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
# 计算Z轴数据(高度数据)
Z = (Z1 - Z2) * 2
# 绘制3D图形
ax.plot_surface(X, Y, Z,
    rstride=1# rstride(row)指定行的跨度
    cstride=1# cstride(column)指定列的跨度
    cmap=plt.get_cmap('rainbow'))  # 设置颜色映射
# 设置Z轴范围
ax.set_zlim(-2, 2)
# 设置标题
plt.title("3D图")
plt.show()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']='Lisu'
plt.rcParams['axes.unicode_minus']=False
 
strike = np.linspace(50, 150, 24)
ttm = np.linspace(0.5, 2.5, 24)
strike, ttm = np.meshgrid(strike, ttm)
iv = (strike - 100) ** 2 / (100 * strike) / ttm
# generate fake implied volatilities
 
fig = plt.figure(figsize=(9, 6))
ax = fig.gca(projection='3d')
 
'''同上面两行代码
fig = plt.figure(figsize=(12, 8))
ax = Axes3D(fig)
'''
 
surf = ax.plot_surface(strike, ttm, iv, rstride=2,
                       cstride=2, cmap=plt.cm.coolwarm,
                       linewidth=0.5, antialiased=True)
ax.set_xlabel('strike')
ax.set_ylabel('time-to-maturity')
ax.set_zlabel('implied volatility')
fig.colorbar(surf, shrink=0.5, aspect=5)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#用三维的视角理解二维世界
#完美解释meshgrid函数,三维曲面,等高线
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#plt.rcParams['font.sans-serif']=['FangSong']# 用来正常显示中文标签
#plt.rcParams['axes.unicode_minus']=False# 用来正常显示负号
  
#meshgrid就是生成x1,y1所能代表的所有点的坐标矩阵
n=32
x1=np.linspace(-3,3,n)
y1=np.linspace(-3,3,n)
x, y = np.meshgrid(x1, y1)
 
#z = x - x
#z[0][0]=-1#变异值
#z[8][8]=1#变异值
#z = y - y 与z = x - x相同
z= np.power(x,2) + np.power(y,2)
       
fig=plt.figure()
ax = fig.add_subplot(111, projection='3d')
#ax.set_title('')
  
#三维曲面
ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))
  
#等高线,其实就是投影,zdir代表了视角,offset表示离视角轴0点的距离
#z方向的等高线
ax.contourf(x,y,z,zdir='z',offset=-5,cmap=plt.get_cmap('rainbow'))
#x方向的等高线
#ax.contourf(x,y,z,zdir='x',offset=-5,cmap=plt.get_cmap('rainbow'))
#ax.contour(x,y,z,10,zdir='x',colors='black',linewidth=0.5)
  
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
   
plt.show()

posted on   McDelfino  阅读(1314)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示