2017-11-25 157 views
0

我想绘制一个条形图。在x轴上是顾问的ID。他们的范围在1000到2000之间。每位顾问都有特定数量的顾客(y轴)。图解停用x轴排序

现在我想绘制阴谋条形图。但是阴谋地命令顾问ID升序并将它们解释为整数,但它们不是。他们应该像我列举的名单那样排列。

顺便说一句,在matplotlib中的顺序是正确的。

trace1 = go.Bar(
    x=consultants, 
    y=info[0,:] 
) 
trace2 = go.Bar(
    x=consultants, 
    y=info[1,:], 
) 
trace3 = go.Bar(
    x=consultants, 
    y=info[2,:] 
) 
trace4 = go.Bar(
    x=consultants, 
    y=info[3,:] 
) 

data = [trace1, trace2, trace3, trace4] 
layout = go.Layout(
     barmode='stack', 
     xaxis=dict(
     categoryorder='array', 
     categoryarray=consultants, 
     titlefont=dict(
     size=18, 
     color='black'), 
     showticklabels=True, 
     tickfont=dict(
     size=16, 
     color='black', 
     ), 
    tickangle=20 
    ), 
yaxis=dict(
    title='Number of customers', 
     titlefont=dict(
     size=18, 
     color='black'), 
    showgrid=True, 
    showline=False, 
    showticklabels=True, 
    tickfont=dict(
     size=16, 
     color='black') 
    ), 

) 

fig = go.Figure(data=data, layout=layout) 
py.iplot(fig, filename='stacked-bar') 
+0

看一看:https://plot.ly/python/reference/#layout-xaxis-categoryorder –

+0

好像是分辩关键字。但是如果我输入categoryorder ='array'。类别阵列应该如何?它是列表(范围(len(num_consultants)))还是num_consultants。到现在为止,现在有效果。 – user47091

回答

1

有趣Plotly似乎忽略categoryorder为整数,但排序的禁用可以通过layoutxaxis通过type='category来实现。

type (enumerated : "-" | "linear" | "log" | "date" | "category")

默认: “ - ”
设置轴的类型。默认情况下,通过查看 引用的轴的轨迹数据,图形地尝试 确定轴类型。

enter image description here

import plotly 
import plotly.graph_objs as go 
import numpy as np 

plotly.offline.init_notebook_mode() 

consultants = [1, 3, 2, 5, 4] 
info = np.random.randint(100, size=(5,5)) 

data = [] 
for i in range(len(info)): 
    data.append(go.Bar(x=consultants, 
         y=info[i,:])) 

layout = go.Layout(barmode='stack', 
        xaxis=dict(type='category'), 
        yaxis=dict(title='Number of customers')) 

fig = go.Figure(data=data, layout=layout) 
plotly.offline.iplot(fig, filename='stacked-bar') 
+0

谢谢。这就是它的工作原理。 – user47091