【704】Python绘制三维曲线
参考:用三维的视角理解二维世界
参考:Matplotlib - 3D Surface plot
参考:PLOT_SURFACE(AXES3D)方法:绘制3D图形
参考:python 3d图
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig)

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'))

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)

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()
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)

#用三维的视角理解二维世界 #完美解释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()
