我正在尝试绘制3d图,但是颜色范围太小,以至于它仅覆盖z轴可以具有的一小部分值。我该如何解决?
我附上代码和得到的图像:
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(B , ENERGY, result_plot, cmap=cm.Spectral_r , linewidth=0.0 ,antialiased =False)
colorbar( surf, shrink=0.5, aspect=3)
ax.view_init(30, 45)
plt.show()
答案 0 :(得分:1)
将来请提供minimal and verifiable example。颜色限制是根据您的数据确定的。因此,我不能完全确定您的数据支持的值比显示的更多。使用docs中的示例,我们可以使用vmin
和vmax
来强制限制。
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
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, vmin = -10, vmax = 10)
# 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()