【发布时间】:2020-10-08 15:18:45
【问题描述】:
我想在饼图中添加一个事件,当悬停时,会显示一个带有值和标签的楔形的注释。经过一段时间的尝试,我得到了一个折线图的工作示例,但是对于饼图,我无法理解如何获取每个楔形的数据。到目前为止我的代码:
from matplotlib.figure import Figure
figure = Figure()
drawFigure = figure.subplots()
annotate = drawFigure.annotate("", xy=(0,0),xytext=(-20, 20), textcoords="offset points",
bbox=dict(boxstyle="round", fc="black", ec="b", lw=2),
arrowprops=dict(arrowstyle="->"))
annotate.set_visible(False)
days, amounts = zip(*my_dict.items())
wedges, _ = drawFigure.pie(amounts, labels=None)
drawFigure.axis('equal')
figure.canvas.mpl_connect("motion_notify_event", Hover)
def UpdateAnnotation(ind):
data = ind.get_patch_transform() # I'm not sure this does anything useful
# what now? can't seem to get anything working
def Hover(event):
vis = annotate.get_visible()
if event.inaxes == drawFigure:
for i, w in enumerate(wedges):
if w.contains_point([event.x, event.y]):
print("hovering")
figure.canvas.draw_idle()
理想情况下,我希望当我将鼠标悬停在楔形上时,会弹出一个注释,其中包含与该楔形相对应的 key : value 对。另外,把那个楔子炸开也不错。
【问题讨论】:
-
@JohanC 如果我要使用该方法,则意味着显示标签。我想要一个没有可见标签的饼图,但是当我将鼠标悬停在楔形上时,标签和值应该会出现。我可以在不使用
labels=None的情况下以某种方式隐藏标签吗?
标签: python matplotlib