[英]How to sort bar charts in plotly3.6?
I have plotly3.6 installed in my office computer, which I can not upgrade.我的办公室电脑上安装了plotly3.6,无法升级。 And I was trying to make sorted bar plots but it is failing to do so.我试图制作排序的条形图,但没有这样做。
How can I do so?我怎么能这样做?
My attempt:我的尝试:
def barplot(x,y):
data = [go.Bar(
x=x,
y=y,
marker={
'color': y,
'colorscale': 'Reds'
}
)]
layout = {
'xaxis': {
'tickvals': x,
'ticktext': [str(i) for i in x],
'tickangle': 40,
'type': "category",
'categoryorder': 'category ascending'
}
}
fig = go.FigureWidget(data=data, layout=layout)
return iplot(fig)
# plot
x = list('abcde')
y = [20,10,5,8,9]
barplot(x,y)
Related links: (This only works for plotly3.10, does not work for 3.6) How to plot sorted barplot in plolty3.10相关链接:(这仅适用于 plotly3.10,不适用于 3.6) 如何在 polty3.10 中绘制已排序的条形图
Help is appreciated.帮助表示赞赏。
Here's a version of your function which does this... basically you just pre-sort the x
and y
values that you pass into go.Bar()
.这是您的函数的一个版本,它执行此操作...基本上您只需对传递给go.Bar()
的x
和y
值进行预排序。 Because x
is a list of strings in this case, the xaxis.type
is inferred to be "category"
and the order of the categories is by default the order in which the x
list is provided, so there's no need to fiddle with tickvals
or ticktext
.因为在这种情况下x
是一个字符串列表,所以xaxis.type
被推断为"category"
并且"category"
的顺序默认是提供x
列表的顺序,所以没有必要摆弄tickvals
或ticktext
。
import plotly.graph_objs as go
from plotly.offline import iplot
def barplot(x,y):
sorted_y = sorted(y)
sorted_x = [str(j) for i,j in sorted(zip(y,x))]
data = [go.Bar(
x=sorted_x,
y=sorted_y,
marker={
'color': y,
'colorscale': 'Reds'
}
)]
fig = go.FigureWidget(data=data)
return iplot(fig)
# plot
x = list('abcde')
y = [20,10,5,8,9]
barplot(x,y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.