我正在使用Bokeh创建一系列带有bokeh.charts.Donut
的饼图。图表基于相同DataFrame的子集,并且都具有相同的类别标签。我想确保相同的类别在各种图表中以相同的颜色显示,但我无法找出控制颜色的一致方法。
目前我正在按标签对输入的DataFrame进行排序,并将相同的颜色数组传递给palette
的{{1}}属性。这仍然不能按预期工作。代码如下:
Donut
将相同类别的甜甜圈图表映射到多个图表的颜色的正确方法是什么?我的另一个想法是在DataFrame中插入一个列,将标签值映射到颜色,然后将该列作为参数传递给main_colors = ['#10A400','#DB5E11','#C8C500','#CF102E','#00AFA8','#82BC00','#A40D7A','#FF7100','#1349BB']
#split out youth health problems
for_youth_health = detailed_assessment_safety.loc[detailed_assessment_safety.youth_health_prob.notnull()]
youth_health_issues = pd.DataFrame(for_youth_health.youth_health_prob.str.split(' ').tolist())
for col in youth_health_issues.columns:
newcol = 'youth_health_prob_' + str(col)
youth_health_issues = youth_health_issues.rename(columns={col:newcol})
youth_health_trans = pd.melt(youth_health_issues)
youth_health_trans = youth_health_trans.loc[youth_health_trans.value.notnull()]
youth_health_trans['issue_text'] = youth_health_trans.value.map(map_health_issues)
youth_health_trans.drop('value',axis=1,inplace=True)
youth_health_trans.sort_values(by='issue_text',ascending=True,inplace=True)
#pie of youth health issues
youth_health_issues = Donut(youth_health_trans,label='issue_text',
values='variable',agg='count',plot_width=plot_width,
plot_height=plot_height,title='Reported Youth Health Issues',
color=main_colors)
hover = HoverTool(point_policy='follow_mouse')
hover.tooltips = [('Number Reported','@values'),('Health Issue','@issue_text')]
youth_health_issues.add_tools(hover)
#split out adult health problems
for_adult_health = detailed_assessment_safety.loc[detailed_assessment_safety.adult_health_prob.notnull()]
adult_health_issues = pd.DataFrame(for_adult_health.adult_health_prob.str.split(' ').tolist())
for col in adult_health_issues.columns:
newcol = 'adult_health_prob_' + str(col)
adult_health_issues = adult_health_issues.rename(columns={col:newcol})
adult_health_trans = pd.melt(adult_health_issues)
adult_health_trans = adult_health_trans.loc[adult_health_trans.value.notnull()]
adult_health_trans['issue_text'] = adult_health_trans.value.map(map_health_issues)
adult_health_trans.drop('value',axis=1,inplace=True)
adult_health_trans.sort_values(by='issue_text',ascending=True,inplace=True)
#pie of adult health issues
adult_health_issues = Donut(adult_health_trans,label='issue_text',
values='variable',agg='count',plot_width=plot_width,
plot_height=plot_height,title='Reported Adult Health Issues',
palette=main_colors)
hover = HoverTool(point_policy='follow_mouse')
hover.tooltips = [('Number Reported','@values'),('Health Issue','@issue_text')]
adult_health_issues.add_tools(hover)
,但我也无法做到这一点。非常感谢任何帮助。
答案 0 :(得分:1)
经过一些实验,结果发现当你将一组颜色传递给palette
的{{1}}参数时,颜色会根据切片名称的字母排序与甜甜圈切片相关联。因此,Donut
颜色数组中的第一种颜色将与具有按字母顺序排列的名称等的切片相关联。