Plotly deactivate x axis sorting

后端 未结 2 1244
南旧
南旧 2021-01-18 08:16

I want to plot a bar chart. On the x-axis are IDs of consultants. They range between 1000 and 2000. Each consultant has a specific number of customers (y-axis).

Now

2条回答
  •  情书的邮戳
    2021-01-18 08:50

    Interestingly Plotly seems to ignore categoryorder for integers but disabling of sorting can be achieved by passing type='category in xaxis in layout.

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

    default: "-"
    Sets the axis type. By default, plotly attempts to determined the axis type by looking into the data of the traces that referenced the axis in question.

    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')
    

提交回复
热议问题