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="5a6i5oi35pWw6YeP",标题字体=字典(尺寸=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="5a6i5oi35pWw6YeP"))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="TnVtYmVyIG9mIGN1c3RvbWVycw==",
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="TnVtYmVyIG9mIGN1c3RvbWVycw=="))
fig = go.Figure(data=data, layout=layout)
plotly.offline.iplot(fig, filename='stacked-bar')
这篇关于Plotly 停用 x 轴排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Plotly 停用 x 轴排序


- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01