[英]Altair: not sorting an axis
Here is some simple sample code demonstrating the problem I would like to solve: 这是一些简单的示例代码,展示了我想解决的问题:
import pandas as pd
import altair as alt
categoryNames = [ 'a', 'f', 'r', 'u', 'p' ]
categories = pd.Series( categoryNames )
categories.index = categoryNames
amountsRaw = [ 50, -100, 75, 100, -500 ]
amounts = pd.Series( amountsRaw )
amounts.index = categoryNames
df = pd.DataFrame( { "Amounts" : amounts, "Categories" : categories } )
alt.Chart( df ).mark_bar().encode(
x='Categories',
y='Amounts',
color = alt.condition( alt.datum.Amounts > 0, alt.value( 'green' ), alt.value( 'red' ) )
)
This will produce a bar chart, but I do not want the x-axis categories sorted. 这将产生一个条形图,但是我不希望对x轴类别进行排序。 They need to appear in the same order as they do in the categoryNames array.
它们需要以与categoryNames数组中相同的顺序出现。 How can I do that?
我怎样才能做到这一点?
You can pass a list to the sort
property of the alt.X
encoding to control the order of the categories. 您可以将列表传递给
alt.X
编码的sort
属性,以控制类别的顺序。 For example: 例如:
alt.Chart(df).mark_bar().encode(
x=alt.X('Categories', sort=categoryNames),
y='Amounts',
color=alt.condition(alt.datum.Amounts > 0, alt.value('green'), alt.value('red'))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.