【问题标题】:Simple pyramid hbar chart with Bokeh带散景的简单金字塔 hbar 图表
【发布时间】:2018-09-07 10:02:41
【问题描述】:

我尝试创建一个非常简单的金字塔图哦,带有散景的水平条,例如this example

我想要完成的结果是这样的:

是否有可能只用一个像这样的简单字典来创建它:

data = {'A': [0.8, 0.85], 'B': [0.31, 0.28], 'C': [0.91, 0.88], 'D': [0.73, 0.78]}

我不知道我应该如何准备我的数据以符合这种图表。

是这样的吗? :

>>> df = pd.DataFrame([[0.8, 0.85], [0.31, 0.28], [0.91, 0.88], [0.73, 0.78]], index=['A', 'B', 'C', 'D'], columns=['label1', 'label2'])
>>> df
   label1  label2
A    0.80    0.85
B    0.31    0.28
C    0.91    0.88
D    0.73    0.78

【问题讨论】:

    标签: python charts bar-chart bokeh


    【解决方案1】:

    我找到了我的解决方案,它给了我这个结果:

    基于示例population.py from Bokeh Github repo,包含import pandas as pd后,我只更改了以下代码:

    gender_transform = CustomJSTransform(args=dict(source=ages), func="", v_func="""
        var val = new Float64Array(xs.length)
        for (var i = 0; i < xs.length; i++) {
            if (source.data['Label'][i] == 'label1') # <--- This line only
                val[i] = -xs[i]
            else
                val[i] = xs[i]
        }
        return val
    """)
    
    
    groups = ['A', 'B', 'C', 'D']   # <--- Override the age group
    
    pyramid = figure(plot_width=600, plot_height=500, toolbar_location=None, y_range=groups,
                     title="label1 vs label2",
                     x_axis_label="",y_axis_label="", x_range=[-1, 1])
    pyramid.hbar(y="NameGrp", height=0.5, right=transform('Value', gender_transform),
                 source=ages, legend="Label", line_color="white",
                 fill_color=factor_cmap('Label', palette=["blue", "red"], factors=["label1", "label2"]))
    

    然后我的数据在update()方法中是这样构造的:

    >>> ages = pd.DataFrame([['A', 'label1', 0.85], ['B', 'label1', 0.28], ['C', 'label1', 0.88], ['D', 'label1', 0.78], ['A', 'label2', 0.80], ['B', 'label2', 0.18], ['C', 'label2', 0.98], ['D', 'label2', 0.88]], columns=['NameGrp', 'Label', 'Value'])
    >>>
      NameGrp   Label  Value
    0       A  label1   0.85
    1       B  label1   0.28
    2       C  label1   0.88
    3       D  label1   0.78
    4       A  label2   0.80
    5       B  label2   0.18
    6       C  label2   0.98
    7       D  label2   0.88
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      相关资源
      最近更新 更多