如何将颜色条添加到3D matplotlib图像

时间:2019-01-08 02:38:34

标签: matplotlib graph 3d

我试图从工作示例中复制代码而未成功

这是工作代码

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)

这是我失败的尝试

temperature = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","orange", "yellow", "green", "blue", "indigo", "purple"])

colorfunction = solar_year( sun, x, y, z )
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)
cax = ax.plot_surface(x, y, z,  rstride=1, cstride=1, cmap=temperature, 
        facecolors=temperature(colorfunction))

cbar = fig.colorbar(cax)
plt.show()

这给了我

文件“ C:\ Users \ Lenovo \ Anaconda3 \ lib \ site-packages \ matplotlib \ colorbar.py”,行1073,位于初始中     mappable.autoscale_None()

文件“ C:\ Users \ Lenovo \ Anaconda3 \ lib \ site-packages \ matplotlib \ cm.py”,行374,在autoscale_None中     引发TypeError('您必须首先为可映射对象设置set_array')

第二次尝试:

im = ax.imshow(colorfunction, cmap=temperature)
fig.colorbar(im, cax=cax)

cbar = fig.colorbar(im)

这显示了一些2D虚假图像和错误

AttributeError: 'Poly3DCollection' object has no attribute 'set_ylabel'

我在堆栈溢出中发现了类似的问题。它被标记为重复,因为已经回答了,还没有回答,但是没有指向先前回答的指导。

我该怎么办?

0 个答案:

没有答案