这里是一个非Plotly解决方案,只是Matplotlib。
读者可以理解,如何在代码中轻松计算出正确的杆间距和位置。
In [102]: import matplotlib.pyplot as plt
...: from random import randint
...:
...: # faking some data
...: dow = 'Mon Tue Wed Thu Fri'.split()
...: emps = 'Concetta Laura Gerry Ceci'.split()
...: data = {e:{d:(randint(0,12),randint(6,12))for d in dow}for e in emps}
...:
...: # computing the appropriate width of the bars
...: nbars = len(emps)
...: d = 0.25 # distance between bars in terms of the bar width
...: total_width = 0.80 # total width of the horizontal stack of bars
...: # total_width = nbars*w + (nbars-1)*d*w â
...: w = total_width/(nbars+(nbars-1)*d)
...: origin = -total_width/2
...:
...: # Plotting
...: x = range(len(dow))
...: fig, ax = plt.subplots()
...: for n, (emp, hours) in enumerate(data.items()):
...: xn = [_+origin+n*(1+d)*w for _ in x]
...: bottoms, heights = zip(*hours.values())
...: ax.bar(xn, heights, width=w,
...: bottom=bottoms, align='edge', label=emp)
...: # add background to groups of bars
...: ax.bar(x, 24, (total_width+1)/2, bottom=0, zorder=0, alpha=0.15)
...: # fixing x tick labels
...: ax.set_xticks(range(len(dow)))
...: ax.set_xticklabels(dow)
...: # fix y axis
...: ax.set_ylim((-0.50, 24.50))
...: ax.set_yticks(range(0, 25, 2))
...: #
...: fig.legend()
...: plt.show()