绘图停用x轴排序

2024-10-04 03:29:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

现在我要绘制一个条形图。但是计划性地命令顾问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')

Tags: infotruegodatasizebardictcolor
1条回答
网友
1楼 · 发布于 2024-10-04 03:29:45

有趣的是,似乎故意忽略了整数的categoryorder,但是可以通过在xaxis中传递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.

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

相关问题 更多 >