Colorbar for matplotlib plot_surface using facecolors

雨燕双飞 提交于 2019-12-01 11:29:30

Your plot is correct, although you might simplify the normalization using a matplotlib.colors.Normalize instance.

norm = matplotlib.colors.Normalize(vmin=V.min().min(), vmax=V.max().max())
ax.plot_surface(X, Y, Z, facecolors=plt.cm.jet(norm(V)))
m = cm.ScalarMappable(cmap=plt.cm.jet, norm=norm)
m.set_array([])
plt.colorbar(m)

The point why you don't see the maximum value of 10.15 on the grid, is a different one:

When having N points along one dimension, the plot has (N-1) faces. That means that the last row and column of the input color array are simply not plotted.

This can be seen in the following picture, where a 3x3 matrix is plotted, resulting in 2x2 faces. They are colorized according to the respective values in a color array, such that the first face has the color given by the first element in the array etc. For the last elements there is no face to color left.

Code to reproduce this plot:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.colors

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.arange(3)
X,Y = np.meshgrid(x,x)
Z = np.ones_like(X)

V = np.array([[3,2,2],[1,0,3],[2,1,0]])

norm = matplotlib.colors.Normalize(vmin=0, vmax=3)
ax.plot_surface(X, Y, Z, facecolors=plt.cm.jet(norm(V)), shade=False)

m = cm.ScalarMappable(cmap=plt.cm.jet, norm=norm)
m.set_array([])
plt.colorbar(m)

ax.set_xlabel('x')
ax.set_ylabel('y')

plt.show()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!