更改颜色范围3d绘图Matplotlib

问题描述 投票:0回答:1

我试图制作一个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()

3d plot

python matplotlib plot 3d surface
1个回答
1
投票

将来请给minimal and verifiable example。颜色限制根据您的数据确定。因此,我不完全确定您的数据支持的值多于它显示的值。使用docs中的示例,我们可以使用vminvmax强制限制。

enter image description here

# 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()
© www.soinside.com 2019 - 2024. All rights reserved.