简体   繁体   English

无法在Bokeh .rect字形中按颜色分组ColumnDataSource数据

[英]Unable to group ColumnDataSource Data by color in Bokeh .rect glyph

I'd like to group my plot by color based on a column that holds strings in a .csv file. 我想根据在.csv文件中包含字符串的列按颜色对图表进行分组。 I was under the impression that "color = 'ColumnName'" would work. 我的印象是“ color ='ColumnName'”可以工作。

p.rect(x = range(len(source.data['Project Description'])),
   y = adj_h,
   color= 'Client',
   source = source,
   width = .4,
   height = 'Budget'     
  )

This just has my rectangle glyphs appear with black outlines and no fillcolor. 这只是让我的矩形字形显示为黑色轮廓,而没有填充色。

Colors in Bokeh are plotted using a color mapping scheme. 散景中的颜色是使用颜色映射方案绘制的。 First you need to set the colormap which allows Bokeh to know what color to use for which object. 首先,您需要设置颜色图,以使Bokeh知道对哪个对象使用哪种颜色。 You can either manually set the colormap by using a dictionary colormap = {'Client_1':'red', Client_2:'blue'} or if you can use the built in CategoricalColorMapper function to create a colormap colormap = CategoricalColorMapper(palette=palette, factors=list_of_factors) . 您可以使用字典colormap = {'Client_1':'red', Client_2:'blue'}手动设置颜色图,也可以使用内置的CategoricalColorMapper函数创建颜色图colormap = CategoricalColorMapper(palette=palette, factors=list_of_factors)

Then you can specify the colors in the plotting function. 然后,您可以在绘图功能中指定颜色。 Either you can use a list containing all the colors for all data points colors = [colormap[x] for x in source['Client']] and then pass the colors to the plot color=colors . 您可以使用包含所有数据点的所有颜色的列表color=colors colors = [colormap[x] for x in source['Client']] ,然后将颜色传递到绘图color=colors Or you can let bokeh do the transformation for you with directly in the plot function color={'field': 'Client', 'transform': colormap} 或者,您可以直接在绘图功能中让bokeh为您完成变换color={'field': 'Client', 'transform': colormap}


In your code, I'd try something like 在您的代码中,我会尝试类似

colormap = CategoricalColorMapper(palette=palette, factors=list(set(data['Client'])))

p.rect(x = range(len(source.data['Project Description'])),
   y = adj_h,
   color = {'field': 'Client', 'transform': colormap}',
   source = source,
   width = .4,
   height = 'Budget'     
  )

Where the palette is chosen depending on the number of categories and data is your original dataset. 根据类别数和数据选择调色板的位置是原始数据集。


References: 参考文献:

iris.py - Example on manually setting colors to each item in the dataset and using the list of colors in the plot. iris.py-关于为数据集中的每个项目手动设置颜色并使用绘图中的颜色列表的示例。

texas.py - Example using the palette and color mapper and then letting the plotting function set the colors with 'transform' . texas.py-示例使用调色板和颜色映射器,然后让绘图功能使用'transform'设置颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM