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

shubham bansal

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

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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将鼠标悬停在饼图上时,如何显示注释并分解楔形?

addeventlistener 鼠标悬停在给出 x 和 y 值时很慢

如何使用 D3.js 在鼠标悬停时在条形图上显示标签?

当我将鼠标悬停在用tmap制作的地图上时,如何修复标签

将鼠标悬停在d3中的图例上时,突出显示堆叠条形图中的相应图层

鼠标悬停时如何隐藏matplotlib图的x和y值

我的 GUI 中嵌入了一个绘图,我想添加一个光标以在将鼠标悬停在绘图上时显示 x 和 y 坐标

将鼠标悬停在雷达图上的某个点上时,为什么看不到数据值?

仅将鼠标悬停在方块上时如何显示方块 id 的值?

使用Python folium库将鼠标悬停在世界地图上时如何显示国家名称和人口?

将鼠标悬停在其方法和类上时如何显示它的javadoc?

将鼠标悬停在 Plotly 中的饼图上时如何获得标准符号(而不是科学符号)

将鼠标悬停在缩略图上时,如何为其他视频进行视频交换?

在C3js上,如何将工具提示悬停在条形图上时显示该条的数据?

当用户将鼠标悬停在 Openlayers 地图上时,角度会重新呈现

将鼠标悬停在 svg 地图上时突出显示表格

将鼠标悬停在图形的图表上时显示标签*具有较高的y轴值*,多条线和多轴

将鼠标悬停在上方时显示绘制线的长度的注释

将鼠标悬停在注释单元格上时禁用Excel弹出窗口

在带有鼠标悬停事件的条形图上进行转换时,“ TypeError:d未定义”

如何获得一个链接,以将鼠标悬停在鼠标悬停时忽略它?

将鼠标悬停在该图像上方的按钮上时,如何对图像应用悬停效果?

jQuery:将鼠标悬停在主元素上时如何悬停子元素的子元素

将鼠标悬停在单个元素CSS上时如何触发两个悬停

当我将鼠标悬停在它上面时,如何仅在单个列表中显示 X?

将鼠标悬停在其他元素上时如何删除和重新设置对输入的关注

将鼠标悬停在Material-UI React中时如何更改复选框样式和图标?

将鼠标悬停在文本上时将鼠标悬停

条形图的鼠标悬停事件