如何在 matplotlib 中的 3D 图上共享 2D 轴

How to share 2D axis on a 3D plot in matplotlib

我可以用 2D 等高线图绘制 3D 曲面图

但我也想使用 ax.twinx() 分享二维绘图的 x 轴,就像我在此处的单独绘图中所做的那样:

但是,当我将其添加到包含 3D 等高线图的 ax 时(例如,“ax2 = ax.twinx()”),我收到错误消息:AttributeError: 'YAxis' object has no attribute 'tick_left'。您对此有任何解决方法吗?谢谢你。这是我的代码的相关部分。

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(projection='3d')
X,Y = np.meshgrid(time_Raw1[t1:t],list(reversed(range(100))))
ax.plot_surface(X,Y, csd_matrix,cmap =cm.seismic, alpha = 0.5)
ax.contourf(X, Y, csd_matrix,  offset=np.min(csd_matrix), levels=levels, cmap=cmap)
ax2 = ax.twinx()
ax2.plot(time_Raw1[t1:t],channel_data, color='k', clip_on=False)         
plt.show() 

我已经弄明白了,下面是相关的代码:

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(projection='3d')
X,Y = np.meshgrid(time_Raw1[t1:t],list(reversed(range(100))))
ax.plot_surface(X,Y, csd_matrix,cmap =cm.seismic, alpha = 0.5)
ax.contourf(X, Y, csd_matrix,  offset=np.min(csd_matrix), levels=levels, cmap=cmap)
ax2 = fig.add_subplot(projection='3d',sharex=ax)
Z = np.full((102),np.min(csd_matrix))
ax2.plot3D(time_Raw1[t1:t],channel_data,Z, color='k', clip_on=False)         
plt.show()