gpt4 book ai didi

python - 悬停时如何注释broken_barh图表?

转载 作者:行者123 更新时间:2023-12-04 08:00:34 24 4
gpt4 key购买 nike

我正在尝试在 python 中使用 matplotlib 绘制甘特图,其中有两种不同算法建议的解决方案。每个算法的解决方案包含一组在不同时间点开始和结束的批次(以不同颜色显示)。
我能够绘制相同的图,但我想以这样的方式注释图形,每当我将鼠标悬停在解决方案上时,它就会显示批次详细信息或条形的长度(处理时间)。我尝试了几种方法,但没有发生。 [当我将鼠标移到批处理解决方案上时,我想看到 (x,y)= (Batch Processing Time, Algorithm Name) 值。

import matplotlib.pyplot as plt 
import numpy as np
import pandas as pd

%matplotlib notebook


fig, gnt = plt.subplots()
gnt.set_ylim(0, 50)
gnt.set_xlim(0, 65)


# Setting labels for x-axis and y-axis
gnt.set_xlabel('Batch Completion Time')
gnt.set_ylabel('Solution by')

# Setting ticks on y-axis
gnt.set_yticks([10, 25])
gnt.set_yticklabels(['Algo_1', 'Algo_2'])

# Setting graph attribute
gnt.grid(True)


#For Algo-1 Solution
gnt.broken_barh([(5,9), (14,1) , (15,4) , (19,9) , (28,4) , (34,4) , (38,5)],(5, 10),\
facecolors = {'tab:blue','tab:red', 'tab:olive', 'tab:pink', 'tab:cyan', 'tab:brown', 'tab:orange'})


#For Algo-2 Solution
gnt.broken_barh([(14,6), (22,4) , (29,7) , (36,3) , (39,15)],(20,10),\
facecolors = {'tab:blue','tab:red', 'tab:olive', 'tab:pink', 'tab:cyan'})

#upto here Gantt Chart is drawn
#Process of showing data while moving the mouse over the chart


annot = gnt.annotate("", xy=(0,0), xytext=(20,30),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()
y = bar.get_y()+ (0.5*bar.get_height())

annot.xy = (x,y) #box no (x,y) cordinate update karse
text = "{:.2g},{:.2g}".format(x,y)
annot.set_text(text)
annot.get_bbox_patch().set_alpha(0.9)


def hover(event):
vis = annot.get_visible()
if event.inaxes == gnt:
for bar in gnt:
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()
I would like to see (x,y)= (Batch Processing Time, Algorithm Name) value when I move hover the over the batch solution.

最佳答案

broken_barh不会创建单个条形,而是创建一个大条形 BrokenBarHCollection目的。
contains(event)被称为 FalseTrue返回,连同指示哪个小柱被点击的索引。
.get_paths()[ind].get_extents()可以得到那个小条的边界框。边界框的坐标导致开始时间和持续时间。

import matplotlib.pyplot as plt

fig, ax_gnt = plt.subplots()
ax_gnt.set_ylim(0, 50)
ax_gnt.set_xlim(0, 65)

ax_gnt.set_yticks([10, 25])
ax_gnt.set_yticklabels(['Algo_1', 'Algo_2'])

ax_gnt.grid(True)

# For Algo-1 Solution
ax_gnt.broken_barh([(5, 9), (14, 1), (15, 4), (19, 9), (28, 4), (34, 4), (38, 5)], (5, 10),
facecolors={'tab:blue', 'tab:red', 'tab:olive', 'tab:pink', 'tab:cyan', 'tab:brown', 'tab:orange'})
# For Algo-2 Solution
ax_gnt.broken_barh([(14, 6), (22, 4), (29, 7), (36, 3), (39, 15)], (20, 10),
facecolors={'tab:blue', 'tab:red', 'tab:olive', 'tab:pink', 'tab:cyan'})

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

def update_annot(brokenbar_collection, coll_id, ind, x, y):
annot.xy = (x, y)
box = brokenbar_collection.get_paths()[ind].get_extents()
text = f"{ax_gnt.get_yticklabels()[coll_id].get_text()} index:{ind} duration:{box.x1 - box.x0:.0f} "
annot.set_text(text)
annot.get_bbox_patch().set_alpha(0.9)

def hover(event):
vis = annot.get_visible()
if event.inaxes == ax_gnt:
for coll_id, brokenbar_collection in enumerate(ax_gnt.collections):
cont, ind = brokenbar_collection.contains(event)
if cont:
update_annot(brokenbar_collection, coll_id, ind['ind'][0], event.xdata, event.ydata)
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()
example plot

关于python - 悬停时如何注释broken_barh图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66499293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com