问题描述
我有以下功能:
def plotTemperatureGradient(temp_mat, file_name):
xs, ys, zs = temp_mat.shape
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = np.mgrid[:xs, :ys, :zs]
faltten_data = temp_mat.ravel().astype(np.float16)
color_map = np.zeros((faltten_data.shape[0], 4))
# map scalars to colors
minima = np.min(faltten_data[np.nonzero(faltten_data)])
maxima = np.max(faltten_data[np.nonzero(faltten_data)])
norm = matplotlib.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap='jet')
rgba = mapper.to_rgba(faltten_data)
color_map[:,0:3] = rgba[:, 0:3]
color_map[:,3] = np.where(faltten_data > 0, 0.07, 0)
p = ax.scatter(X, Y, Z, c=color_map.astype(np.float16))
ax.set_xlabel('X position [Arb.]')
ax.set_ylabel('Y position [Arb.]')
ax.set_zlabel('Z position [Arb.]')
c_bar = plt.colorbar(mapper, cmap='jet')
c_bar.set_label('Temp [K]')
# plt.title(title)
plt.savefig(file_name)
plt.close(fig)
print("Exported Temperature Gradient")
return
它获取一个 3D numpy 数组并使用 scatter
绘制它并在 Windows 上生成以下内容:
which gets a 3D numpy array and plots it using scatter
and produce the following on Windows:
当我在 Linux 服务器上运行相同的代码时,作为脚本的一部分,它失败并给我以下错误:
When I run the same code on a Linux server, as part of a script, it fails and give me the following errors:
plotTemperatureGradient(temp_domain, 'test.png') 文件CloudDomainCreate.py",第 151 行,在 plotTemperatureGradient 中c_bar = plt.colorbar(mapper, cmap='jet') 文件/apps/RH7U2/gnu/python/3.7.0/lib/python3.7/site-packages/matplotlib/pyplot.py",第2100行,在颜色栏中ret = gcf().colorbar(可映射,cax = cax,ax = ax,** kw)文件"/apps/RH7U2/gnu/python/3.7.0/lib/python3.7/site-packages/matplotlib/figure.py",第2129行,在颜色栏中cb = cbar.colorbar_factory(cax,mappable,** cb_kw)文件"/apps/RH7U2/gnu/python/3.7.0/lib/python3.7/site-packages/matplotlib/colorbar.py",第 1567 行,在 colorbar_factory 中cb = Colorbar(cax,mappable,** kwargs)文件"/apps/RH7U2/gnu/python/3.7.0/lib/python3.7/site-packages/matplotlib/colorbar.py",第1073行,在 init 中mappable.autoscale_None()文件"/apps/RH7U2/gnu/python/3.7.0/lib/python3.7/site-packages/matplotlib/cm.py",第374行,在autoscale_None中raise TypeError('You must first set_array for mappable') TypeError: You must first set_array for mappable
我找不到解决该问题的方法,我还尝试按照以下问题进行操作:matplotlib plot_surface命令的颜色条,但没有成功.
I could not find a way to fix it, I also tried following this question: Colorbar for matplotlib plot_surface command but with no success.
不胜感激
推荐答案
它起作用的版本比不起作用的版本新.在旧版本中,您需要手动设置数组,
The version where it works is newer than the one where it doesn't. In older versions you need to set the array manually,
mapper.set_array([])
这篇关于向 3D 散点图添加颜色条在 Linux 服务器上失败,而在 Windows 上成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!