I would like to plot a colorbar next to an image plotted with plot_surface
in a 3d plot, such that the colorbar also rotates with the plot_surface image when the 3d axes are rotated.
So far, my code looks something like this:
import matplotlib.pyplot as plt
import numpy as np
rng = np.random.default_rng()
fig = plt.figure(figsize=(40, 16))
ax = fig.add_subplot(121, projection="3d", computed_zorder=False)
ax.view_init(roll=ax.roll-5, elev=ax.elev - 20, azim=-75)
xx, yy = np.meshgrid(np.arange(1, 8), np.arange(1, 8))
im = ax.plot_surface(
xx,
np.zeros_like(xx),
yy,
# facecolors=cmap(img), normally image here
zorder=1,
vmin=-1,
vmax=1,
cmap="inferno",
alpha=1,
zsort="max",
)
cbaxes = ax.inset_axes([0.8, 0.3, 0.015, 0.41], transform=ax.transAxes)
cbar = fig.colorbar(im, cax=cbaxes, orientation="vertical", pad=0.05)
cbar.set_ticks(ticks=[0, 0.5, 1], labels=[-1, 0, 1])
This resulted in the following plot where the colorbar remains vertical regardless of the 3d axes orientation: