【问题标题】:Moving a stacked bar that that worked in Bokeh 0.9 to Bokeh 0.11将在 Bokeh 0.9 中工作的堆叠条移动到 Bokeh 0.11
【发布时间】:2016-01-22 19:04:36
【问题描述】:

我正在将我的所有系统从 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' object is not iterable”,但我显然只是错过了关于这种类型的堆叠条在 0.11 中如何工作的大图。这里还有一些其他 Bokeh 堆叠条形讨论,但它们要么针对 Bokeh 的早期版本(我的代码在 0.9 中工作),要么似乎是不同的情况。现在做这种堆叠条的最好方法是什么?

【问题讨论】:

    标签: python-2.7 bokeh stacked-chart


    【解决方案1】:

    我不知道这是否是唯一的方法,但是 Bokeh 0.11 中的堆积条形图可以在您将所有数据放在一列而不是矩阵中时起作用。然后,您需要在相应的数据框列中提供矩阵行和列索引,在下面的示例代码中称为 nrrank。这些是在调用 Bar 方法时引用的,其中“堆栈”应指矩阵示例中的列。

    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 等级示例所示。我使用下面的代码 sn-p,改编自其他代码,根据需要为条形图生成尽可能多的不同颜色。它使用 matplotlib colormap 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

    【讨论】:

    • 谢谢,阿纳丁。那效果很好。我想从现在开始我只需要在列中获取数据。能够直接从矩阵生成图表而无需任何进一步更改,这很好,但我承认仅堆叠数据并重置索引并不难。
    • 很高兴它成功了!我同意矩阵公式更直观。但是,当通过查询数据库获取数据时,例如在我的情况下,数据会立即以列公式形式出现,无需在矩阵中对其进行排序。然后代码实际上变得更短了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多