2011-04-27 7 views
17

ax = plt.subplot()을 감안할 때 :matplotlib에서 3D 막대의 범례를 만드는 방법은 무엇입니까?

ax.bar()[0]plt.legend()에 전달 될 수 있습니다.

그러나 ax.bar3d()None을 반환합니다. 표시된 막대의 범례는 어떻게 만듭니 까?

UPDATE : ax.bar3d()ax.legend()를 호출하는 것보다 = "물건"

전달 전설 당신은 전설이 지원되지 않는 proxy artist를 사용할 필요가

/usr/lib/python2.6/site-packages/matplotlib/axes.py:4368: UserWarning: No labeled objects found. Use label='...' kwarg on individual plots. 
warnings.warn("No labeled objects found. " 

답변

19

발생합니다.

이 코드 :

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() 

이 생성됩니다 enter image description here

+0

이 분산과 좋은 작품을()도, 당신이 너무 표면 플롯으로 작업을 수행하는 방법을 알고 – Alex

+0

감사합니다? –

+0

@CharlieParker 모든 플롯에서 작동해야합니다. 표준 전설이 아닌 '컬러 바'를 원할 수도 있습니다. – Paul

관련 문제