简体   繁体   English

在 Altair 中订购条形图?

[英]Order bar chart in Altair?

How can I order my bar chart so that it is in the order of greatest to least value?如何订购我的条形图,使其按从最大值到最小值的顺序排列? I tried the below code, but it isn't giving me the expected result.我尝试了下面的代码,但它没有给我预期的结果。

I would like the bars ordered 'b', 'a', 'c' (by count)我希望条形按“b”、“a”、“c”排序(按计数)

df = pd.DataFrame([['a',2],['a',3],['b',4],['b',5],['b',4],['c',8]], columns=['Letters', 'Numbers'])

Letters Numbers
0   a   2
1   a   3
2   b   4
3   b   5
4   b   4
5   c   8

alt.Chart(df).mark_bar().encode(
   alt.X('Letters:N'),
   alt.Y('count():Q', sort=alt.EncodingSortField(field='count', op='count', order='ascending')))

输出图表

The sort keyword needs to be applied to the axis being sorted - here it's the X axis. sort关键字需要应用于正在排序的轴 - 这里是X轴。

alt.Chart(df).mark_bar().encode(
   alt.X('Letters:N', sort=alt.EncodingSortField(field="Letters", op="count", order='ascending')),
   alt.Y('count():Q'))

Simple alternative, specify the channel you'd like to sort by:简单的替代方法,指定您要排序的频道:

alt.Chart(df).mark_bar().encode(
   alt.X('Letters:N', sort='-y'),
   alt.Y('count():Q'))

From the documentation文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM