Plotly 停用 x 轴排序Plotly

2023-09-06 07:44:41 作者:一个男人的偏执

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

现在我想在 plotly 中绘制一个条形图.但是,按顺序排列顾问 ID 升序并将它们解释为整数,但事实并非如此.它们应该像我给出的列表一样排序.

Plotly colorscale 配色方案

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

trace1 = go.Bar(x=顾问,y=信息[0,:])trace2 = go.Bar(x=顾问,y=信息[1,:],)trace3 = go.Bar(x=顾问,y=信息[2,:])trace4 = go.Bar(x=顾问,y=信息[3,:])数据 = [trace1,trace2,trace3,trace4]布局=去.布局(barmode='堆栈',xaxis=dict(categoryorder='数组',categoryarray=顾问,标题字体=字典(尺寸=18,颜色='黑色'),showticklabels=真,滴答字体=字典(大小=16,颜色='黑色',),勾角=20),y轴=字典(title='客户数量',标题字体=字典(尺寸=18,颜色='黑色'),showgrid=真,显示线=假,showticklabels=真,滴答字体=字典(大小=16,颜色='黑色')),)fig = go.Figure(数据=数据,布局=布局)py.iplot(fig, filename='stacked-bar')

解决方案

有趣的是,Plotly 似乎忽略了整数的 categoryorder,但可以通过传递

绘图导入导入 plotly.graph_objs将 numpy 导入为 npplotly.offline.init_notebook_mode()顾问 = [1, 3, 2, 5, 4]信息 = np.random.randint(100, 大小=(5,5))数据 = []对于我在范围内(len(信息)):data.append(go.Bar(x=顾问,y=info[i,:]))布局 = go.Layout(barmode='stack',xaxis=dict(类型='类别'),yaxis=dict(title='客户数量'))fig = go.Figure(数据=数据,布局=布局)plotly.offline.iplot(fig, filename='stacked-bar')

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 I want to plot a bar chart in plotly. But plotly orders the consultant IDs ascending and interprets them as integer, but they are not. They shall be ordered like the list I give plotly.

By the way in matplotlib the order is right.

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

解决方案

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