首页 > 解决方案 > 袖扣的坐标轴问题

问题描述

我试图在通过袖扣生成的堆积条形图中避免 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')

情节如下所示:

在此处输入图像描述

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

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

标签: pythonpandasbar-chartplotly

解决方案


为什么不直接从源头切断空值。我的意思是,使用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)

参考:这里

输出:

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.

输出:

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)

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


推荐阅读