首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在matplotlib中创建3D bar的图例?

如何在matplotlib中创建3D bar的图例?
EN

Stack Overflow用户
提问于 2011-04-27 19:03:45
回答 1查看 11.8K关注 0票数 19

给定ax = plt.subplot()

可以将ax.bar()[0]传递给plt.legend()

但是,ax.bar3d()返回None。如何为显示的栏创建图例?

更新:

将legend=“东西”传递给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. "
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-28 00:09:32

您需要使用不支持图例的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()

生成以下内容:

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5803015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档