How to create a legend for 3D bar in matplotlib?

前端 未结 1 991
逝去的感伤
逝去的感伤 2020-12-03 01:52

Given ax = plt.subplot():

ax.bar()[0] can be passed to plt.legend().

However, ax.bar3d() returns No

相关标签:
1条回答
  • 2020-12-03 01:59

    You need to use a proxy artist where legends are not supported.

    This code:

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    x, y = np.random.rand(2, 100) * 4
    hist, xedges, yedges = np.histogram2d(x, y, bins=4)
    
    elements = (len(xedges) - 1) * (len(yedges) - 1)
    xpos, ypos = np.meshgrid(xedges[:-1]+0.25, yedges[:-1]+0.25)
    
    xpos = xpos.flatten()
    ypos = ypos.flatten()
    zpos = np.zeros(elements)
    dx = 0.5 * np.ones_like(zpos)
    dy = dx.copy()
    dz = hist.flatten()
    
    ax.bar3d(xpos[:8], ypos[:8], zpos[:8], dx, dy, dz, color='b', zsort='average')
    blue_proxy = plt.Rectangle((0, 0), 1, 1, fc="b")
    ax.bar3d(xpos[8:], ypos[8:], zpos[8:], dx, dy, dz, color='r', zsort='average')
    red_proxy = plt.Rectangle((0, 0), 1, 1, fc="r")
    ax.legend([blue_proxy,red_proxy],['cars','bikes'])
    
    plt.show()
    

    produces this:enter image description here

    0 讨论(0)
提交回复
热议问题