【问题标题】:Plotly deactivate x axis sortingPlotly 停用 x 轴排序
【发布时间】:2017-11-25 19:02:23
【问题描述】:

我想绘制条形图。 x 轴上是顾问的 ID。它们的范围在 1000 到 2000 之间。每个顾问都有特定数量的客户(y 轴)。

现在我想在 plotly 中绘制一个条形图。但是,按顺序排列顾问 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')

【问题讨论】:

  • 似乎是正确的关键字。但是如果我输入 categoryorder='array'。 categoryarray 应该是怎样的?是 list(range(len(num_consultants))) 还是 num_consultants。到现在已经有效果了。

标签: python plotly


【解决方案1】:

最新版本的 Plotly 现在在布局选项中有一个变量来指定 X 轴的分类布局:

fig.update_layout(
xaxis_type = 'category'
)

【讨论】:

    【解决方案2】:

    有趣的是,Plotly 似乎忽略了整数的categoryorder,但可以通过在xaxis 中传递type='category 在layout 中来实现禁用排序。

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

    默认值:“-”
    设置轴类型。默认情况下,阴谋尝试 通过查看轨迹的数据确定轴类型 引用了有问题的轴。

    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 1 2 3 4 我想要4 3 2 1 0 这是代码python xaxis=dict(type='category', tickvals = ["4", "3", "2" ,"1", "0"] ) )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    • 2020-03-15
    • 2021-09-04
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    相关资源
    最近更新 更多