简体   繁体   English

使用 Altair 对标准化堆积条形图进行排序

[英]sort a normalized stacked bar chart with Altair

I am attempting to sort a normalized stacked bar chart based on a specific order.我正在尝试根据特定顺序对标准化堆积条形图进行排序。

I would like stacked bars sorted by this order:我想要按此顺序排序的堆积条:

Order = dict({'Paid work':1,'Education':2,'Sleep':3,'Other unpaid work':4,'Housework & Shopping':5,'Personal care':6, 'Eating and drinking':7,'TV and Radio':8,'Seeing friends':9,'Other leisures':10})

and Country (Y axis) should be also sorted by the length of "Paid work" bars.和国家(Y 轴)也应按“有偿工作”条的长度排序。

But I've got is this order with "TV & Radio" on the left end side of the chart and cant see in which country people spend time on "paid work" most.但我得到的是图表左端带有“电视和广播”的订单,无法看到在哪个国家/地区人们花时间在“有偿工作”上的时间最多。

My attempt:我的尝试:

在此处输入图像描述

Wrong code:错误代码:

alt.Chart(df).mark_bar(size=15).encode(
    alt.Y('Country:O'),
    alt.X('Time:Q', stack='normalize',sort=alt.EncodingSortField(field='Order',order='descending')),
    
    alt.Color('Category:N',sort=alt.EncodingSortField(field='Order')),
    tooltip=['Country', 'Category', 'Time']
).properties(
    width=600,
    height=600, title = {'text' :'How do people spend their time?',
'subtitle' : 'Average of minutes per day from time-use diaries for people between 15 and 64'})

I'm new to this platform, please take a look at the data and my simple code in this notebook: https://www.kaggle.com/hoangyennhi/exercise-dv我是这个平台的新手,请看一下这个笔记本中的数据和我的简单代码: https://www.kaggle.com/hoangyennhi/exercise-dv

If you set the sort parameter in alt.Color() to either 'ascending' or 'descending' , it will sort the stacked bars automatically:如果您将alt.Color()中的sort参数设置为'ascending''descending' ,它将自动对堆叠的条形图进行排序:

import altair as alt
from vega_datasets import data

source = data.barley()

alt.Chart(source).mark_bar().encode(
    x='sum(yield)',
    y='variety',
    color=alt.Color('site', sort='descending'))

在此处输入图像描述

However when you specify a custom sort order it will not as it is not yet supported by Vega-Lite .但是,当您指定自定义排序顺序时,它不会因为Vega-Lite 尚不支持 A workaround was described in this comment , which mentions that you can use a special field called color_<name-of-column>_sort_index , to sort according to the color index. 此评论中描述了一种解决方法,其中提到您可以使用名为color_<name-of-column>_sort_index的特殊字段来根据颜色索引进行排序。 In this example it would look like this:在此示例中,它看起来像这样:

site_order = ['Duluth', 'Crookston', 'Waseca', 'University Farm', 'Grand Rapids', 'Morris']
alt.Chart(source).mark_bar().encode(
    x='sum(yield)',
    y='variety',
    color=alt.Color('site', sort=site_order),
    order=alt.Order('color_site_sort_index:Q'))

在此处输入图像描述

I successfully order the category following your guide, this is the result:我按照您的指南成功订购了类别,结果如下:

在此处输入图像描述

But may I ask you a further question?但我可以再问你一个问题吗? I now have a problem with text color, when I add text it has the same color with the bars so it is invisible.我现在遇到了文本颜色的问题,当我添加文本时,它的颜色与条形相同,因此它是不可见的。 I would like to make it black but impossible.我想让它变黑但不可能。

I search on some github (source: https://github.com/altair-viz/altair/issues/1033 ) they told me to create a base chart without coloring my bars, and use that base to create text.我搜索了一些 github(来源: https://github.com/altair-viz/altair/issues/1033 ),他们告诉我创建一个基本图表而不给我的条着色,并使用该基础创建文本。 However I cant make my base without different colors while still keeping and order of the Category (order of category is made based on colors I defined in CatOrder)但是我不能在没有不同 colors 的情况下制作我的基础,同时仍然保持类别的顺序(类别的顺序是根据我在 CatOrder 中定义的 colors 制作的)

My question is: how to make text black while keeping the order of Categories in every bar row?我的问题是:如何在保持每个条形行中的类别顺序的同时使文本变黑?

Please view my code hear if my question confuses you: https://www.kaggle.com/hoangyennhi/exercise-dv如果我的问题让您感到困惑,请查看我的代码: https://www.kaggle.com/hoangyennhi/exercise-dv

Thank you so much for your help.非常感谢你的帮助。

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

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