Plotly deactivate x axis sorting(Plotly 停用 x 轴排序)
问题描述
我想绘制条形图.x 轴上是顾问的 ID.它们的范围在 1000 到 2000 之间.每个顾问都有特定数量的客户(y 轴).
现在我想在 plotly 中绘制一个条形图.但是,按顺序排列顾问 ID 升序并将它们解释为整数,但事实并非如此.它们应该像我给出的列表一样排序.
顺便说一句,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')
这篇关于Plotly 停用 x 轴排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Plotly 停用 x 轴排序
基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01