apply color map to mpl_toolkits.mplot3d.Axes3D.bar3d

前端 未结 3 1524
耶瑟儿~
耶瑟儿~ 2020-12-18 10:48

There is a \'color\' argument to Axes3D\'s bar3d function which can accept arrays to color individual bars different colors - but how would I apply a color map (i.e. cmap =

3条回答
  •  太阳男子
    2020-12-18 11:05

    You can pass a color array to the facecolors argument, it can set every patches in the surface a color.

    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    from matplotlib.ticker import LinearLocator, FormatStrFormatter
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    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)
    colors = np.random.rand(40, 40, 4)
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
            linewidth=0, antialiased=False)
    ax.set_zlim(-1.01, 1.01)
    
    ax.zaxis.set_major_locator(LinearLocator(10))
    ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
    
    plt.show()
    

    enter image description here

提交回复
热议问题