我想绘制条形图。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')
Run Code Online (Sandbox Code Playgroud)
小智 5
最新版本的 Plotly 现在在布局选项中有一个变量来指定 X 轴的分类布局:
fig.update_layout(
xaxis_type = 'category'
)
Run Code Online (Sandbox Code Playgroud)
有趣的是,Plotly 似乎忽略了categoryorder整数,但可以通过传入 in 来实现禁用type='category排序。xaxislayout
type ( enumerated : "-" | "linear" | "log" | "date" | "category" )默认值:“-”
设置轴类型。默认情况下,plotly 尝试通过查看引用相关轴的迹线数据来确定轴类型。
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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10958 次 |
| 最近记录: |