首页 > 解决方案 > 使用 Trisurf 的 3D 绘图:添加颜色图

问题描述

我想通过使用数据框中三个不同列的数据来绘制 3D 表面。我能够绘制散点图,并在其上添加了彩色图。它完美地工作!

在此处输入图像描述

现在我正在使用带有相同数据集的 trisurf。我有一个情节,但我无法在情节上应用颜色渐变。这是我到目前为止所做的:

ttm = df_iv['nb of days to expiration'].tolist()
strikes = df_iv['Strike'].tolist()
imp_vol = df_iv['IV with Newton Method'].tolist()
xx = np.linspace(np.min(strikes), np.max(strikes))
yy = np.linspace(np.min(ttm), np.max(ttm))
xx, yy = np.meshgrid(xx, yy)
zz = interpolate.griddata((strikes, ttm), imp_vol, (xx.ravel(), yy.ravel()))

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(xx.ravel(), yy.ravel(), zz,c=zz,cmap='viridis')

cb = plt.colorbar(line)
ax.set_ylabel('Time to Maturity')
ax.set_xlabel('Strike Price')
ax.set_zlabel('Implied Volatility')
ax.grid(False)
ax.set_title ('Plot of the implied volatility surface')
plt.show()

包含绘图所需数据的df_iv位置在哪里。dataframe不幸的是,程序返回:

Poly3DCollection 对象没有属性 c

.

你有解决方案吗?

先感谢您!

标签: pythonplot3dfinancevolatility

解决方案


推荐阅读