如何在matplotlib中为3D条形图创建图例? [英] How to create a legend for 3D bar in matplotlib?
本文介绍了如何在matplotlib中为3D条形图创建图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ax = plt.subplot()
: ax.bar( )[0]
可以传递给 plt.legend()
。
然而, , ax.bar3d()
返回无
。
更新:
将legend =stuff传递给 ax.bar3d()
而不是调用 ax.legend()
引发
/usr/lib/python2.6/site-packages/matplotlib/axes.py:4368:UserWarning:找不到标记的对象。在个人地块上使用标签='...'kwarg。
warnings.warn(没有找到标记的对象。
解决方案
您需要在传说中使用代理艺术家不支持。
这段代码:
来自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(元素)
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',zso rt ='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()
产生这样的结果:
Given ax = plt.subplot()
:
ax.bar()[0]
can be passed to plt.legend()
.
However, ax.bar3d()
returns None
. How do I create legend for displayed bars?
UPDATE:
Passing legend="stuff" to ax.bar3d()
and than calling ax.legend()
raises
/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. "
解决方案
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:
这篇关于如何在matplotlib中为3D条形图创建图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文