如何在x轴上的不同位置开始水平条?只要在条的开始处留出x天的空白就可以了。我可能需要使用ax.broken_barh来修改它,但这似乎是大量的工作,而且仅仅是让项目在不同的日子开始就需要两倍的数据。我可以在开始处设置一个指定宽度的白色部分吗?
import numpy as np
import matplotlib.pyplot as plt
#Types of work:
category_names = ['Excavation & Foundation', 'Exterior Framing',
'Roofing', 'Interior Framing', 'Drywall, Finish & Painting']
#crew days needed on project:
days = {
'':[0,0,0,0,0], #this works for creating space at top
'Proj 1 budget': [10, 15, 7, 32, 26],
'Proj 1 actual': [10, 14, 7, 30, 28],
'Proj 2 budget': [15, 20, 8, 30, 20],
'Proj 2 actual': [15, 19, 8, 28, 20],
'Proj 3 budget': [7, 15, 5, 20, 20],
'Proj 3 actual': [7, 14, 5, 19, 20],
}
def crew(days, category_names):
labels = list(days.keys())
data = np.array(list(days.values()))
data_cum = data.cumsum(axis=1)
category_colors = plt.colormaps['RdYlGn'](
np.linspace(0.15, 0.95, data.shape[1]))
fig, ax = plt.subplots(figsize=(10.4, 4.5)) #set graph lenght & widwth
ax.invert_yaxis()
ax.xaxis.set_visible(True)
title = ax.set_title('Tasks and Crew-Days')
title.set_position([0.5, 1.0]) #set title at center
ax.set_xlim(0, np.sum(data, axis=1).max())
plt.xlabel('Total Days')
for i, (colname, color) in enumerate(zip(category_names, category_colors)):
widths = data[:, i]
starts = data_cum[:, i] - widths
rects = ax.barh(labels, widths, left=starts, height=0.75, #bar heigth
label=colname, color=color)
r, g, b, _ = color
text_color = 'white' if r * g * b < 0.1 else 'black' #use one or the other
#text_color = 'black' #of these lines
ax.bar_label(rects, label_type='center', color=text_color)
ax.legend(ncols=len(category_names), bbox_to_anchor=(1.0, 1.00),
loc='upper right', fontsize='small')
return fig, ax
crew(days, category_names)
plt
plt.show()
1条答案
按热度按时间b1payxdu1#
我放弃了,用broken_barh重新写了一遍: