我正在尝试使用python中的matplotlib绘制甘特图,其中有两种不同算法建议的解决方案。每个算法的解决方案包含一组批次(以不同颜色显示),在不同的时间点开始和结束
我能够绘制相同的图形,但我希望以这样的方式对图形进行注释,即每当我将鼠标悬停在解决方案上时,它都会显示批处理细节或条的长度(处理时间)。我尝试了几种方法,但没有成功。[当我将鼠标移到批处理解决方案上时,我希望看到(x,y)=(批处理时间,算法名称)值
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()
broken_barh
不创建单个条,而是创建一个大的BrokenBarHCollection
对象。 当调用contains(event)
时,返回False
或True
,同时返回索引,指示单击了哪个小条用
.get_paths()[ind].get_extents()
可以得到那个小条的边界框。边界框的坐标表示开始时间和持续时间约翰,你救了我一天。。。如果使用pyqt5,则需要进行少量校正
相关问题 更多 >
编程相关推荐