Matplotlib 3d plot - associate colorbar with different axis

前端 未结 1 1389
南笙
南笙 2021-01-21 00:52

I\'m currently conducting some 3D plots in Python 2.7.9 using Matplotlib 1.4.3. (Sorry, my rating does not allow me to attach pictures yet). I would like to switch the data of

相关标签:
1条回答
  • 2021-01-21 01:28

    You have to use facecolors instead of cmap and for calling colorbar we have to create a mappable object using ScalarMappable. This code here worked for me.

    import pylab as py
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import axes3d
    import matplotlib as mpl
    from matplotlib import cm
    
    # Create figure and get data
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    X, Y, Z = axes3d.get_test_data(0.05)
    
    N = (Z-Z.min())/(Z-Z.min()).max()
    
    # Plot surface and colorbar
    c1 = ax.plot_surface(Z, Y, X, rstride=8, cstride=8, alpha=0.9, facecolors=cm.PiYG_r(N))
    
    m = cm.ScalarMappable(cmap=cm.PiYG_r)
    m.set_array(X)
    cbar = plt.colorbar(m)
    
    
    # Labels
    ax.set_xlabel('X')
    ax.set_xlim3d(-100, 100)
    ax.set_ylabel('Y')
    ax.set_ylim3d(-40, 40)
    ax.set_zlabel('Z')
    ax.set_zlim3d(-40, 40)
    
    plt.show()
    
    0 讨论(0)
提交回复
热议问题