我在tkinter选项卡中有一个堆叠的条形图。

在这里,如果我尝试使用mplcursor在条中悬停。它在我需要显示条形图的值的地方显示y轴值
我用过
mplcursors.cursor(hover=True)如何显示有效的条形值?
发布于 2021-09-24 15:07:49
您可以创建自定义函数来更新注释。下面是一个示例:
from matplotlib.container import BarContainer
import matplotlib.pyplot as plt
import mplcursors
import pandas as pd
import numpy as np
def show_annotation(sel):
if type(sel.artist) == BarContainer:
bar = sel.artist[sel.target.index]
sel.annotation.set_text(f'{sel.artist.get_label()}: {bar.get_height():.1f}')
sel.annotation.xy = (bar.get_x() + bar.get_width() / 2, bar.get_y() + bar.get_height() / 2)
sel.annotation.get_bbox_patch().set_alpha(0.8)
df2 = pd.DataFrame({'alpha': [10, 20], 'beta': [np.nan, 30], 'gamma': [54, 38], 'delta': [42, 75]},
index=['First', 'Second'])
ax = df2.plot(kind='bar', stacked=True, rot=0, figsize=(15, 5), cmap='inferno')
cursor = mplcursors.cursor(hover=True)
cursor.connect('add', show_annotation)
plt.show()

https://stackoverflow.com/questions/69251152
复制相似问题