首页 文章

情节轴问题与袖扣图

提问于
浏览
0

我试图避免在通过袖扣生成的堆积条形图中y轴上的死空间[plotly]

数据看起来像这样:

delay_percentage
crane_delay_type_gkey   1.0      2.0      3.0        4.0         5.0       6.0  7.0 8.0 9.0 10.0    ... 18.0     19.0   20.0    21.0    22.0    23.0    24.0    25.0    26.0    27.0
  crane_gkey                                                                                    
         288     76.425626  1.846134    0.000000    0.701747    0.000000     0.000000   4.933820    0.939261    0.000000    0.000000    ... 1.338717     0.291495   0.421048    0.269903    0.151145    0.636970    6.395612    1.589187    0.000000    0.172738
         333    46.153846   0.000000    0.000000    0.000000    0.000000    0.000000    7.692308    0.000000    0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
         338    81.818182   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
         345    75.000000   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    12.500000   0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000

我用于袖扣的代码:

df.iplot(kind ='barh', barmode = 'stack')

情节看起来像这样:

enter image description here

如何删除栏之间的空格?尤其是y轴值288和333之间的巨大差距 .

我已经尝试将crane_gkey值[y轴值]变成一个字符串,它没有做任何事情 . 另外,我如何增加袖扣条形图中的条形厚度 .

1 回答

  • 1

    为什么不在源头切断空值 . 我的意思是,使用 pandas 本身 .

    所以这是我的方法 .

    我们有一个示例数据框 .

    df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                              "bar", "bar", "bar", "bar"],
                        "B": ["one", "one", "one", "two", "two",
                              "one", "one", "two", "two"],
                        "C": ["small", "large", "large", "small",
                              "small", "large", "small", "small",
                              "large"],
                        "D": [1, 2, 2, 0, 0, 4, 5, 6, 7]})
    

    哪个在枢轴上给了我 .

    table = pd.pivot_table(df, values='D', index=['A', 'B'],
                         columns=['C'], aggfunc=np.sum)
    

    参考:here

    Output:

    C       large   small
    A   B       
    bar one 4.0     5.0
        two 7.0     6.0
    foo one 4.0     1.0
        two NaN     0.0
    

    因此,如果我们删除 foo and two ,我们可以得到正确的情节 . 我这样做是通过使用 .

    table = table.fillna(0) # replace all NaN values to zero
    table = table[(table.T != 0).any()] # remove all the rows which are having sum as zero.
    

    Output:

    C       large   small
    A   B       
    bar one 4.0     5.0
        two 7.0     6.0
    foo one 4.0     1.0
    

    最后我们可以使用袖扣来绘制

    plot = table.iplot(kind ='barh', barmode = 'stack', asFigure=True)
    py_offline.iplot(plot)
    

    请尝试此解决方案,如果这解决了您的问题,请告诉我们!

相关问题