代码之家  ›  专栏  ›  技术社区  ›  alexfrize

Python Bokeh饼图颜色,如何更改

  •  1
  • alexfrize  · 技术社区  · 6 年前

    我有一个调色板:

    chart_colors = ['#44e5e2', '#e29e44', '#e244db',
                    '#d8e244', '#eeeeee', '#56e244', '#007bff', 'black']
    

    以及一个由Bokeh生成的饼图。

    x = Counter({
        'Submitted': 179,
        'Approved': 90,
        'Denied': 80
    })
    
    data = pd.DataFrame.from_dict(dict(x), orient='index').reset_index().rename(
        index=str, columns={0: 'value', 'index': 'claimNumber'})
    data['angle'] = data['value']/sum(x.values()) * 2*pi
    data['color'] = Category20c[len(x)]
    
    p = figure(plot_height=200,
               tooltips="@claimNumber: @value",
               name='claimChart')
    
    p.wedge(x=0, y=1, radius=0.28,
            start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
            line_color="white", fill_color='color', legend='claimNumber', source=data)
    
    curdoc().add_root(p)
    

    现在fill_color='color',color被定义为'data['color']=Category20c[len(x)]'。

    在旧版本中,可以提供'color'(p.wedge(…,color=…),但是我使用bokeh0.13.0,所以对于每种颜色我只有fill\u color='color'。

    如何将数据['color']更改为“chart\u colors”数组中的颜色?

    1 回复  |  直到 6 年前
        1
  •  2
  •   bigreddot    6 年前

    但是我使用的是bokeh0.13.0,所以对于每种颜色我只有fill\u color='color'。

    这不是真的。这个 color 参数可用于任何glyph方法(包括 wedge fill_color line_color 同时。您的问题有些令人困惑,因为调色板的大小与数据的大小不匹配,但下面是一个完整的示例,仅使用调色板,截断:

    from collections import Counter
    from math import pi
    
    import pandas as pd
    
    from bokeh.io import output_file, show
    from bokeh.plotting import figure
    from bokeh.transform import cumsum
    
    chart_colors = ['#44e5e2', '#e29e44', '#e244db',
                    '#d8e244', '#eeeeee', '#56e244', '#007bff', 'black']
    
    x = Counter({
        'Submitted': 179,
        'Approved': 90,
        'Denied': 80
    })
    
    data = pd.DataFrame.from_dict(dict(x), orient='index').reset_index().rename(
        index=str, columns={0: 'value', 'index': 'claimNumber'})
    data['angle'] = data['value']/sum(x.values()) * 2*pi
    data['color'] = chart_colors[:len(x)]
    
    p = figure(plot_height=350, title="Pie Chart", toolbar_location=None)
    
    p.wedge(x=0, y=1, radius=0.28,
            start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
            color='color', legend='claimNumber', source=data)
    
    p.axis.axis_label=None
    p.axis.visible=False
    p.grid.grid_line_color = None
    
    show(p)
    

    enter image description here