pythonmatplotlibmatplotlib-3dtwinx

How to share 2D axis on a 3D plot


I am able to plot a 3D surface plot with a 2D contour plot just fine enter image description here

but I would also like to share the x axis of the 2D plot using ax.twinx() like I've done in a separate plot here:

enter image description here

However, when I add this to the ax including the 3D contour plot (e.g."ax2 = ax.twinx()"), I get an error: AttributeError: 'YAxis' object has no attribute 'tick_left'. Do you have any ideas for a workaround for this? Thank you. Here are the relevant parts of my code.

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

Solution

  • I've figured it out, here are the relevant bits of code:

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