我正在尝试根据某些范围或名称类型获得指定颜色的条件为matplotlib barh图添加颜色。它的类似数据具有相同的颜色。即使它们发生在不同的情况下。这是代码的片段,它总是产生所有黑色的barh。
begin_dates = []
end_dates = []
job_names = []
job_colors = {}
color_list = []
color_counter = 0
for each in jobs:
begin_dates.append(each.start_time)
end_dates.append(each.end_time)
name = each.job_name.split('(')[0]
job_names.append(name)
if name not in job_colors.keys():
job_colors[name] = color_counter
color_counter += 1
color_list.append(job_colors[name])
end_dates, begin_dates = [mdates.date2num(item) for item in (end_dates, begin_dates)]
ypos = range(len(end_dates))
fig,ax = plt.subplots()
ax.barh(ypos,end_dates-begin_dates,left=begin_dates,align='center',color = color_list)
ax.axis('tight')
ax.xaxis_date()
#in case you want to display the names uncomment the bottom line.
#plt.yticks(range(len(job_names)),job_names,size='small')
plt.show()
感谢您的帮助。谢谢!