0

I have a matplotlib 3d plot, like the one from the examples in the documentation

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                   linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

enter image description here

One thing I dislike is that both the colorbar and the z-axis ticks and ticklabels show the same information, i.e. in this case both show a range of numbers from -1 to 1. Is there an easy way to integrate them? I.e. I would like to replace the z-axis with the colorbar, a bit like this, i.e. the z-axis and the colorbar are combined:

enter image description here

Is there any easier way to do this other than somehow removing the z-axis, then manually rotating the colorbar and trying to align the whole thing?

charelf
  • 3,103
  • 4
  • 29
  • 51
  • My question is: since the colorbar shows z-values, do you really need the colorbar? Do you really need the surface to be colored with z-values? Or is it to make the 3D surface more "understandable" since Matplotlib 3D offers a meh-like experience. If that's the case, maybe you can try a different 3D plotting library... – Davide_sd Apr 19 '22 at 13:11
  • That is a good comment. My motivation to use a colorbar is because I am used to include a colorbar every time I used a colormap. However your argumentation also makes sense. In any case, I was just wondering if there is a simple way to do so – charelf Apr 19 '22 at 14:00

0 Answers0