当前位置: 首页 > 面试题库 >

将鼠标悬停在条形图上时如何注释X和Y的值

邓卓
2023-03-14
问题内容

我试图做到这一点,但是在执行时,它显示了以下错误:

bar,=plt.bar(xpos,revenue)

ValueError:太多值无法解包

我要如何解决它,因为我希望在悬停鼠标时添加xy注释值。这是我的以下代码:

import numpy as np
import matplotlib.pyplot as plt

company=['google','amazon','msft','fb']
revenue=[80,68,54,27]

fig=plt.figure()
ax=plt.subplot()

xpos=np.arange(len(company))

bar,=plt.bar(xpos,revenue)


annot = ax.annotate("", xy=(0,0), xytext=(-20,20),textcoords="offset points",
                    bbox=dict(boxstyle="round", fc="black", ec="b", lw=2),
                    arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)

def update_annot(ind):
    x,y = bar.get_data()
    x0 = x[ind["ind"][0]]
    y0 = y[ind["ind"][0]]
    annot.xy = (x0, y0)
    text = "({:.2g},{:.2g})".format(
        x0,y0,
    )
    annot.set_text(text)
    annot.get_bbox_patch().set_alpha(0.4)

def hover(event):
    vis = annot.get_visible()
    if event.inaxes == ax:
        cont, ind = bar.contains(event)
        if cont:
            update_annot(ind)
            annot.set_visible(True)
            fig.canvas.draw_idle()
        else:
            if vis:
                annot.set_visible(False)
                fig.canvas.draw_idle()

fig.canvas.mpl_connect("motion_notify_event", hover)

plt.show()

问题答案:

该错误告诉您plt.bar返回的单个对象无法解包。因此,您需要删除逗号(,)。而是将返回的bar容器称为bars = plt.bar(xpos,revenue)

您也不能盲目地复制散点图或条形图的其他解决方案。相反,您需要使其适应条形。因此,您需要浏览一下酒吧,并检查其中的哪一个(如果有的话)徘徊。

在此处查看完整的解决方案:

import numpy as np
import matplotlib.pyplot as plt

company=['google','amazon','msft','fb']
revenue=[80,68,54,27]

fig=plt.figure()
ax=plt.subplot()

xpos=np.arange(len(company))

bars = plt.bar(xpos,revenue)


annot = ax.annotate("", xy=(0,0), xytext=(-20,20),textcoords="offset points",
                    bbox=dict(boxstyle="round", fc="black", ec="b", lw=2),
                    arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)

def update_annot(bar):
    x = bar.get_x()+bar.get_width()/2.
    y = bar.get_y()+bar.get_height()
    annot.xy = (x,y)
    text = "({:.2g},{:.2g})".format( x,y )
    annot.set_text(text)
    annot.get_bbox_patch().set_alpha(0.4)


def hover(event):
    vis = annot.get_visible()
    if event.inaxes == ax:
        for bar in bars:
            cont, ind = bar.contains(event)
            if cont:
                update_annot(bar)
                annot.set_visible(True)
                fig.canvas.draw_idle()
                return
    if vis:
        annot.set_visible(False)
        fig.canvas.draw_idle()

fig.canvas.mpl_connect("motion_notify_event", hover)

plt.show()


 类似资料:
  • 我有一个条形图,每个条形图上都有一个标签。 悬停前: 悬停后: 这是我的代码: 我使用的chartjs版本是2.7.3

  • 问题内容: 我有一张图片: 然后,我将其显示在屏幕上: 如何检测鼠标是否在触摸图像? 问题答案: 使用获得描述你的边界,然后使用来检查,如果鼠标光标这里面。 例:

  • 问题内容: 我的问题.. 我有许多图像(在超链接中),并且我希望每个图像在鼠标悬停时变暗(即,使用具有高不透明度或某些功能的黑色蒙版),然后在mouseout上恢复正常。但是我想不出最好的方法。 我试过了.. jQuery颜色动画和一些javascript参考。 使用javascript设置图像的不透明度。 我不要 图像从80%的不透明度开始,然后在鼠标悬停时达到100%(这很容易)。 要在2张图

  • 问题内容: 似乎这是在webdriver中(至少在Java api中)进行悬停/鼠标悬停的方法: Python API中有可能吗?用于python的webdriver api文档似乎未提及任何类似内容。 http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html 如何在python webdriver中进行悬停/鼠标悬停? 问题答

  • 问题内容: 我想将鼠标悬停在GUI(地图)上的多个JButton上,并显示该位置的名称,例如曼彻斯特和伦敦。我的代码适用于一个按钮,但不适用于一个以上的按钮,并且为所有按钮位置打印最后一条消息(因为我有10个按钮)。 如果为true,则通过my 方法在指定区域的GUI上绘制文本。 我该如何解决? 问题答案: 为什么不使用现有的工具提示API? 您甚至可以使用HTML文本来生成格式化结果。 如果图像

  • 问题内容: 这是第二简单的翻转效果,但我仍然找不到任何简单的解决方案。 想要的: 我有一个项目列表和一个相应的幻灯片(DIV)列表。加载后,第一个列表项应被选中(粗体),并且第一张幻灯片应可见。当用户将鼠标悬停在另一个列表项上时,应改为选择该列表项,并显示相应的幻灯片。 以下代码有效,但 很糟糕 。如何以一种优雅的方式获得这种行为?jQuery具有数十种动画效果和复杂的过渡效果,但是我没有想出这种