正在try 交换直方图中列的顺序.

data = {'Age group': {0: '0 - 4', 1: '12 - 16', 2: '5 - 11', 3: 'not specified'},
 'Count': {0: 81.0, 1: 86.0, 2: 175.0, 3: 0.0}}

dp = pd.DataFrame(data)

alt.Chart(dp).mark_bar().encode(
    x=alt.X('Age group:N', title='Age group'),
    y='Count:Q',
    tooltip=['Age group', 'Count']
).properties(title='Number of children in voucher', width=400)

enter image description here

我只是想换一下订单,但找不到办法.

推荐答案

Alt.X schema有一个sort参数,它接受an array specifying the field values in preferred order:

X_ORDER = ["0 - 4", "5 - 11", "12 - 16", "not specified"] # >> set an order

alt.Chart(dp).mark_bar().encode(
    x=alt.X(
        "Age group:N",
        title="Age group",
        sort=X_ORDER, # >> add this line
    ),
    y="Count:Q",
    tooltip=["Age group", "Count"],
).properties(title="Number of children in voucher", width=400)

发帖主题:Re:Kolibrios

enter image description here

Python相关问答推荐

Django mysql图标不适用于小 case

Python虚拟环境的轻量级使用

如何将一个动态分配的C数组转换为Numpy数组,并在C扩展模块中返回给Python

对象的`__call__`方法的setattr在Python中不起作用'

如何保持服务器发送的事件连接活动?

如何启动下载并在不击中磁盘的情况下呈现响应?

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

通过ManyToMany字段与Through在Django Admin中过滤

AES—256—CBC加密在Python和PHP中返回不同的结果,HELPPP

如何排除prefecture_related中查询集为空的实例?

Python—压缩叶 map html作为邮箱附件并通过sendgrid发送

Polars map_使用多处理对UDF进行批处理

如何获得满足掩码条件的第一行的索引?

如何防止html代码出现在quarto gfm报告中的pandas表之上

Polars时间戳同步延迟计算

为什么在不先将包作为模块导入的情况下相对导入不起作用

对当前的鼹鼠进行编码,并且我的按键获得了注册

利用广播使减法更有效率

按最大属性值Django对对象进行排序

组颠倒大Pandas 数据帧