是否可以根据类别之一的值对Altair分组条形图的列进行排序? [英] Is it possible to sort the columns of an Altair grouped bar chart based on the value of one of the categories?
问题描述
我有以下图表-
我希望能够对列进行排序(不是单个组的单个条-我已经知道如何做到),即订购3个子图-如果可以的话-我选择的任何类别( a
, b
或 c
)的值.
我尝试使用 alt.SortField
和 alt.EncodeSortField
,它们在图表上略有移动,但是如果您更改类别以查看是否他们实际上在工作.
代码-
将altair导入为alt将熊猫作为pd导入dummy = pd.DataFrame({'place':['Asia','Antarctica','Africa','Antarctica','Asia','Africa','Africa','Antarctica','Asia'],'类别':['a','a','a','b','b','b','c','c','c'],'值':[5,2,3,4,3,5,6,9,5]})alt.Chart(dummy).mark_bar().encode(x = alt.X('category'),y ='value',column = alt.Column('place:N',sort = alt.SortField(field ='value',order ='descending')),color ='category',)
我知道 alt.Column('place:N',sort = alt.SortField(field ='value',order ='descending'))
似乎不正确,因为我没有针对任何类别,所以我也尝试了 x = alt.X('category',sort = alt.SortField(field ='c',order ='descending'))
,但是它也不起作用.
预期产量(假设降序)-
- 如果我想按'c'排序,则应该首先在中间一列,然后是左列,最后是右列.
- 它似乎已经按'b'排序了.
- 如果我想按'a'排序,则应首先选择右列,然后是左列,最后是中间列.
这有点涉及,但是您可以通过一系列转换来做到这一点:
- a
然后按
"a"
进行排序:alt.Chart(dummy).transform_calculate(key ="datum.category =='a'").transform_joinaggregate(sort_key ="argmax(key)",groupby = ['place']).transform_calculate(sort_val ='datum.sort_key.value').mark_bar().encode(x = alt.X('category'),y ='value',column = alt.Column('place:N',sort = alt.SortField("sort_val",order ="descending")),color ='category',)
I have the following chart -
I'd like to be able to sort the columns (NOT the individual bars of a single group - I know how to do that already), i.e order the 3 sub-chart - if you will - based on the value of any category(a
,b
orc
) I choose.I tried using
alt.SortField
andalt.EncodeSortField
, they move around the charts a bit, but don't actually work if you change the category to see if they actually work.Code -
import altair as alt import pandas as pd dummy = pd.DataFrame({'place':['Asia', 'Antarctica','Africa', 'Antarctica', 'Asia', 'Africa', 'Africa','Antarctica', 'Asia'],'category':['a','a','a','b','b','b','c','c','c'],'value':[5,2,3,4,3,5,6,9,5]}) alt.Chart(dummy).mark_bar().encode( x=alt.X('category'), y='value', column=alt.Column('place:N', sort=alt.SortField(field='value', order='descending')), color='category', )
I know that
alt.Column('place:N', sort=alt.SortField(field='value', order='descending')),
doesn't seem correct, since I am not targeting any category, so I triedx=alt.X('category', sort=alt.SortField(field='c', order='descending')),
too, but it doesn't work either.Expected Output (assuming descending order)-
- If I want to order by 'c', then middle column should be first, followed by left and finally right column.
- It already seems ordered by 'b'.
- If I want to order by 'a', then right column should be first, followed by left and finally middle column.
解决方案This is a bit involved, but you can do this with a series of transforms:
- a Calculate Transform to select the value you want to sort on
- a Join-Aggregate Transform with
argmax
to join the desired values to each group - another calculate transform to pull-out the specific field within this result that you would like to sort by
It looks like this, first sorting by
"c"
:import altair as alt import pandas as pd dummy = pd.DataFrame({'place':['Asia', 'Antarctica','Africa', 'Antarctica', 'Asia', 'Africa', 'Africa','Antarctica', 'Asia'],'category':['a','a','a','b','b','b','c','c','c'],'value':[5,2,3,4,3,5,6,9,5]}) alt.Chart(dummy).transform_calculate( key="datum.category == 'c'" ).transform_joinaggregate( sort_key="argmax(key)", groupby=['place'] ).transform_calculate( sort_val='datum.sort_key.value' ).mark_bar().encode( x=alt.X('category'), y='value', column=alt.Column('place:N', sort=alt.SortField("sort_val", order="descending")), color='category', )
Then sorting by
"a"
:alt.Chart(dummy).transform_calculate( key="datum.category == 'a'" ).transform_joinaggregate( sort_key="argmax(key)", groupby=['place'] ).transform_calculate( sort_val='datum.sort_key.value' ).mark_bar().encode( x=alt.X('category'), y='value', column=alt.Column('place:N', sort=alt.SortField("sort_val", order="descending")), color='category', )
这篇关于是否可以根据类别之一的值对Altair分组条形图的列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!