将在 Bokeh 0.9 中有效的堆叠条移动到 Bokeh 0.11
Moving a stacked bar that that worked in Bokeh 0.9 to Bokeh 0.11
我正在将我所有的系统从 Bokeh 0.9 更新到 Bokeh 0.11,并且有一张图表我似乎无法再工作了。
我从这样的 DataFrame 开始:
Out[75]:
First Second Third Fourth Fifth
Red 27 22 33 20 9
Blue 10 27 18 31 14
Magenta 32 10 11 8 10
Yellow 8 6 14 13 15
Green 9 5 6 6 2
然后我会制作一个漂亮的图表,轴上有颜色名称,5 个堆叠条的顺序与图例相同,这将给出排名。例如,这是我们在 0.9.0 中生成的具有 10 个等级和 10 个类别的堆叠条的输出:
Stacked Bar with 10 categories and 10 ranks
我过去常常这样做:
plot = Bar(dataframe, list_of_color_names, title="stack of 5 categorical ranked in order from first to last", stacked=True, legend="top_right", ylabel="count", width=600, height=600)
其中 "list_of_colors_names" 只是从 DataFrame 的索引生成的列表,但这不再有效。我意识到 0.11 下降 "stacked=True",现在我们使用 "stack",但我似乎仍然无法让它工作。
Bokeh 网站上的示例适用于更简单的条形图,当我将该模型应用于我的 DataFrame 时,我遇到了各种错误,例如“'NoneType' 对象不可迭代”,但我显然只是缺少关于这种类型的堆叠条在 0.11 中如何工作的大图。这里还有一些其他的 Bokeh 堆叠条讨论,但它们要么是针对较早版本的 Bokeh(我的代码在 0.9 中运行),要么似乎是不同的情况。现在做这种堆叠条最好的方法是什么?
我不知道这是否是唯一的方法,但如果您将 所有数据放在一列 中,Bokeh 0.11 中的堆叠条形图就可以工作,而不是在矩阵中。然后,您需要在相应的数据框列中提供矩阵行和列索引,在下面的示例代码中称为 nr
和 rank
。这些在调用 Bar 方法时被引用,其中 "stack" 应引用矩阵示例中的列。
import pandas as pd
from bokeh.charts import Bar, show
all_data={
'nr': [1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5],
'rank':['First','First','First','First','First',
'Second','Second','Second','Second','Second',
'Third','Third','Third','Third','Third',
'Fourth','Fourth','Fourth','Fourth','Fourth',
'Fifth','Fifth','Fifth','Fifth','Fifth'],
'data':[27,10,32,8,9,
22,27,10,6,5,
33,18,11,14,6,
20,31,8,16,6,
9,14,10,15,2]
}
df=pd.DataFrame(all_data)
p=Bar(df,label='nr',values='data',stack='rank',legend='top_right')
show(p)
评论:标准条形图调色板只有六种颜色,如您的 10 等级示例所示。我使用下面的代码片段(改编自其他代码片段)来生成条形图所需的尽可能多的不同颜色。它使用 matplotlib colormap 颜色图作为输入。
import matplotlib.cm as cm
import numpy as np
colormap =cm.get_cmap("jet")
different_colors=10
color_mapping=colormap(np.linspace(0,1,different_colors),1,True)
bokeh_palette=["#%02x%02x%02x" % (r, g, b) for r, g, b in color_mapping[:,0:3]]
p=Bar(df,label='nr',values='data',stack='rank',legend='top_right',palette=bokeh_palette)
show(p)
这是讨论如何选择的好页面 matplotlib colormaps。
我正在将我所有的系统从 Bokeh 0.9 更新到 Bokeh 0.11,并且有一张图表我似乎无法再工作了。
我从这样的 DataFrame 开始:
Out[75]:
First Second Third Fourth Fifth
Red 27 22 33 20 9
Blue 10 27 18 31 14
Magenta 32 10 11 8 10
Yellow 8 6 14 13 15
Green 9 5 6 6 2
然后我会制作一个漂亮的图表,轴上有颜色名称,5 个堆叠条的顺序与图例相同,这将给出排名。例如,这是我们在 0.9.0 中生成的具有 10 个等级和 10 个类别的堆叠条的输出:
Stacked Bar with 10 categories and 10 ranks
我过去常常这样做:
plot = Bar(dataframe, list_of_color_names, title="stack of 5 categorical ranked in order from first to last", stacked=True, legend="top_right", ylabel="count", width=600, height=600)
其中 "list_of_colors_names" 只是从 DataFrame 的索引生成的列表,但这不再有效。我意识到 0.11 下降 "stacked=True",现在我们使用 "stack",但我似乎仍然无法让它工作。
Bokeh 网站上的示例适用于更简单的条形图,当我将该模型应用于我的 DataFrame 时,我遇到了各种错误,例如“'NoneType' 对象不可迭代”,但我显然只是缺少关于这种类型的堆叠条在 0.11 中如何工作的大图。这里还有一些其他的 Bokeh 堆叠条讨论,但它们要么是针对较早版本的 Bokeh(我的代码在 0.9 中运行),要么似乎是不同的情况。现在做这种堆叠条最好的方法是什么?
我不知道这是否是唯一的方法,但如果您将 所有数据放在一列 中,Bokeh 0.11 中的堆叠条形图就可以工作,而不是在矩阵中。然后,您需要在相应的数据框列中提供矩阵行和列索引,在下面的示例代码中称为 nr
和 rank
。这些在调用 Bar 方法时被引用,其中 "stack" 应引用矩阵示例中的列。
import pandas as pd
from bokeh.charts import Bar, show
all_data={
'nr': [1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5],
'rank':['First','First','First','First','First',
'Second','Second','Second','Second','Second',
'Third','Third','Third','Third','Third',
'Fourth','Fourth','Fourth','Fourth','Fourth',
'Fifth','Fifth','Fifth','Fifth','Fifth'],
'data':[27,10,32,8,9,
22,27,10,6,5,
33,18,11,14,6,
20,31,8,16,6,
9,14,10,15,2]
}
df=pd.DataFrame(all_data)
p=Bar(df,label='nr',values='data',stack='rank',legend='top_right')
show(p)
评论:标准条形图调色板只有六种颜色,如您的 10 等级示例所示。我使用下面的代码片段(改编自其他代码片段)来生成条形图所需的尽可能多的不同颜色。它使用 matplotlib colormap 颜色图作为输入。
import matplotlib.cm as cm
import numpy as np
colormap =cm.get_cmap("jet")
different_colors=10
color_mapping=colormap(np.linspace(0,1,different_colors),1,True)
bokeh_palette=["#%02x%02x%02x" % (r, g, b) for r, g, b in color_mapping[:,0:3]]
p=Bar(df,label='nr',values='data',stack='rank',legend='top_right',palette=bokeh_palette)
show(p)
这是讨论如何选择的好页面 matplotlib colormaps。