2011-04-27 153 views
17

鑑於ax = plt.subplot()如何在matplotlib中爲3D條形圖創建圖例?

ax.bar()[0]可以傳遞給plt.legend()

但是,ax.bar3d()返回None。如何爲顯示的條形圖創建圖例?

UPDATE:

傳遞傳奇= 「東西」 到ax.bar3d()和比調用ax.legend()提高

/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

你需要使用一個proxy artist傳說那裏不支持。

此代碼:

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 2013-12-15 02:06:48

+0

你知道如何使用曲面圖也做呢? – 2017-09-08 16:35:35

+0

@CharlieParker它應該適用於任何情節。也許你想要一個'colorbar'而不是一個標準的傳說? – Paul 2017-09-08 19:40:24

相關問題